Skip to content
Snippets Groups Projects
Select Git revision
  • master default protected
1 result

Posts.pm

Blame
  • Posts.pm 7.19 KiB
    package CF::Controller::Posts;
    
    use Mojo::Base 'Mojolicious::Controller';
    use Mojo::Pg::PubSub;
    use feature 'signatures';
    no warnings qw{ experimental::signatures };
    
    sub create ($c) {
        $c->openapi->valid_input or return;
    
        my $args   = $c->req->json;
        my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
    
        # Navrh postupu muze predlozit jenom clen
        if ( $args->{type} == 0 && ! $c->user_roles->{member} ) {
            return $c->error(401, 'Insufficient permissions');
        }
    
        my $program_entry = $c->schema->resultset('ProgramEntry')->search({
            is_live => 1,
        })->first;
    
        # Neni zadny aktivnibod rozpravy
        if ( ! $program_entry ) {
            return $c->error(403, 'Debate closed');
        }
    
        my $post = $program_entry->add_to_posts({
            user_id          => $c->user->{id},
            type             => $args->{type},
            content          => $args->{content},
        });
    
        $pubsub->json('posts')->notify( posts => {
            event   => 'created',
            payload => $post->view->format(),
        });
    
        $c->render(
            status  => 201,
            openapi => { id => $post->id },
        );
    };
    
    sub get ($c) {
        $c->openapi->valid_input or return;
    
        my $post = $c->schema->resultset('Post_view')->find($c->stash->{id});
        return $c->error(404, 'Post not found') if ! $post;
    
        my $formatted = $post->format();
    
        if (my $my_vote = $post->rankings({ user_id => $c->user->{id} })->first) {
            $formatted->{ranking}{my_vote} = $my_vote->ranking;
        }
    
        foreach my $history ( $post->history() ) {
            push @{ $formatted->{history_log} }, {
                attribute  => 'content',
                value      => $history->content,
                datetime   => $history->datetime,
                originator => '',
            }
        }
    
        $c->render(openapi => $c->spec_filter($formatted, 'Post'));
    }
    
    sub list ($c) {
        $c->openapi->valid_input or return;