From c3784c10c35673db35dd23c934c077e7ae25f553 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrej=20Rama=C5=A1euski?= <andrej@x2.cz>
Date: Sun, 20 Dec 2020 02:22:41 +0100
Subject: [PATCH] Update programu

---
 lib/CF/Controller/Program.pm | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/lib/CF/Controller/Program.pm b/lib/CF/Controller/Program.pm
index 2c4f8bc..a526154 100644
--- a/lib/CF/Controller/Program.pm
+++ b/lib/CF/Controller/Program.pm
@@ -1,5 +1,9 @@
 package CF::Controller::Program;
+
 use Mojo::Base 'Mojolicious::Controller';
+use Mojo::Pg::PubSub;
+use feature 'signatures';
+no warnings qw{ experimental::signatures };
 
 sub entries {
     my $c = shift->openapi->valid_input or return;
@@ -23,4 +27,36 @@ sub entries {
     $c->render(openapi => \@entries );
 }
 
+sub update ($c) {
+    $c->openapi->valid_input or return;
+
+    my $entry = $c->schema->resultset('ProgramEntry')->find( $c->stash->{id} );
+    return $c->error(404, 'Program entry not found') if ! $entry;
+
+    my $update = $c->prepare_update_data( $entry, $c->req->json );
+    my $pubsub = Mojo::Pg::PubSub->new(pg => $c->pg);
+    my $guard  = $c->schema->txn_scope_guard;
+
+    # muze byt jen jeden aktualni bod programu
+    if ( $update->{is_live} ) {
+        $c->schema->resultset('ProgramEntry')->search({})->update(
+            { is_live => 'f' }
+        );
+    }
+
+    $entry->update( $update );
+
+    $pubsub->json('notify')->notify( notify => {
+        event   => 'program_entry_changed',
+        payload => {
+            id => $entry->id,
+            %{ $update },
+        }
+    });
+
+    $guard->commit;
+
+    $c->render(status => 204, text => '');
+}
+
 1;
-- 
GitLab