From 94300ee81707c494ec820412be6ce487e4b63682 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrej=20Rama=C5=A1euski?= <andrej@x2.cz>
Date: Tue, 22 Dec 2020 23:05:45 +0100
Subject: [PATCH] Pridana prace s konfigem

---
 .gitlab-ci.yml                      |  2 +-
 lib/CF/Controller/Config.pm         | 51 +++++++++++++++++++++++++++++
 lib/CF/Schema/Result/ConfigEntry.pm | 24 ++++++++++++++
 openapi.yaml                        | 45 +++++++++++++++++++++++++
 sql/4/up.sql                        |  8 +++++
 5 files changed, 129 insertions(+), 1 deletion(-)
 create mode 100644 lib/CF/Controller/Config.pm
 create mode 100644 lib/CF/Schema/Result/ConfigEntry.pm
 create mode 100644 sql/4/up.sql

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 5b5ff4a..bc0d8db 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -3,7 +3,7 @@ image: docker:19.03.1
 variables:
   DOCKER_TLS_CERTDIR: "/certs"
   IMAGE_TAG: $CI_REGISTRY_IMAGE
-  IMAGE_VER: 0.5.1
+  IMAGE_VER: 0.6.0
 
 services:
   - docker:19.03.1-dind
diff --git a/lib/CF/Controller/Config.pm b/lib/CF/Controller/Config.pm
new file mode 100644
index 0000000..108da6d
--- /dev/null
+++ b/lib/CF/Controller/Config.pm
@@ -0,0 +1,51 @@
+package CF::Controller::Config;
+
+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;
+
+    my $entries = $c->schema->resultset('ConfigEntry')->search();
+
+    my @entries = ();
+
+    ENTRY:
+    while ( my $entry = $entries->next ) {
+        push @entries, $c->spec_filter( {
+            $entry->get_columns,
+        }, 'ConfigEntry');
+    }
+
+    $c->render(openapi => \@entries );
+}
+
+sub update ($c) {
+    $c->openapi->valid_input or return;
+
+    my $entry = $c->schema->resultset('ConfigEntry')->find( $c->stash->{id} );
+    return $c->error(404, 'Config entry not found') if ! $entry;
+    return $c->error(403, 'Config entry protected') if $entry->is_readonly;
+
+    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;
+
+    $entry->update( $update );
+
+    $pubsub->json('notify')->notify( notify => {
+        event   => 'config_entry_changed',
+        payload => {
+            id => $entry->id,
+            %{ $update },
+        }
+    });
+
+    $guard->commit;
+
+    $c->render(status => 204, text => '');
+}
+
+1;
diff --git a/lib/CF/Schema/Result/ConfigEntry.pm b/lib/CF/Schema/Result/ConfigEntry.pm
new file mode 100644
index 0000000..6c97930
--- /dev/null
+++ b/lib/CF/Schema/Result/ConfigEntry.pm
@@ -0,0 +1,24 @@
+package CF::Schema::Result::ConfigEntry;
+
+use strict;
+use warnings;
+
+use base 'DBIx::Class::Core';
+
+our $VERSION = 1;
+
+__PACKAGE__->table('config');
+
+__PACKAGE__->add_columns(
+    qw(
+        id
+        is_readonly
+        value
+        default
+        description
+    ),
+);
+
+__PACKAGE__->set_primary_key('id');
+
+1;
diff --git a/openapi.yaml b/openapi.yaml
index 41226eb..4915664 100644
--- a/openapi.yaml
+++ b/openapi.yaml
@@ -44,6 +44,17 @@ components:
         default: 100
         minimum: 1
   schemas:
+    ConfigEntry:
+      type: object
+      properties:
+        id:
+          type: string
+          readOnly: true
+        is_readonly:
+          type: boolean
+          readOnly: true
+        value:
+          type: string
     ProgramScheduleEntry:
       type: object
       properties:
@@ -155,6 +166,40 @@ components:
               $ref: '#/components/schemas/PostHistoryItem'
 
 paths:
+  /config:
+    get:
+      x-mojo-to: config#entries
+      tags:
+        - config
+      summary: "Konfigurace systemu"
+      operationId: getConfig
+      responses:
+        200:
+          description: Konfigurace systemu
+          content:
+            application/json:
+              schema:
+                type: array
+                items:
+                  $ref: '#/components/schemas/ConfigEntry'
+  /config/{id}:
+    put:
+      x-mojo-to: config#update
+      security:
+        - Bearer: ['chairman']
+      tags:
+        - config
+      summary: "Upravit polozku konfigu"
+      operationId: updateConfigEntry
+      requestBody:
+        content:
+          application/json:
+            schema:
+              $ref: '#/components/schemas/ConfigEntry'
+      responses:
+        204:
+          description: Program entry updated
+
   /program:
     get:
       x-mojo-to: program#entries
diff --git a/sql/4/up.sql b/sql/4/up.sql
new file mode 100644
index 0000000..c03e257
--- /dev/null
+++ b/sql/4/up.sql
@@ -0,0 +1,8 @@
+create table "config" (
+    "id" varchar(64),
+    "is_readonly" bool not null default false,
+    "value" text,
+    "default" text,
+    "description" text,
+    primary key("id")
+);
-- 
GitLab