-
-
Save anonymous/9ad82499d9a85a8d2b8e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/examples/blog/lib/Blog.pm b/examples/blog/lib/Blog.pm | |
index e69de29..14ebf14 100644 | |
--- a/examples/blog/lib/Blog.pm | |
+++ b/examples/blog/lib/Blog.pm | |
@@ -0,0 +1,27 @@ | |
+package Blog; | |
+use Mojo::Base 'Mojolicious'; | |
+ | |
+use Blog::Model::Posts; | |
+ | |
+sub startup { | |
+ my $self = shift; | |
+ | |
+ # Configuration | |
+ $self->secrets(['s3cret']); | |
+ | |
+ # Model | |
+ $self->helper(posts => sub { state $posts = Blog::Model::Posts->new }); | |
+ | |
+ # 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; | |
diff --git a/examples/blog/lib/Blog/Controller/Posts.pm b/examples/blog/lib/Blog/Controller/Posts.pm | |
index e69de29..79844ee 100644 | |
--- a/examples/blog/lib/Blog/Controller/Posts.pm | |
+++ b/examples/blog/lib/Blog/Controller/Posts.pm | |
@@ -0,0 +1,58 @@ | |
+package Blog::Controller::Posts; | |
+use Mojo::Base 'Mojolicious::Controller'; | |
+ | |
+sub create { shift->stash(post => {}) } | |
+ | |
+sub edit { | |
+ my $self = shift; | |
+ $self->stash(post => $self->posts->find($self->param('id'))); | |
+} | |
+ | |
+sub index { | |
+ my $self = shift; | |
+ $self->stash(posts => $self->posts->all); | |
+} | |
+ | |
+sub remove { | |
+ my $self = shift; | |
+ $self->posts->withdraw($self->param('id')); | |
+ $self->redirect_to('posts'); | |
+} | |
+ | |
+sub show { | |
+ my $self = shift; | |
+ $self->stash(post => $self->posts->find($self->param('id'))); | |
+} | |
+ | |
+sub store { | |
+ my $self = shift; | |
+ | |
+ my $validation = $self->_validation; | |
+ return $self->render('posts/create', post => {}) if $validation->has_error; | |
+ | |
+ my $id = $self->posts->publish($validation->output); | |
+ $self->redirect_to('show_post', id => $id); | |
+} | |
+ | |
+sub update { | |
+ my $self = shift; | |
+ | |
+ my $validation = $self->_validation; | |
+ return $self->render('posts/edit', post => {}) if $validation->has_error; | |
+ | |
+ my $id = $self->param('id'); | |
+ $self->posts->revise($id, $validation->output); | |
+ $self->redirect_to('show_post', id => $id); | |
+} | |
+ | |
+sub _validation { | |
+ my $self = shift; | |
+ | |
+ my $validation = $self->validation; | |
+ $validation->required('title'); | |
+ $validation->required('body'); | |
+ | |
+ return $validation; | |
+} | |
+ | |
+1; | |
diff --git a/examples/blog/lib/Blog/Model/Posts.pm b/examples/blog/lib/Blog/Model/Posts.pm | |
index e69de29..16606d4 100644 | |
--- a/examples/blog/lib/Blog/Model/Posts.pm | |
+++ b/examples/blog/lib/Blog/Model/Posts.pm | |
@@ -0,0 +1,25 @@ | |
+package Blog::Model::Posts; | |
+use Mojo::Base -base; | |
+ | |
+sub all { [values %{shift->_posts}] } | |
+ | |
+sub find { shift->_posts->{shift()} } | |
+ | |
+sub publish { | |
+ my ($self, $post) = @_; | |
+ my $id = $post->{id} = ++$self->{last}; | |
+ $self->_posts->{$id} = $post; | |
+ return $id; | |
+} | |
+ | |
+sub revise { | |
+ my ($self, $id, $post) = @_; | |
+ $post->{id} = $id; | |
+ $self->_posts->{$id} = $post; | |
+} | |
+ | |
+sub withdraw { delete shift->_posts->{shift()} } | |
+ | |
+sub _posts { shift->{posts} ||= {} } | |
+ | |
+1; | |
diff --git a/examples/blog/script/blog b/examples/blog/script/blog | |
index e69de29..1f04408 100755 | |
--- a/examples/blog/script/blog | |
+++ b/examples/blog/script/blog | |
@@ -0,0 +1,10 @@ | |
+#!/usr/bin/env perl | |
+ | |
+use strict; | |
+use warnings; | |
+ | |
+use lib 'lib'; | |
+ | |
+# Start command line interface for application | |
+require Mojolicious::Commands; | |
+Mojolicious::Commands->start_app('Blog'); | |
diff --git a/examples/blog/templates/layouts/blog.html.ep b/examples/blog/templates/layouts/blog.html.ep | |
index e69de29..47a583e 100644 | |
--- a/examples/blog/templates/layouts/blog.html.ep | |
+++ b/examples/blog/templates/layouts/blog.html.ep | |
@@ -0,0 +1,19 @@ | |
+<!DOCTYPE html> | |
+<html> | |
+ <head> | |
+ <title><%= title %></title> | |
+ <style> | |
+ a, body { color: #2a2a2a } | |
+ body { font: 0.9em 'Helvetica Neue', Helvetica, sans-serif } | |
+ input[type=text], textarea { width: 600px } | |
+ input.field-with-error, textarea.field-with-error { | |
+ border: 4px solid #f00; | |
+ } | |
+ textarea { height: 300px } | |
+ </style> | |
+ </head> | |
+ <body> | |
+ <h1><%= link_to 'Blog' => 'posts' %></h1> | |
+ %= content | |
+ </body> | |
+</html> | |
diff --git a/examples/blog/templates/posts/_form.html.ep b/examples/blog/templates/posts/_form.html.ep | |
index e69de29..dec6330 100644 | |
--- a/examples/blog/templates/posts/_form.html.ep | |
+++ b/examples/blog/templates/posts/_form.html.ep | |
@@ -0,0 +1,11 @@ | |
+%= form_for $target => begin | |
+ %= label_for title => 'Title' | |
+ <br> | |
+ %= text_field title => $post->{title} | |
+ <br> | |
+ %= label_for body => 'Body' | |
+ <br> | |
+ %= text_area body => $post->{body} | |
+ <br> | |
+ %= submit_button $description | |
+% end | |
diff --git a/examples/blog/templates/posts/create.html.ep b/examples/blog/templates/posts/create.html.ep | |
index e69de29..ddeaef1 100644 | |
--- a/examples/blog/templates/posts/create.html.ep | |
+++ b/examples/blog/templates/posts/create.html.ep | |
@@ -0,0 +1,3 @@ | |
+% layout 'blog', title => 'New post'; | |
+<h2>New post</h2> | |
+%= include 'posts/_form', target => 'store_post', description => 'Create post' | |
diff --git a/examples/blog/templates/posts/edit.html.ep b/examples/blog/templates/posts/edit.html.ep | |
index e69de29..7a85265 100644 | |
--- a/examples/blog/templates/posts/edit.html.ep | |
+++ b/examples/blog/templates/posts/edit.html.ep | |
@@ -0,0 +1,3 @@ | |
+% layout 'blog', title => 'Edit post'; | |
+<h2>Edit post</h2> | |
+%= include 'posts/_form', target => 'update_post', description => 'Update post' | |
diff --git a/examples/blog/templates/posts/index.html.ep b/examples/blog/templates/posts/index.html.ep | |
index e69de29..fa91f67 100644 | |
--- a/examples/blog/templates/posts/index.html.ep | |
+++ b/examples/blog/templates/posts/index.html.ep | |
@@ -0,0 +1,8 @@ | |
+% layout 'blog', title => 'Blog'; | |
+% for my $post (@$posts) { | |
+ <p> | |
+ <h2><%= link_to $post->{title} => show_post => {id => $post->{id}} %></h2> | |
+ %= $post->{body} | |
+ </p> | |
+% } | |
+%= button_to 'New post' => 'create_post' | |
diff --git a/examples/blog/templates/posts/show.html.ep b/examples/blog/templates/posts/show.html.ep | |
index e69de29..12c1842 100644 | |
--- a/examples/blog/templates/posts/show.html.ep | |
+++ b/examples/blog/templates/posts/show.html.ep | |
@@ -0,0 +1,5 @@ | |
+% layout 'blog', title => $post->{title}; | |
+<h2><%= $post->{title} %></h2> | |
+<p><%= $post->{body} %></p> | |
+%= button_to 'Edit post' => edit_post => {id => $post->{id}} | |
+%= button_to 'Remove post' => remove_post => {id => $post->{id}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment