From 0eabd9cf1e1d1ddda42e9010dfbbe99c9908c4af Mon Sep 17 00:00:00 2001 From: Ben Goldsworthy Date: Sun, 1 Nov 2020 18:06:11 +0000 Subject: [PATCH] Add API endpoint for sending push notis --- lib/Pear/LocalLoop.pm | 2 + .../LocalLoop/Controller/Api/Sendmessage.pm | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 lib/Pear/LocalLoop/Controller/Api/Sendmessage.pm diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index caa62ae..29121dc 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -141,6 +141,8 @@ sub startup { $api_public->post('/register')->to('api-register#post_register'); $api_public->post('/logout')->to('api-auth#post_logout'); $api_public->post('/feedback')->to('api-feedback#post_feedback'); + $api_public->post('/send-message')->to('api-sendmessage#post_message'); + #$api_public->post('/send-message')->to('api-sendmessage#post_message'); # Private, must be authenticated api routes my $api = $api_public->under('/')->to('api-auth#auth'); diff --git a/lib/Pear/LocalLoop/Controller/Api/Sendmessage.pm b/lib/Pear/LocalLoop/Controller/Api/Sendmessage.pm new file mode 100644 index 0000000..1dd8a4c --- /dev/null +++ b/lib/Pear/LocalLoop/Controller/Api/Sendmessage.pm @@ -0,0 +1,42 @@ +package Pear::LocalLoop::Controller::Api::Sendmessage; +use Mojo::Base 'Mojolicious::Controller'; + +has error_messages => sub { + return { + email => { + required => { message => 'Email is required or not registered', status => 400 }, + in_resultset => { message => 'Email is required or not registered', status => 400, error => "required" }, + }, + messagetext => { + required => { message => 'Message is required', status => 400 }, + }, + }; +}; + +sub post_message { + my $c = shift; + + my $validation = $c->validation; + $validation->input( $c->stash->{api_json} ); + + my $user_rs = $c->schema->resultset('User'); + + # $validation->required('email')->in_resultset( 'email', $user_rs ); + $validation->required('messagetext'); + + return $c->api_validation_error if $validation->has_error; + + my $user = $user_rs->find({'email' => $validation->param('email')}); +=begin comment + $c->schema->resultset('Feedback')->create({ + user => $user, + messagetext => $validation->param('messagetext'), + }); +=cut + return $c->render( json => { + success => Mojo::JSON->true, + message => 'Your message has been sent successfully!', + }); +} + +1;