[2077] | 1 | package ProjectBuilder::Controller::Conf;
|
---|
| 2 | use Mojo::Base 'Mojolicious::Controller';
|
---|
| 3 | use Data::Dumper;
|
---|
| 4 |
|
---|
| 5 | sub create { shift->stash(conf => {}) }
|
---|
| 6 |
|
---|
| 7 | sub edit {
|
---|
| 8 | my $self = shift; # Web request
|
---|
| 9 | $self->stash(conf => $self->confs->find($self->param('id')));
|
---|
| 10 | }
|
---|
| 11 |
|
---|
| 12 | sub list {
|
---|
| 13 | my ($self, $args, $cb) = @_;
|
---|
| 14 |
|
---|
| 15 | if ($cb) { # Swagger2 request
|
---|
| 16 | #print "SELF: ".Dumper($self)."\n";
|
---|
| 17 | #print "ARGS ".Dumper($args)."\n";
|
---|
| 18 | #print "CB ".Dumper($cb)."\n";
|
---|
| 19 | $self->$cb($self->confs->all, 200);
|
---|
| 20 | }
|
---|
| 21 | else { # Web request
|
---|
| 22 | $self->render(confs => $self->confs->all);
|
---|
| 23 | }
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | sub remove {
|
---|
| 27 | my ($self, $args, $cb) = @_;
|
---|
| 28 |
|
---|
| 29 | if ($cb) { # Swagger2 request
|
---|
| 30 | $self->confs->remove($args->{id});
|
---|
| 31 | $self->$cb({}, 200);
|
---|
| 32 | }
|
---|
| 33 | else { # Web request
|
---|
| 34 | $self->confs->remove($self->param('id'));
|
---|
| 35 | $self->redirect_to('confs');
|
---|
| 36 | }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | sub show {
|
---|
| 40 | my ($self, $args, $cb) = @_;
|
---|
| 41 |
|
---|
| 42 | if ($cb) { # Swagger2 request
|
---|
| 43 | my $entry = $self->confs->find($args->{id});
|
---|
| 44 | return $self->$cb($entry, 200) if $entry;
|
---|
| 45 | return $self->$cb({errors => [{message => 'ProjectBuilder conf not found.', path => '/id'}]}, 404);
|
---|
| 46 | }
|
---|
| 47 | else { # Web request
|
---|
| 48 | $self->render(conf => $self->confs->find($self->param('id')));
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | sub store {
|
---|
| 53 | my ($self, $args, $cb) = @_;
|
---|
| 54 | my $validation = $self->_validation($args->{entry});
|
---|
| 55 |
|
---|
| 56 | if ($cb) { # Swagger2 request
|
---|
| 57 | $args->{entry}{id} = int $self->confs->add($validation->output);
|
---|
| 58 | return $self->$cb($args->{entry}, 200);
|
---|
| 59 | }
|
---|
| 60 | else { # Web request
|
---|
| 61 | return $self->render(action => 'create', conf => {}) if $validation->has_error;
|
---|
| 62 | my $id = $self->confs->add($validation->output);
|
---|
| 63 | return $self->redirect_to('show_conf', id => $id);
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | sub update {
|
---|
| 68 | my ($self, $args, $cb) = @_;
|
---|
| 69 | my $validation = $self->_validation($args->{entry});
|
---|
| 70 |
|
---|
| 71 | if ($cb) { # Swagger2 request
|
---|
| 72 | $self->confs->save($args->{id}, $validation->output);
|
---|
| 73 | return $self->$cb({}, 200);
|
---|
| 74 | }
|
---|
| 75 | else { # Web request
|
---|
| 76 | return $self->render(action => 'edit', conf => {}) if $validation->has_error;
|
---|
| 77 | my $id = $self->param('id');
|
---|
| 78 | $self->confs->save($id, $validation->output);
|
---|
| 79 | $self->redirect_to('show_conf', id => $id);
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | sub _validation {
|
---|
| 84 | my ($self, $input) = @_;
|
---|
| 85 |
|
---|
| 86 | my $validation = $self->validation;
|
---|
| 87 | $validation->input($input) if $input;
|
---|
| 88 | $validation->required('title');
|
---|
| 89 | $validation->required('body');
|
---|
| 90 |
|
---|
| 91 | return $validation;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | 1;
|
---|