From 23ca3d73652a3218d64d8ade0779bbdcdcaa03ad Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrej=20Rama=C5=A1euski?= <andrej@x2.cz>
Date: Tue, 9 Nov 2021 23:35:03 +0100
Subject: [PATCH] Update API, cleanup

---
 .gitlab-ci.yml                 |   2 +-
 lib/CF2022/Controller/Lists.pm |  14 +++-
 openapi.yaml                   |  74 ++++++++++++++++++-
 openapi2.yaml                  | 130 ---------------------------------
 order-parametrs.txt            |  68 -----------------
 5 files changed, 87 insertions(+), 201 deletions(-)
 delete mode 100644 openapi2.yaml
 delete mode 100644 order-parametrs.txt

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 755680b..486f570 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -2,7 +2,7 @@ image: docker:20.10.9
 
 variables:
   DOCKER_TLS_CERTDIR: "/certs"
-  IMAGE_VER: 0.1.0
+  IMAGE_VER: 0.2.0
 
 services:
   - docker:20.10.9-dind
diff --git a/lib/CF2022/Controller/Lists.pm b/lib/CF2022/Controller/Lists.pm
index 0d982e5..bd9c587 100644
--- a/lib/CF2022/Controller/Lists.pm
+++ b/lib/CF2022/Controller/Lists.pm
@@ -15,6 +15,9 @@ sub options {
         $c->config->{pretix_event},
     );
 
+    if ( $args->{type} eq 'products' ) {
+        $url .= 'items/';
+    }
     if ( $args->{type} eq 'variations' ) {
         $url .= 'items/' . $args->{product_id} . '/variations/';
     }
