From d2335c6513952a3d07c9f2571aa761dac41557e3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrej=20Rama=C5=A1euski?= <andrej@x2.cz>
Date: Thu, 15 Apr 2021 00:33:32 +0200
Subject: [PATCH] Dokonceny zakladni CRUD pro Events

---
 .gitlab-ci.yml                   |  2 +-
 lib/CF/Controller/Events.pm      | 20 ++++++++++----------
 lib/CF/Schema/ResultSet/Event.pm | 15 +++++++++++++++
 openapi.yaml                     |  6 +++++-
 4 files changed, 31 insertions(+), 12 deletions(-)
 create mode 100644 lib/CF/Schema/ResultSet/Event.pm

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 3dfe9d5..b43488c 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -2,7 +2,7 @@ image: docker:19.03.12
 
 variables:
   DOCKER_TLS_CERTDIR: "/certs"
-  IMAGE_VER: 2.0.0
+  IMAGE_VER: 2.1.0
 
 services:
   - docker:19.03.12-dind
diff --git a/lib/CF/Controller/Events.pm b/lib/CF/Controller/Events.pm
index cdbc1ba..0b5b92d 100644
--- a/lib/CF/Controller/Events.pm
+++ b/lib/CF/Controller/Events.pm
@@ -30,8 +30,10 @@ sub create ($c) {
 sub get ($c) {
     $c->openapi->valid_input or return;
 
-    my $event = $c->schema->resultset('Event')->find($c->stash->{id});
-    return $c->error(404, 'Event not found') if ! $event;
+    my $event = $c->schema->resultset('Event')->find({
+        id => $c->stash->{id},
+    });
+    return $c->error(404, 'Event not found') if ! $event || $event->deleted;
 
     my $formatted = $event->format();
     $formatted->{acl} = [];
@@ -49,7 +51,7 @@ sub get ($c) {
         return $c->error(404, 'Event not found');
     }
 
-    $c->render(openapi => $c->spec_filter($formatted, 'Event'));
+    $c->render(json => $c->spec_filter($formatted, 'Event')); # BUG: error if openpi
 }
 
 sub list ($c) {
@@ -98,14 +100,11 @@ sub update ($c) {
     return $c->error(404, 'Event not found') if ! $event;
 
     my $update = $c->prepare_update_data( $event, $c->req->json );
-    my $guard  = $c->schema->txn_scope_guard;
-
-    $event->update({
-        %{ $update },
-        changed => \'now()',
-    });
 
+    my $guard  = $c->schema->txn_scope_guard;
+    $event->update({ %{ $update }, });
     $guard->commit;
+
     $c->render(status => 204, text => '');
 }
 
@@ -117,8 +116,9 @@ sub delete ($c) {
 
     my $guard  = $c->schema->txn_scope_guard;
     $event->update({ deleted => \'now()', });
-
     $guard->commit;
+
     $c->render(status => 204, text => '');
 }
+
 1;
diff --git a/lib/CF/Schema/ResultSet/Event.pm b/lib/CF/Schema/ResultSet/Event.pm
new file mode 100644
index 0000000..79f4452
--- /dev/null
+++ b/lib/CF/Schema/ResultSet/Event.pm
@@ -0,0 +1,15 @@
+package CF::Schema::ResultSet::Event;
+
+use strict;
+use warnings;
+
+use base 'DBIx::Class::ResultSet';
+
+use constant PROTECTED_FIELDS => [qw(
+    id
+    uuid
+    owner_id
+    deleted
+)];
+
+1;
diff --git a/openapi.yaml b/openapi.yaml
index 8911ab3..1b59267 100644
--- a/openapi.yaml
+++ b/openapi.yaml
@@ -401,6 +401,8 @@ paths:
         required: false
         schema:
           type: boolean
+          default: false
+        example: true
       - name: sort
         description: "Razeni"
         in: query
@@ -431,7 +433,7 @@ paths:
     get:
       x-mojo-to: events#get
       security:
-        - Bearer: ['optional']
+        - Bearer: ['optional', '*']
       tags:
         - events
       summary: "Detail udalosti"
@@ -440,6 +442,7 @@ paths:
       - name: id
         in: path
         required: true
+        example: 100345
         description: "ID"
         schema:
           type: integer
@@ -462,6 +465,7 @@ paths:
       - name: id
         in: path
         required: true
+        example: 100345
         description: "ID"
         schema:
           type: integer
-- 
GitLab