~peter-kovac11/play/play-scala-console-improvements

« back to all changes in this revision

Viewing changes to samples-and-tests/yabe-with-scala/app/controllers/Admin.java

  • Committer: guillaume
  • Date: 2009-11-11 17:19:54 UTC
  • Revision ID: guillaume@macbook-pro-de-guillaume.local-20091111171954-1z761dqr0aw93747
yabe-with-scala

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package controllers;
2
 
 
3
 
import play.*;
4
 
import play.mvc.*;
5
 
import play.data.validation.*;
6
 
 
7
 
import java.util.*;
8
 
 
9
 
import models.*;
10
 
 
11
 
@With(Secure.class)
12
 
public class Admin extends Controller {
13
 
    
14
 
    @Before
15
 
    static void setConnectedUser() {
16
 
        if(Security.isConnected()) {
17
 
            User user = User.find("byEmail", Security.connected()).first();
18
 
            renderArgs.put("user", user.fullname);
19
 
        }
20
 
    }
21
 
 
22
 
    public static void index() {
23
 
        List<Post> posts = Post.find("author.email", Security.connected()).fetch();
24
 
        render(posts);
25
 
    }
26
 
    
27
 
    public static void form(Long id) {
28
 
        if(id != null) {
29
 
            Post post = Post.findById(id);
30
 
            render(post);
31
 
        }
32
 
        render();
33
 
    }
34
 
    
35
 
    public static void save(Long id, String title, String content, String tags) {
36
 
        Post post;
37
 
        if(id == null) {
38
 
            // Create post
39
 
            User author = User.find("byEmail", Security.connected()).first();
40
 
            post = new Post(author, title, content);
41
 
        } else {
42
 
            // Retrieve post
43
 
            post = Post.findById(id);
44
 
            post.title = title;
45
 
            post.content = content;
46
 
            post.tags.clear();
47
 
        }
48
 
        // Set tags list
49
 
        for(String tag : tags.split("\\s+")) {
50
 
            if(tag.trim().length() > 0) {
51
 
                post.tags.add(Tag.findOrCreateByName(tag));
52
 
            }
53
 
        }
54
 
        // Validate
55
 
        validation.valid(post);
56
 
        if(validation.hasErrors()) {
57
 
            render("@form", post);
58
 
        }
59
 
        // Save
60
 
        post.save();
61
 
        index();
62
 
    }
63
 
    
64
 
}