| 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 | 0 | public class Admin extends Controller { |
| 13 | |
|
| 14 | |
@Before |
| 15 | |
static void setConnectedUser() { |
| 16 | 0 | if(Security.isConnected()) { |
| 17 | 0 | User user = User.find("byEmail", Security.connected()).first(); |
| 18 | 0 | renderArgs.put("user", user.fullname); |
| 19 | |
} |
| 20 | 0 | } |
| 21 | |
|
| 22 | |
public static void index() { |
| 23 | 0 | List<Post> posts = Post.find("author.email", Security.connected()).fetch(); |
| 24 | 0 | render(posts); |
| 25 | 0 | } |
| 26 | |
|
| 27 | |
public static void form(Long id) { |
| 28 | 0 | if(id != null) { |
| 29 | 0 | Post post = Post.findById(id); |
| 30 | 0 | render(post); |
| 31 | |
} |
| 32 | 0 | render(); |
| 33 | 0 | } |
| 34 | |
|
| 35 | |
public static void save(Long id, String title, String content, String tags) { |
| 36 | |
Post post; |
| 37 | 0 | if(id == null) { |
| 38 | |
|
| 39 | 0 | User author = User.find("byEmail", Security.connected()).first(); |
| 40 | 0 | post = new Post(author, title, content); |
| 41 | |
} else { |
| 42 | |
|
| 43 | 0 | post = Post.findById(id); |
| 44 | 0 | post.title = title; |
| 45 | 0 | post.content = content; |
| 46 | 0 | post.tags.clear(); |
| 47 | |
} |
| 48 | |
|
| 49 | 0 | for(String tag : tags.split("\\s+")) { |
| 50 | 0 | if(tag.trim().length() > 0) { |
| 51 | 0 | post.tags.add(Tag.findOrCreateByName(tag)); |
| 52 | |
} |
| 53 | |
} |
| 54 | |
|
| 55 | 0 | validation.valid(post); |
| 56 | 0 | if(validation.hasErrors()) { |
| 57 | 0 | render("@form", post); |
| 58 | |
} |
| 59 | |
|
| 60 | 0 | post.save(); |
| 61 | 0 | index(); |
| 62 | 0 | } |
| 63 | |
|
| 64 | |
} |