File: Blog.pm

package info (click to toggle)
libmojo-sqlite-perl 3.009-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 324 kB
  • sloc: perl: 610; sql: 11; makefile: 6
file content (35 lines) | stat: -rw-r--r-- 1,151 bytes parent folder | download | duplicates (3)
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
package Blog;
use Mojo::Base 'Mojolicious';

use Blog::Model::Posts;
use Mojo::SQLite;

sub startup {
  my $self = shift;

  # Configuration
  $self->plugin('Config');
  $self->secrets($self->config('secrets'));

  # Model
  $self->helper(sqlite => sub { state $sql = Mojo::SQLite->new->from_filename(shift->config('sqlite')) });
  $self->helper(
    posts => sub { state $posts = Blog::Model::Posts->new(sqlite => shift->sqlite) });

  # Migrate to latest version if necessary
  my $path = $self->home->child('migrations', 'blog.sql');
  $self->sqlite->auto_migrate(1)->migrations->name('blog')->from_file($path);

  # Controller
  my $r = $self->routes;
  $r->get('/' => sub { shift->redirect_to('posts') });
  $r->get('/posts')->to('posts#index');
  $r->get('/posts/create')->to('posts#create')->name('create_post');
  $r->post('/posts')->to('posts#store')->name('store_post');
  $r->get('/posts/:id')->to('posts#show')->name('show_post');
  $r->get('/posts/:id/edit')->to('posts#edit')->name('edit_post');
  $r->put('/posts/:id')->to('posts#update')->name('update_post');
  $r->delete('/posts/:id')->to('posts#remove')->name('remove_post');
}

1;