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
variables:
DOCKER_TLS_CERTDIR: "/certs"
IMAGE_VER: 0.2.0
IMAGE_VER: 0.3.0
services:
- 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:
components:
schemas:
Response:
Question:
type: object
properties:
request_id:
id:
type: integer
question:
type: string
help:
type: string
Response:
type: object
properties:
question_id:
type: integer
response:
type: string
Product:
Variation:
type: object
properties:
id:
type: integer
variation:
type: integer
value:
type: string
price:
type: integer
Option:
Product:
type: object
properties:
value:
id:
type: integer
category_id:
type: integer
name:
type: string
label:
description:
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:
type: integer
paths:
/options/{type}/:
/questions:
get:
x-mojo-to: questions#list
tags:
- options
summary: "Questions"
operationId: getQuestions
responses:
200:
description: Questions list
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Question'
/products:
get:
x-mojo-to: lists#options
x-mojo-to: products#list
tags:
- options
summary: "Options"
operationId: getOptions
parameters:
- name: type
in: path
description: List ID
required: true
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:
type: integer
summary: "Products"
operationId: getProducts
responses:
200:
description: Options list
description: Products list
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Option'
$ref: '#/components/schemas/Product'
/orders:
post:
......@@ -116,7 +141,7 @@ paths:
products:
type: array
items:
$ref: '#/components/schemas/Product'
$ref: '#/components/schemas/ProductOrder'
responses:
type: array
items:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment