Ejemplo n.º 1
0
 public static void save(Long id, String title, String content, String tags) {
   Post post;
   if (id == null) {
     // Create post
     User author = User.find("byEmail", Security.connected()).first();
     post = new Post(author, title, content);
   } else {
     // Retrieve post
     post = Post.findById(id);
     // Edit
     post.title = title;
     post.content = content;
     post.tags.clear();
   }
   // Set tags list
   for (String tag : tags.split("\\s+")) {
     if (tag.trim().length() > 0) {
       post.tags.add(Tag.findOrCreateByName(tag));
     }
   }
   // Validate
   validation.valid(post);
   if (validation.hasErrors()) {
     render("@form", post);
   }
   // Save
   post.save();
   index();
 }
Ejemplo n.º 2
0
  public static void deletePost(Long post_ID) throws SQLException {
    Post post = null;

    String deletePostStr =
        String.format("DELETE from %s where postid = ?", Post.dquote(POST), post_ID);

    Connection conn = null;
    PreparedStatement deleteP = null;

    if (post_ID != null) {
      try {
        conn = DB.getConnection();
        deleteP = conn.prepareStatement(deletePostStr);
        ResultSet rs = deleteP.executeQuery();

        if (rs.next()) {
          Logger.debug("Failed to delete the Post.");
        }
        deleteP.close();
        conn.close();
      } catch (SQLException e) {
        Logger.debug("Failed while trying to delete the post.");
      }
    } else {
      Logger.debug("Post id is null.");
    }
  }
Ejemplo n.º 3
0
  /**
   * FindById Searches the database for a Group object by the object id.
   *
   * @param connection The jdbc connection
   * @param id The id to select by
   */
  public static Post FindByID(Long post_ID) throws SQLException {
    Post post = null;
    PreparedStatement findPostId = null;
    Connection conn = null;

    String sql = String.format("SELECT * from %s where postid = ?", Post.dquote(POST), post_ID);

    if (post_ID != null) {
      try {
        conn = DB.getConnection();
        findPostId = conn.prepareStatement(sql);
        findPostId.setLong(1, post_ID);

        ResultSet rs = findPostId.executeQuery();

        if (rs.next()) {
          post =
              new Post(
                  rs.getString(Post.CONTENT),
                  rs.getString(Post.TYPE),
                  rs.getTimestamp(Post.TIMESTAMP),
                  rs.getLong(Post.USER_ID),
                  rs.getLong(Post.WALL_ID),
                  rs.getLong(Post.POST_ID));
        }

        findPostId.close();
        conn.close();
        return post;
      } catch (SQLException e) {
        Logger.debug("Error retrieving post.", e);
      }
    }
    return post;
  }
Ejemplo n.º 4
0
 public static void form(Long id) {
   if (id != null) {
     Post post = Post.findById(id);
     render(post);
   }
   render();
 }
Ejemplo n.º 5
0
  public static void postComment(
      Long postId,
      @Required(message = "Author is required") String author,
      @Required(message = "A message is required") String content,
      @Required(message = "Please type the code") String code,
      String randomID) {
    Post post = Post.findById(postId);

    if (!Play.id.equals("test")) {
      validation.equals(code, Cache.get(randomID)).message("Invalid code. Please type it again");
    }
    if (validation.hasErrors()) {
      render("Application/show.html", post);
    }

    post.addComment(author, content);
    flash.success("Thanks for posting %s", author);
    Cache.delete(randomID);
    show(postId);
  }
Ejemplo n.º 6
0
 public static void listTagged(String tag) {
   List<Post> posts = Post.findTaggedWith(tag);
   render(tag, posts);
 }
Ejemplo n.º 7
0
 public static void show(Long id) {
   Post post = Post.findById(id);
   String randomID = Codec.UUID();
   render(post, randomID);
 }
Ejemplo n.º 8
0
 public static void index() {
   Post frontPost = Post.find("order by postedAt desc").first();
   List<Post> olderPosts = Post.find("order by postedAt desc").from(1).fetch(10);
   render(frontPost, olderPosts);
 }
Ejemplo n.º 9
0
 public static void index() {
   String user = Security.connected();
   List<Post> posts = Post.find("author.email", user).fetch();
   render(posts);
 }