I am creating a simple blog web application using play framework, where there are certain categories and users can post to the category they want, however the problem is that when i post to one category the post is posted to all categories as well.
Application code:
import java.util.*;
import models.*;
public class Application extends Controller {
public static void index() {
List Posts = Post.find ("order by type desc").fetch();
render(Posts);
}
public static void addpost (String content) {
Post post = new Post(content).save();
renderJSON(content);
html code:
<h2>
CollabBlog
</h2>
#{extends 'main.html' /}
#{set title:'Home' /}
<head>
<title>#{get 'title' /}</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" type="text/css" media="screen"
href="@{'/public/stylesheets/categories.css'}" />
</head>
<h3>Category: Health</h3>
#{ifnot Posts}
No posts to view
#{/ifnot}
<ul>
#{list items:Posts, as: 'Post'}
<li>
<p>${Post.poster}</p><p>${Post.content}</p>
</li>
#{/list}
</ul>
<p>
<a id="addpost" href="#">add new Health's post</a>
</p>
<h3>Category: Politics</h3>
#{ifnot Posts}
No posts to view
#{/ifnot}
<ul>
#{list items:Posts, as: 'Post'}
<li>
<p>${Post.poster}:</p><p>${Post.content}</p>
</li>
#{/list}
</ul>
<p>
<a id="addpost" href="#">add new Politic's post</a>
</p>
<h3>Category: Entertainment</h3>
#{ifnot Posts}
No posts to view
#{/ifnot}
<ul>
#{list items:Posts, as: 'Post'}
<li>
<p>${Post.poster}</p><p>${Post.content}</p>
</li>
#{/list}
</ul>
<p>
<a id="addpost" href="#">add new Entertainment's post</a>
</p>
<body>
<link href="index.css" rel="stylesheet" type="text/css">
</body>
<script type="text/javascript" charset="utf-8">
$('#addpost').click(function() {
$.post('@{addpost()}', {content: prompt('post content ?')}, function(Post){
$('ul').prepend()
})
$.post('@{addpost()}', {poster : prompt('which category ?')}, function(Post){
$('ul').prepend()
})
})
</script>
}
}
anyone knows how to solve this problem?
After reading your additional comments (and hopefully I haven’t misunderstood), what you could do in your controller is find all unique categories in your
typecolumn. Then for each type (as represented by thecategoriesvariable in the code snippet below) you could make this call:And then in your view, you can go over the keys of the map which will represent all the category names and then you get the value associated with the key and iterate over the posts for that category.
The above assumes that the user can type in anything they want. If you actually want them to select just either Health, Politics and Entertainment as per your view example in your original post, then maybe you need to model categories in your database. Some options could be:
First Option
You could have two entities: (1) Post and (2) Category. The first you have. The second you will need to create. So create a Category entity like this:
Then in your Post entity you can have a relationship between the Post and Category entities like this:
This would be good if a user (or you) could create categories dynamically (or directly in the database) to allow users to select pre-defined categories.
Second Option
If the above example is overkill because category is simply a word and doesn’t have other properties like description, then you could just use an enum for category:
Where Category is an enum like this:
Again this option means that the user would be selecting from a pre-defined list.