Skip to content
Snippets Groups Projects
Verified Commit 7c1289a1 authored by Andrej Ramašeuski's avatar Andrej Ramašeuski
Browse files

Options refactoring

parent 23ca3d73
No related branches found
No related tags found
No related merge requests found
Pipeline #5988 passed
...@@ -2,7 +2,7 @@ image: docker:20.10.9 ...@@ -2,7 +2,7 @@ image: docker:20.10.9
variables: variables:
DOCKER_TLS_CERTDIR: "/certs" DOCKER_TLS_CERTDIR: "/certs"
IMAGE_VER: 0.2.0 IMAGE_VER: 0.3.0
services: services:
- docker:20.10.9-dind - docker:20.10.9-dind
......
package CF2022::Controller::Lists;
use Mojo::Base 'Mojolicious::Controller';
use Mojo::UserAgent;
sub options {
my $c = shift->openapi->valid_input or return;
my $args = $c->validation->output;
my @options = ();
my $url = sprintf ('%s/organizers/%s/events/%s/',
$c->config->{pretix_api},
$c->config->{pretix_organizer},
$c->config->{pretix_event},
);
if ( $args->{type} eq 'products' ) {
$url .= 'items/';
}
if ( $args->{type} eq 'variations' ) {
$url .= 'items/' . $args->{product_id} . '/variations/';
}
elsif ( $args->{type} eq 'questions' ) {
$url .= 'questions/';
}
my $ua = Mojo::UserAgent->new;
my $records = $ua->get( $url, {
Authorization => 'Token ' . $c->config->{pretix_token}
} )->result->json;
RECORD:
foreach my $record ( @{ $records->{results} } ) {
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},
label => $record->{question}{en},
price => 0,
};
}
elsif ( $args->{type} eq 'variations' ) {
next RECORD if $record->{hidden};
push @options, {
value => $record->{id},
label => $record->{value}{en},
price => $record->{price} + 0,
};
}
}
$c->render(openapi => \@options);
}
1;
package CF2022::Controller::Products;
use Mojo::Base 'Mojolicious::Controller';
use Mojo::UserAgent;
sub list {
my $c = shift->openapi->valid_input or return;
my $url = sprintf ('%s/organizers/%s/events/%s/items/',
$c->config->{pretix_api},
$c->config->{pretix_organizer},
$c->config->{pretix_event},
);
my $ua = Mojo::UserAgent->new;
my $records = $ua->get( $url, {
Authorization => 'Token ' . $c->config->{pretix_token}
} )->result->json;
my @options = ();
RECORD:
foreach my $record ( @{ $records->{results} } ) {
next RECORD if ! $record->{active};
push @options, {
id => $record->{id},
name => $record->{name}{en},
price => $record->{default_price} + 0,
free_price => $record->{free_price},
category_id => $record->{category},
variations => [
map {{
id => $_->{id},
value => $_->{value}{en},
price => $_->{price} + 0
}} @{ $record->{variations} }
],
};
}
$c->render(openapi => \@options);
}
1;
package CF2022::Controller::Questions;
use Mojo::Base 'Mojolicious::Controller';
use Mojo::UserAgent;
sub list {
my $c = shift->openapi->valid_input or return;
my $url = sprintf ('%s/organizers/%s/events/%s/questions/',
$c->config->{pretix_api},
$c->config->{pretix_organizer},
$c->config->{pretix_event},
);
my $ua = Mojo::UserAgent->new;
my $records = $ua->get( $url, {
Authorization => 'Token ' . $c->config->{pretix_token}
} )->result->json;
my @options = ();
RECORD:
foreach my $record ( @{ $records->{results} } ) {
next RECORD if $record->{hidden};
push @options, {
id => $record->{id},
question => $record->{question}{en},
help => $record->{help_text}{en} // '',
};
}
$c->render(openapi => \@options);
}
1;
...@@ -22,69 +22,94 @@ servers: ...@@ -22,69 +22,94 @@ servers:
components: components:
schemas: schemas:
Response: Question:
type: object type: object
properties: properties:
request_id: id:
type: integer
question:
type: string type: string
help:
type: string
Response:
type: object
properties:
question_id:
type: integer
response: response:
type: string type: string
Product: Variation:
type: object type: object
properties: properties:
id: id:
type: integer type: integer
variation: value:
type: integer type: string
price: price:
type: integer type: integer
Option: Product:
type: object type: object
properties: properties:
value: id:
type: integer
category_id:
type: integer
name:
type: string type: string
label: description:
type: string type: string
variations:
type: array
items:
$ref: '#/components/schemas/Variation'
price:
type: integer
free_price:
type: boolean
ProductOrder:
type: object
properties:
id:
type: integer
variation:
type: integer
price: price:
type: integer type: integer
paths: paths:
/options/{type}/: /questions:
get: get:
x-mojo-to: lists#options x-mojo-to: questions#list
tags: tags:
- options - options
summary: "Options" summary: "Questions"
operationId: getOptions operationId: getQuestions
parameters: responses:
- name: type 200:
in: path description: Questions list
description: List ID content:
required: true application/json:
schema:
type: string
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: schema:
type: integer type: array
items:
$ref: '#/components/schemas/Question'
/products:
get:
x-mojo-to: products#list
tags:
- options
summary: "Products"
operationId: getProducts
responses: responses:
200: 200:
description: Options list description: Products list
content: content:
application/json: application/json:
schema: schema:
type: array type: array
items: items:
$ref: '#/components/schemas/Option' $ref: '#/components/schemas/Product'
/orders: /orders:
post: post:
...@@ -116,7 +141,7 @@ paths: ...@@ -116,7 +141,7 @@ paths:
products: products:
type: array type: array
items: items:
$ref: '#/components/schemas/Product' $ref: '#/components/schemas/ProductOrder'
responses: responses:
type: array type: array
items: items:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment