-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.pl
executable file
·308 lines (277 loc) · 8.9 KB
/
app.pl
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/lib";
use lib "$FindBin::Bin/local/lib/perl5";
use Mojolicious::Lite;
use Mojo::JSON;
use JSON;
use Data::Dumper;
use Try::Tiny;
use Wires::Schema;
use DBIx::Class::ResultClass::HashRefInflator;
# Development plugins
# Documentation browser under "/perldoc"
plugin 'PODRenderer';
#plugin 'ConsoleLogger';
# Deploy plugins
my $config = plugin 'JSONConfig';
helper schema => sub {
my $schema = Wires::Schema->connect(
$config->{'pg_dsn'}, $config->{'pg_user'},
$config->{'pg_pass'}, { pg_enable_utf8 => 1 }
);
return $schema;
};
get '/' => sub {
my $self = shift;
my $items = $self->schema->resultset( 'Item' )->get_items(undef,undef,undef,1);
#my $sources = $self->schema->resultset( 'Source' )->get_sources();
my $cats = $self->schema->resultset( 'Category' )->get_categories();
my $json
= JSON->new->allow_nonref; # Using JSON module for proper encoding
my $items_json = $json->encode( $items );
#my $sources_json = $json->encode( $sources );
my $cats_json = $json->encode( $cats );
#$self->app->log->debug( $sources_json );
#my $j = Mojo::JSON->new();
#my $items_json = $j->encode($items);
$self->stash(
{ items => $items_json,
#sources => $sources_json,
cats => $cats_json
}
);
$self->render( 'index' );
};
# Get feed items, with optional limit or page
get '/items' => sub {
my $self = shift;
my $category = $self->param( 'category' ) || undef;
my $hot = $self->param( 'hot' ) || undef;
my $page = $self->param( 'page' );
my $limit = $self->param( 'limit' );
my $items = $self->schema->resultset( 'Item' )
->get_items( $category, $page, $limit, $hot );
$self->respond_to(
json => sub {
$self->render( json => $items ), status => 200;
},
any => { text => '', status => 204 }
);
};
# Get one source by id
get '/items/:id' => sub { # Read one
my $self = shift;
my $rs = $self->schema->resultset( 'Item' );
my $id = $self->stash( 'id' );
my $item = $rs->get_item( $id );
$self->respond_to(
json => sub {
$self->render( json => $item ), status => 200;
},
any => { text => '', status => 204 }
);
};
# POST/CREATE item
post '/items' => sub { # Create
my $self = shift;
my $schema = $self->schema;
my $json = $self->req->json;
my $item = $schema->resultset( 'Item' )->create( $json );
if ( $item ) {
$self->res->code( 200 );
$self->render( json => { id => $item->id } );
}
else {
$self->res->code( 422 );
$self->render( json => { error => "Unable to create item." } );
}
};
any [ 'put', 'patch' ] => '/items/:id' => sub { # Update full or partial
my $self = shift;
my $id = $self->stash( 'id' );
my $schema = $self->schema;
my $json = $self->req->json;
my $item = $schema->resultset( 'Item' )->find( $id );
if ( $item ) {
my $result = $item->update( $json );
$self->res->code( 200 );
$self->render( json => { id => $result->id } );
}
else {
$self->res->code( 422 );
$self->render( json => { error => "Unable to update source." } );
}
};
any ['delete'] => '/items/:id' => sub { # Delete
my $self = shift;
my $id = $self->stash( 'id' );
my $schema = $self->schema;
my $item = $schema->resultset( 'Item' )->find( $id );
if ( $item ) {
my $result = $item->delete;
$self->res->code( 200 );
$self->render( json => { text => "Item $id deleted" } );
}
else {
$self->res->code( 422 );
$self->render( json => { error => "Unable to delete item." } );
}
};
# Read /sources with optional filter by category
get '/sources' => sub { # Read collection
my $self = shift;
my $rs = $self->schema->resultset( 'Source' );
my $category = $self->param( 'category' ) || undef;
my $page = $self->param( 'page' );
my $limit = $self->param( 'limit' );
my $sources = $rs->get_sources( $category, $page, $limit );
# Not using this currently...
my $json
= JSON->new->allow_nonref; # Using JSON module for proper encoding
my $sources_json = $json->encode( $sources );
$self->stash( sources_json => $sources_json );
$self->respond_to(
json => sub {
$self->render( json => $sources ), status => 200;
},
html => sub {
$self->render( 'sources' );
}
);
};
# Get one source by id
get '/sources/:id' => sub { # Read one
my $self = shift;
my $rs = $self->schema->resultset( 'Source' );
my $id = $self->stash( 'id' );
my $source = $rs->get_source( $id );
$self->respond_to(
json => sub {
$self->render( json => $source ), status => 200;
},
any => { text => '', status => 204 }
);
};
post '/sources' => sub { # Create
my $self = shift;
my $schema = $self->schema;
my $json = $self->req->json;
my $source = $schema->resultset( 'Source' )->create( $json );
if ( $source ) {
$self->res->code( 200 );
$self->render( json => { id => $source->id } );
}
else {
$self->res->code( 422 );
$self->render( json => { error => "Unable to create source." } );
}
};
any [ 'put', 'patch' ] => '/sources/:id' => sub { # Update full or partial
my $self = shift;
my $id = $self->stash( 'id' );
my $schema = $self->schema;
my $json = $self->req->json;
my $source = $schema->resultset( 'Source' )->find( $id );
if ( $source ) {
my $result = $source->update( $json );
$self->res->code( 200 );
$self->render( json => { id => $result->id } );
}
else {
$self->res->code( 422 );
$self->render( json => { error => "Unable to update source." } );
}
};
any ['delete'] => '/sources/:id' => sub { # Delete
my $self = shift;
my $id = $self->stash( 'id' );
my $schema = $self->schema;
my $source = $schema->resultset( 'Source' )->find( $id );
if ( $source ) {
my $result = $source->delete;
$self->res->code( 200 );
$self->render( json => { text => "Source $id deleted" } );
}
else {
$self->res->code( 422 );
$self->render( json => { error => "Unable to delete source." } );
}
};
# Read /categories
get '/categories' => sub { # Read collection
my $self = shift;
my $rs = $self->schema->resultset( 'Category' );
#my $category = $self->param( 'category' );
my $cats = $rs->get_categories();
my $json
= JSON->new->allow_nonref; # Using JSON module for proper encoding
my $cats_json = $json->encode( $cats );
$self->respond_to(
json => sub {
$self->render( json => $cats_json ), status => 200;
},
any => { text => 'No results', status => 204 }
);
};
# Get one source by id
get '/categories/:id' => sub { # Read one
my $self = shift;
#my $rs = $self->schema->resultset( 'Source' );
#my $id = $self->stash( 'id' );
#my $source = $rs->get_source( $id );
#$self->respond_to(
#json => sub {
#$self->render( json => $source ), status => 200;
#},
#any => { text => '', status => 204 }
#);
};
post '/categories' => sub { # Create
my $self = shift;
#my $schema = $self->schema;
#my $json = $self->req->json;
#my $source = $schema->resultset( 'Source' )->create( $json );
#if ( $source ) {
#$self->res->code( 200 );
#$self->render( json => { id => $source->id } );
#}
#else {
#$self->res->code( 422 );
#$self->render( json => { error => "Unable to create source." } );
#}
};
any [ 'put', 'patch' ] => '/categories/:id' => sub { # Update full or partial
my $self = shift;
my $id = $self->stash( 'id' );
my $schema = $self->schema;
my $json = $self->req->json;
my $category = $schema->resultset( 'Category' )->find( $id );
if ( $category ) {
my $result = $category->update( $json );
$self->res->code( 200 );
$self->render( json => { id => $result->id } );
} else {
$self->res->code( 422 );
$self->render( json => { error => "Unable to update category" } );
}
};
any ['delete'] => '/categories/:id' => sub { # Delete
my $self = shift;
#my $id = $self->stash( 'id' );
#my $schema = $self->schema;
#my $source = $schema->resultset( 'Source' )->find( $id );
#if ( $source ) {
#my $result = $source->delete;
#$self->res->code( 200 );
#$self->render( json => { text => "Source $id deleted" } );
#}
#else {
#$self->res->code( 422 );
#$self->render( json => { error => "Unable to delete source." } );
#}
};
app->secrets( [ $config->{'app_secret'} ] );
app->start;