1 | |
package controllers; |
2 | |
|
3 | |
import play.*; |
4 | |
import play.mvc.*; |
5 | |
import play.data.validation.*; |
6 | |
import play.libs.*; |
7 | |
import play.cache.*; |
8 | |
|
9 | |
import java.util.*; |
10 | |
import models.*; |
11 | |
|
12 | 0 | public class Application extends Controller { |
13 | |
|
14 | |
@Before |
15 | |
static void addDefaults() { |
16 | 0 | renderArgs.put("blogTitle", Play.configuration.getProperty("blog.title")); |
17 | 0 | renderArgs.put("blogBaseline", Play.configuration.getProperty("blog.baseline")); |
18 | 0 | } |
19 | |
|
20 | |
public static void index() { |
21 | 0 | Post frontPost = Post.find("order by postedAt desc").first(); |
22 | 0 | List<Post> olderPosts = Post.find("order by postedAt desc").from(1).fetch(10); |
23 | 0 | render(frontPost, olderPosts); |
24 | 0 | } |
25 | |
|
26 | |
public static void show(Long id) { |
27 | 0 | Post post = Post.findById(id); |
28 | 0 | String randomID = Codec.UUID(); |
29 | 0 | render(post, randomID); |
30 | 0 | } |
31 | |
|
32 | |
public static void postComment( |
33 | |
Long postId, |
34 | |
@Required(message="Author is required") String author, |
35 | |
@Required(message="A message is required") String content, |
36 | |
@Required(message="Please type the code") String code, |
37 | |
String randomID) |
38 | |
{ |
39 | 0 | Post post = Post.findById(postId); |
40 | 0 | if(!Play.id.equals("test")) { |
41 | 0 | validation.equals(code, Cache.get(randomID)).message("Invalid code. Please type it again"); |
42 | |
} |
43 | 0 | if(validation.hasErrors()) { |
44 | 0 | render("Application/show.html", post, randomID); |
45 | |
} |
46 | 0 | post.addComment(author, content); |
47 | 0 | flash.success("Thanks for posting %s", author); |
48 | 0 | Cache.delete(randomID); |
49 | 0 | show(postId); |
50 | 0 | } |
51 | |
|
52 | |
public static void captcha(String id) { |
53 | 0 | Images.Captcha captcha = Images.captcha(); |
54 | 0 | String code = captcha.getText("#E4EAFD"); |
55 | 0 | Cache.set(id, code, "30mn"); |
56 | 0 | renderBinary(captcha); |
57 | 0 | } |
58 | |
|
59 | |
public static void listTagged(String tag) { |
60 | 0 | List<Post> posts = Post.findTaggedWith(tag); |
61 | 0 | render(tag, posts); |
62 | 0 | } |
63 | |
|
64 | |
} |