Add topic creation

This commit is contained in:
Rumperuu 2021-03-21 16:34:08 +00:00
parent abcf16497f
commit 13188d49a1
15 changed files with 5315 additions and 35 deletions

View file

@ -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');

View file

@ -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;

View 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;