Add topic creation
This commit is contained in:
parent
abcf16497f
commit
13188d49a1
15 changed files with 5315 additions and 35 deletions
|
@ -19,7 +19,7 @@ has error_messages => sub {
|
|||
};
|
||||
};
|
||||
|
||||
sub check_token {
|
||||
sub check_exists {
|
||||
my $c = shift;
|
||||
|
||||
my $validation = $c->validation;
|
||||
|
@ -47,7 +47,7 @@ sub check_token {
|
|||
}
|
||||
}
|
||||
|
||||
sub add_token {
|
||||
sub create {
|
||||
my $c = shift;
|
||||
|
||||
my $validation = $c->validation;
|
||||
|
@ -120,7 +120,7 @@ sub add_token {
|
|||
}
|
||||
}
|
||||
|
||||
sub get_tokens {
|
||||
sub get_all {
|
||||
my $c = shift;
|
||||
|
||||
my $token_rs = $c->schema->resultset('DeviceToken');
|
||||
|
|
|
@ -65,31 +65,6 @@ sub create_jwt_from_path_and_scopes {
|
|||
return $jwt->encode;
|
||||
}
|
||||
|
||||
sub get_topics {
|
||||
my $c = shift;
|
||||
|
||||
my $topic_rs = $c->schema->resultset('Topic');
|
||||
|
||||
my @topics = (
|
||||
map {
|
||||
{
|
||||
id => $_->id,
|
||||
name => $_->name,
|
||||
numberOfSubscribers =>
|
||||
$_->search_related( 'device_subscriptions',
|
||||
{ 'topic_id' => $_->id } )->count,
|
||||
}
|
||||
} $topic_rs->all
|
||||
);
|
||||
|
||||
return $c->render(
|
||||
json => {
|
||||
success => Mojo::JSON->true,
|
||||
topics => \@topics,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
sub post_message {
|
||||
my $c = shift;
|
||||
|
||||
|
|
78
lib/Pear/LocalLoop/Controller/Api/Topic.pm
Normal file
78
lib/Pear/LocalLoop/Controller/Api/Topic.pm
Normal file
|
@ -0,0 +1,78 @@
|
|||
package Pear::LocalLoop::Controller::Api::Topic;
|
||||
use Mojo::Base 'Mojolicious::Controller';
|
||||
use LWP::UserAgent;
|
||||
use JSON;
|
||||
use JSON::Parse 'parse_json';
|
||||
use Mojo::JWT;
|
||||
use Mojo::File;
|
||||
use Carp;
|
||||
|
||||
has error_messages => sub {
|
||||
return {
|
||||
topic => {
|
||||
required => { message => 'Topic is required', status => 400 },
|
||||
not_in_resultset => { message => 'Topic already exists', status => 400 },
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
sub create {
|
||||
my $c = shift;
|
||||
|
||||
my $user = $c->stash->{api_user};
|
||||
|
||||
my $validation = $c->validation;
|
||||
$validation->input( $c->stash->{api_json} );
|
||||
|
||||
my $topic_rs = $c->schema->resultset('Topic');
|
||||
my $user_rs = $c->schema->resultset('User');
|
||||
|
||||
$validation->required('topic')->not_in_resultset( 'topic', $topic_rs );
|
||||
# TODO: validate that requester is an org user
|
||||
|
||||
my $organisation = $user->entity->organisation;
|
||||
|
||||
return $c->api_validation_error if $validation->has_error;
|
||||
|
||||
my $topic = $validation->param('topic');
|
||||
|
||||
$organisation->create_related(
|
||||
'topics',
|
||||
{
|
||||
name => $topic,
|
||||
}
|
||||
);
|
||||
|
||||
return $c->render(
|
||||
json => {
|
||||
success => Mojo::JSON->true,
|
||||
message => 'Topic created successfully!',
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
sub get_all {
|
||||
my $c = shift;
|
||||
|
||||
my $topic_rs = $c->schema->resultset('Topic');
|
||||
|
||||
my @topics = (
|
||||
map {
|
||||
{
|
||||
id => $_->id,
|
||||
name => $_->name,
|
||||
numberOfSubscribers =>
|
||||
$_->search_related( 'device_subscriptions',
|
||||
{ 'topic_id' => $_->id } )->count,
|
||||
}
|
||||
} $topic_rs->all
|
||||
);
|
||||
|
||||
return $c->render(
|
||||
json => {
|
||||
success => Mojo::JSON->true,
|
||||
topics => \@topics,
|
||||
}
|
||||
);
|
||||
}
|
||||
1;
|
|
@ -6,7 +6,7 @@ use warnings;
|
|||
|
||||
use base 'DBIx::Class::Schema';
|
||||
|
||||
our $VERSION = 33;
|
||||
our $VERSION = 34;
|
||||
|
||||
__PACKAGE__->load_namespaces;
|
||||
|
||||
|
|
|
@ -124,6 +124,13 @@ __PACKAGE__->has_many(
|
|||
{ cascade_copy => 0, cascade_delete => 0 },
|
||||
);
|
||||
|
||||
__PACKAGE__->has_many(
|
||||
"topics",
|
||||
"Pear::LocalLoop::Schema::Result::Topic",
|
||||
{ "foreign.organisation_id" => "self.id" },
|
||||
{ cascade_copy => 0, cascade_delete => 0 },
|
||||
);
|
||||
|
||||
__PACKAGE__->filter_column(
|
||||
pending => {
|
||||
filter_to_storage => 'to_bool',
|
||||
|
|
|
@ -21,6 +21,11 @@ __PACKAGE__->add_columns(
|
|||
is_auto_increment => 1,
|
||||
is_nullable => 0,
|
||||
},
|
||||
"organisation_id" => {
|
||||
data_type => "integer",
|
||||
is_foreign_key => 1,
|
||||
is_nullable => 1,
|
||||
},
|
||||
"name" => {
|
||||
data_type => "varchar",
|
||||
size => 200,
|
||||
|
@ -30,6 +35,13 @@ __PACKAGE__->add_columns(
|
|||
|
||||
__PACKAGE__->set_primary_key("id");
|
||||
|
||||
__PACKAGE__->belongs_to(
|
||||
"organisation",
|
||||
"Pear::LocalLoop::Schema::Result::Organisation",
|
||||
{ id => "organisation_id" },
|
||||
{ is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" },
|
||||
);
|
||||
|
||||
__PACKAGE__->has_many(
|
||||
"device_subscriptions",
|
||||
"Pear::LocalLoop::Schema::Result::DeviceSubscription",
|
||||
|
|
Reference in a new issue