burguillos.info/lib/BurguillosInfo/Controller/ConquerTeam.pm

58 lines
1.4 KiB
Perl

package BurguillosInfo::Controller::ConquerTeam;
use v5.34.1;
use strict;
use warnings;
use utf8;
use Mojo::Base 'Mojolicious::Controller', '-signatures';
use UUID::URandom qw/create_uuid_string/;
use JSON;
use BurguillosInfo::Schema;
sub get($self) {
my $user = $self->current_user;
if (!defined $user) {
return $self->render(status => 401, json => {
error => 'You must be logged to fetch a team.',
});
}
my $uuid = $self->param('uuid');
my $resultset = BurguillosInfo::Schema->Schema->resultset('ConquerTeam');
my @teams = $resultset->search({
'uuid' => $uuid,
});
if (scalar @teams <= 0) {
return $self->render( status => 404, json => {
error => 'This team does not exist.',
});
}
my $team = $teams[0];
return $self->render(json => $team);
}
sub getSelfTeam($self) {
my $user = $self->current_user;
if (!defined $user) {
return $self->render(status => 401, json => {
error => 'You must be logged to fetch your Team.',
});
}
my $resultset = BurguillosInfo::Schema->Schema->resultset('ConquerTeam');
my @teams = $resultset->search({
'players.uuid' => $user->uuid
}, {
join => 'players',
});
if (scalar @teams <= 0) {
return $self->render(json => undef);
}
my $team = $teams[0];
return $self->render(json => $team);
}
1;