186 lines
5.1 KiB
Perl
186 lines
5.1 KiB
Perl
package BurguillosInfo::Controller::ConquerNode;
|
|
|
|
use v5.34.1;
|
|
|
|
use strict;
|
|
use warnings;
|
|
|
|
use utf8;
|
|
|
|
use Mojo::Base 'Mojolicious::Controller', '-signatures';
|
|
use UUID::URandom qw/create_uuid_string/;
|
|
use BurguillosInfo::Schema;
|
|
|
|
sub get($self) {
|
|
my $uuid = $self->param('uuid');
|
|
my $user = $self->current_user;
|
|
if (!defined $uuid || !$uuid) {
|
|
return $self->render(status => 400, json => {
|
|
error => 'UUID de nodo invalido.',
|
|
});
|
|
}
|
|
my $schema = BurguillosInfo::Schema->Schema->resultset('ConquerNode');
|
|
my @nodes = $schema->search({uuid => $uuid});
|
|
if (!scalar @nodes) {
|
|
return $self->render(status => 404, json => {
|
|
error => 'Nodo no encontrado',
|
|
});
|
|
}
|
|
my $node = $nodes[0];
|
|
if (defined $user) {
|
|
return $self->render(json => $node->serialize($user));
|
|
}
|
|
return $self->render(json => $node->serialize());
|
|
}
|
|
|
|
sub tryConquer($self) {
|
|
my $user = $self->current_user;
|
|
my $schema = BurguillosInfo::Schema->Schema->resultset('ConquerNode');
|
|
if (!defined $user) {
|
|
return $self->render(status => 401, json => {
|
|
error => 'You must be logged to conquer a node.',
|
|
});
|
|
}
|
|
if (!defined $user->team) {
|
|
return $self->render(status => 400, json => {
|
|
error => 'You must belong to a team to conquer a node.',
|
|
});
|
|
}
|
|
my $uuid = $self->param('uuid');
|
|
my ($node) = $schema->search({uuid => $uuid});
|
|
if (!defined $node) {
|
|
return $self->render(status => 404, json => {
|
|
error => 'No existe ese nodo.',
|
|
});
|
|
}
|
|
$node->team($user->team);
|
|
$node->update;
|
|
$self->render(json => {
|
|
ok => $JSON::true,
|
|
});
|
|
}
|
|
|
|
sub create ($self) {
|
|
my $user = $self->current_user;
|
|
if ( !defined $user ) {
|
|
return $self->render(
|
|
status => 401,
|
|
json => {
|
|
error => 'No estás autenticado.',
|
|
}
|
|
);
|
|
}
|
|
if ( !$user->is_admin ) {
|
|
return $self->render(
|
|
status => 403,
|
|
json => {
|
|
error => 'No tienes permiso para hacer eso.',
|
|
}
|
|
);
|
|
}
|
|
my $input = $self->_expectJson;
|
|
if ( !defined $input ) {
|
|
return;
|
|
}
|
|
my $name = $input->{name};
|
|
my $coordinates = $input->{coordinates};
|
|
my $type = $input->{type};
|
|
my $description = $input->{description};
|
|
if ( ref $coordinates ne 'ARRAY' || scalar $coordinates->@* != 2 ) {
|
|
return $self->render(
|
|
status => 400,
|
|
json => {
|
|
error => 'Formato erroneo de coordenadas.',
|
|
}
|
|
);
|
|
}
|
|
my ($coordinate_1, $coordinate_2) = $coordinates->@*;
|
|
if ( !defined $name || length $name < 5 ) {
|
|
return $self->render(
|
|
status => 400,
|
|
json => {
|
|
error =>
|
|
'Número incorrecto de carácteres en el nombre del nodo.',
|
|
}
|
|
);
|
|
}
|
|
if ( !defined $description ) {
|
|
return $self->render(
|
|
status => 400,
|
|
json => {
|
|
error => 'La descripción puede estar vacía, '
|
|
. 'pero debe existir, si ves este error '
|
|
. 'desde la aplicación es un error de programación.',
|
|
}
|
|
);
|
|
}
|
|
if ( !defined $type ) {
|
|
return $self->render(
|
|
status => 400,
|
|
json => {
|
|
error => 'Los nodos deben tener un tipo.'
|
|
}
|
|
);
|
|
}
|
|
if ( $type ne 'normal' ) {
|
|
return $self->render(
|
|
status => 400,
|
|
json => {
|
|
error => 'Tipo de nodo no soportado.',
|
|
}
|
|
);
|
|
}
|
|
my $uuid_node = create_uuid_string();
|
|
my $node;
|
|
eval {
|
|
$node = BurguillosInfo::Schema->Schema->resultset('ConquerNode')->new(
|
|
{
|
|
uuid => $uuid_node,
|
|
description => $description,
|
|
name => $name,
|
|
type => $type,
|
|
geometry => \['ST_MakePoint(?, ?)', $coordinate_1, $coordinate_2],
|
|
}
|
|
);
|
|
$node->insert;
|
|
$node = $node->get_from_storage;
|
|
};
|
|
if ($@) {
|
|
warn $@;
|
|
return $self->render(
|
|
status => 500,
|
|
json => {
|
|
error => 'El servidor no pudo almacenar el nodo, reporta este error.',
|
|
}
|
|
);
|
|
}
|
|
return $self->render(
|
|
status => 200,
|
|
json => $node->serialize,
|
|
);
|
|
}
|
|
|
|
sub nearbyNodes($self) {
|
|
my $user = $self->current_user;
|
|
if (!defined $user) {
|
|
return $self->render(status => 401, json => {
|
|
error => 'No estás loggeado.',
|
|
});
|
|
}
|
|
my @nodes = BurguillosInfo::Schema->Schema->resultset('ConquerNode')->search({});
|
|
@nodes = map { $_->serialize($user) } @nodes;
|
|
return $self->render(json => \@nodes);
|
|
}
|
|
|
|
sub _expectJson ($self) {
|
|
my $input;
|
|
eval { $input = $self->req->json; };
|
|
if ($@) {
|
|
say STDERR $@;
|
|
$self->_renderError( 400, 'Se esperaba JSON.' );
|
|
return;
|
|
}
|
|
return $input;
|
|
}
|
|
1;
|