-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathExterm.pm
98 lines (86 loc) · 3.11 KB
/
Exterm.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
=head1 NAME
Tuba::Exterm - Controller for external terms.
=head1 DESCRIPTION
This controller manages the mapping between external terms and GCIDs.
=cut
package Tuba::Exterm;
use Mojo::Base qw/Tuba::Controller/;
use Tuba::DB::Objects qw/-nicknames/;
# create allows updates too; it just assigns lexicon/context/term to a GCID.
sub create {
my $c = shift;
$c->stash(tab => 'create_form');
my $lexicon = $c->stash('lexicon_identifier');
my %entry;
my $term;
# PUT uses the URL (stash), POST uses the payload (json)
if (my $json = $c->req->json) {
%entry = (
term => $json->{term} // $c->stash('term'),
context => $json->{context} // $c->stash('context'),
lexicon_identifier => $lexicon,
);
$term = Exterm->new(%entry);
$term->load(speculative => 1);
$term->gcid($json->{gcid});
} else {
# NB: forms are handled through lexicon/rel
return $c->update_error("missing JSON to add exterm");
}
$term->save(audit_user => $c->audit_user, audit_note => $c->audit_note) or return $c->update_error($term->error);
$c->stash(_this_object => $term);
return $c->redirect_without_error('create_form');
}
sub find {
my $c = shift;
my $term = Exterm->new(
lexicon_identifier => $c->stash('lexicon_identifier'),
context => $c->stash('context'),
term => ( $c->stash('term') =~ s/\.t(tl|html)$//r ),
);
$term->load(speculative => 1) or return $c->reply->not_found;
my $gcid = $term->gcid;
$c->res->headers->location($gcid);
$c->respond_to(
html => { code => 303, text => qq[See <a href="$gcid">$gcid</a>.]},
json => { code => 303, json => { gcid => $gcid } }
);
$c->rendered(303);
}
sub remove {
my $c = shift;
my $term = Exterm->new(
lexicon_identifier => $c->stash('lexicon_identifier'),
context => $c->stash('context'),
term => $c->stash('term'),
);
$term->load(speculative => 1) or return $c->reply->not_found;
$term->delete(audit_user => $c->user, audit_note => $c->audit_note) or return $c->reply->exception($term->error);
return $c->render(text => 'ok');
}
sub list_context {
my $c = shift;
my $lexicon_identifier = $c->stash('lexicon_identifier');
my $context = $c->stash('context');
my $terms = Exterms->get_objects(
query => [ lexicon_identifier => $lexicon_identifier, context => $context ],
sort_by => 'term',
);
$c->respond_to(
json => sub {
my $c = shift;
$c->render(json => [ map +{ term => $_->term, gcid => $_->gcid }, @$terms ])
},
yaml => sub {
my $c = shift;
$c->render_yaml([ map +{ term => $_->term, gcid => $_->gcid }, @$terms ])
},
html => sub {
my $c = shift;
$c->res->headers->content_type('text/plain');
$c->render(text => join "\n",
map sprintf("%40s : %s",$_->term, $_->gcid), @$terms)
},
);
}
1;