@@ -30,7 +33,16 @@ sub options {
 
     RECORD:
     foreach my $record ( @{ $records->{results} } ) {
-        if ( $args->{type} eq 'questions' ) {
+        if ( $args->{type} eq 'products' ) {
+            next RECORD if ! $record->{active};
+            next RECORD if $args->{category_id} && $record->{category} != $args->{category_id};
+            push @options, {
+               value => $record->{id},
+               label => $record->{name}{en},
+               price => $record->{default_price} + 0,
+            };
+        }
+        elsif ( $args->{type} eq 'questions' ) {
             next RECORD if $record->{hidden};
             push @options, {
                value => $record->{id},
diff --git a/openapi.yaml b/openapi.yaml
index e6ac196..b11b1d6 100644
--- a/openapi.yaml
+++ b/openapi.yaml
@@ -29,6 +29,15 @@ components:
           type: string
         response:
           type: string
+    Product:
+      type: object
+      properties:
+        id:
+         type: integer
+        variation:
+          type: integer
+        price:
+          type: integer
     Option:
       type: object
       properties:
@@ -54,13 +63,19 @@ paths:
           required: true
           schema:
             type: string
-            enum: ['variations', 'questions']
+            enum: ['products', 'variations', 'questions']
         - name: product_id
           in: query
           description: Product ID for variations
           required: false
           schema:
             type: integer
+        - name: category_id
+          in: query
+          description: Category ID for variations
+          required: false
+          schema:
+            type: integer
       responses:
         200:
           description: Options list
@@ -71,3 +86,60 @@ paths:
                 items:
                   $ref: '#/components/schemas/Option'
 
+  /orders:
+    post:
+      x-mojo-to: orders#create
+      tags:
+        - orders
+      summary: "Vytvorit registraci"
+      operationId: createOrder
+      requestBody:
+        required: true
+        content:
+          application/json:
+            schema:
+              type: object
+              properties:
+                token:
+                  type: string
+                name:
+                  type: string
+                  maxLength: 128
+                  example: 'Ivan Bartoš'
+                email:
+                  type: string
+                  maxLength: 128
+                  example: 'ivan.bartos@pirati.cz'
+                organization:
+                  type: string
+                  example: 'ČTK'
+                products:
+                  type: array
+                  items:
+                    $ref: '#/components/schemas/Product'
+                responses:
+                  type: array
+                  items:
+                    $ref: '#/components/schemas/Response'
+                  example:
+                    - question_id: 1
+                      response: XXXXX
+                    - question_id: 2
+                      response: ZZZZZ
+              required:
+                - type
+                - name
+                - email
+                - products
+                - responses
+      responses:
+        201:
+          description: Order created
+          content:
+            application/json:
+              schema:
+                type: object
+                properties:
+                  url:
+                    type: string
+                    description: Tickets url
diff --git a/openapi2.yaml b/openapi2.yaml
deleted file mode 100644
index a39b85e..0000000
--- a/openapi2.yaml
+++ /dev/null
@@ -1,130 +0,0 @@
-openapi: 3.0.3
-
-info:
-  version: "2.0"
-  title: CF 2022
-  description: CF 2022
-  license:
-    name: Artistic License 2.0
-    url: https://www.perlfoundation.org/artistic-license-20.html
-  contact:
-    name: Andrej Ramašeuski
-    email: andrej.ramaseuski@pirati.cz
-    url: https://pardubicky.pirati.cz/lide/andrej-ramaseuski/
-
-servers:
-    - url: https://cf2022.pirati.cz/api
-      description: Production server
-    - url: https://cf2022.pir-test.eu/api
-      description: Developement server
-    - url: http://127.0.0.1:3000/api
-      description: Local server
-
-components:
-  schemas:
-    Response:
-      type: object
-      properties:
-        request_id:
-          type: string
-        response:
-          type: string
-    Option:
-      type: object
-      properties:
-        value:
-          type: string
-        label:
-          type: string
-        price:
-          type: string
-
-paths:
-  /options:
-    get:
-      x-mojo-to: lists#options
-      tags:
-        - options
-      summary: "Options"
-      operationId: getOptions
-      parameters:
-        - name: list
-          in: query
-          description: List ID
-          type: string
-          required: true
-          enum: ['lunch', 'questions']
-      responses:
-        200:
-          description: Options list
-          content:
-            application/json:
-              schema:
-                type: array
-                items:
-                  $ref: '#/components/schemas/Option'
-
-  /orders:
-    post:
-      x-mojo-to: orders#create
-      tags:
-        - orders
-      summary: "Vytvorit registraci"
-      operationId: createOrder
-      requestBody:
-        required: true
-        content:
-          application/json:
-            schema:
-              type: object
-              properties:
-                type:
-                  type: integer
-                  enum: [1, 2, 3]
-                  example: 1
-                token:
-                  type: string
-                name:
-                  type: string
-                  maxLength: 128
-                  example: 'Ivan Bartoš'
-                email:
-                  type: string
-                  maxLength: 128
-                  example: 'ivan.bartos@pirati.cz'
-                organization:
-                  type: string
-                  example: 'ČTK'
-                lunch:
-                  type: integer
-                  example: 2
-                party:
-                  type: array
-                  items:
-                    type: integer
-                responses:
-                  type: array
-                  items:
-                    $ref: '#/components/schemas/Response'
-                  example:
-                    - question_id: 1
-                      response: XXXXX
-                    - question_id: 2
-                      response: ZZZZZ
-                notes:
-                  type: string
-              required:
-                - type
-                - name
-                - email
-      responses:
-        201:
-          description: Order created
-          content:
-            application/json:
-              schema:
-                type: object
-                properties:
-                  url:
-                    type: string
-                    description: Tickets url
diff --git a/order-parametrs.txt b/order-parametrs.txt
deleted file mode 100644
index 75cc0f4..0000000
--- a/order-parametrs.txt
+++ /dev/null
@@ -1,68 +0,0 @@
-status (potrebuje platbu) - 'n', jinak 'p'
-email
-payment_provider "manual" ? "free"
-
-positions
-
-[
-
-
-
-]
-
-
-
-
-iPOST /api/v1/organizers/bigevents/events/sampleconf/orders/ HTTP/1.1
-Host: pretix.eu
-Accept: application/json, text/javascript
-Content-Type: application/json
-
-{
-  "email": "dummy@example.org",
-  "locale": "en",
-  "sales_channel": "web",
-  "fees": [
-    {
-      "fee_type": "payment",
-      "value": "0.25",
-      "description": "",
-      "internal_type": "",
-      "tax_rule": 2
-    }
-  ],
-  "payment_provider": "banktransfer",
-  "invoice_address": {
-    "is_business": false,
-    "company": "Sample company",
-    "name_parts": {"full_name": "John Doe"},
-    "street": "Sesam Street 12",
-    "zipcode": "12345",
-    "city": "Sample City",
-    "country": "UK",
-    "state": "",
-    "internal_reference": "",
-    "vat_id": ""
-  },
-  "positions": [
-    {
-      "positionid": 1,
-      "item": 1,
-      "variation": null,
-      "price": "23.00",
-      "attendee_name_parts": {
-        "full_name": "Peter"
-      },
-      "attendee_email": null,
-      "addon_to": null,
-      "answers": [
-        {
-          "question": 1,
-          "answer": "23",
-          "options": []
-        }
-      ],
-      "subevent": null
-    }
-  ]
-}
-- 
GitLab