Get rating mongodb
This commit is contained in:
parent
fc31eb97d4
commit
fd1b24d35e
@ -31,6 +31,8 @@ import org.neo4j.driver.v1.Record;
|
|||||||
import org.neo4j.driver.v1.Session;
|
import org.neo4j.driver.v1.Session;
|
||||||
import org.neo4j.driver.v1.StatementResult;
|
import org.neo4j.driver.v1.StatementResult;
|
||||||
import com.camillepradel.movierecommender.model.Rating;
|
import com.camillepradel.movierecommender.model.Rating;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class MainController {
|
public class MainController {
|
||||||
@ -145,6 +147,51 @@ public class MainController {
|
|||||||
return listMovies;
|
return listMovies;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public List<Rating>getRatingMongoDBByUser(Integer user_id){
|
||||||
|
ArrayList<Rating> listRating= new ArrayList<Rating>();
|
||||||
|
MongoCursor<Document> cursor;
|
||||||
|
|
||||||
|
MongoDatabase db = MongoDBConnector.getInstance().getConnexion();
|
||||||
|
MongoCollection<Document> users = db.getCollection("users");
|
||||||
|
MongoCollection<Document> movies = db.getCollection("movies");
|
||||||
|
|
||||||
|
BasicDBObject whereQuery = new BasicDBObject();
|
||||||
|
whereQuery.put("_id", user_id);
|
||||||
|
|
||||||
|
cursor = users.find(whereQuery).iterator();
|
||||||
|
Document user;
|
||||||
|
try {
|
||||||
|
user = cursor.next();
|
||||||
|
} catch(Exception e)
|
||||||
|
{
|
||||||
|
return new ArrayList<Rating>();
|
||||||
|
}
|
||||||
|
|
||||||
|
ArrayList<Document> user_movies = (ArrayList) user.get("movies");
|
||||||
|
BasicDBObject inQuery = new BasicDBObject();
|
||||||
|
HashMap<Integer,Integer> list = new HashMap<Integer,Integer>();
|
||||||
|
for(Document movie : user_movies)
|
||||||
|
list.put(movie.getInteger("movieid"),movie.getInteger("rating"));
|
||||||
|
|
||||||
|
inQuery.put("_id", new BasicDBObject("$in", list.keySet()));
|
||||||
|
cursor = movies.find(inQuery).iterator();
|
||||||
|
|
||||||
|
while (cursor.hasNext()) {
|
||||||
|
Document movie = cursor.next();
|
||||||
|
String[] genres;
|
||||||
|
genres = movie.getString("genres").split("\\|");
|
||||||
|
ArrayList<Genre> listGenres = new ArrayList<Genre>();
|
||||||
|
for(String genre : genres){
|
||||||
|
listGenres.add(new Genre(1,genre));
|
||||||
|
}
|
||||||
|
|
||||||
|
listRating.add(new Rating(new Movie(movie.getInteger("_id"), movie.getString("title"),
|
||||||
|
listGenres),user_id, list.get(movie.getInteger("_id"))));
|
||||||
|
}
|
||||||
|
return listRating;
|
||||||
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/movieratings", method = RequestMethod.GET)
|
@RequestMapping(value = "/movieratings", method = RequestMethod.GET)
|
||||||
public ModelAndView showMoviesRattings(
|
public ModelAndView showMoviesRattings(
|
||||||
@RequestParam(value = "user_id", required = true) Integer userId) {
|
@RequestParam(value = "user_id", required = true) Integer userId) {
|
||||||
@ -161,9 +208,7 @@ public class MainController {
|
|||||||
allMovies.add(new Movie(3, "Titre 3", Arrays.asList(new Genre[] {genre0, genre1, genre2})));
|
allMovies.add(new Movie(3, "Titre 3", Arrays.asList(new Genre[] {genre0, genre1, genre2})));
|
||||||
|
|
||||||
// TODO: write query to retrieve all ratings from the specified user
|
// TODO: write query to retrieve all ratings from the specified user
|
||||||
List<Rating> ratings = new LinkedList<Rating>();
|
List<Rating> ratings = getRatingMongoDBByUser(userId);
|
||||||
ratings.add(new Rating(new Movie(0, "Titre 0", Arrays.asList(new Genre[] {genre0, genre1})), userId, 3));
|
|
||||||
ratings.add(new Rating(new Movie(2, "Titre 2", Arrays.asList(new Genre[] {genre1})), userId, 4));
|
|
||||||
|
|
||||||
ModelAndView mv = new ModelAndView("movieratings");
|
ModelAndView mv = new ModelAndView("movieratings");
|
||||||
mv.addObject("userId", userId);
|
mv.addObject("userId", userId);
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
<ul>
|
<ul>
|
||||||
<c:forEach items="${ratings}" var="rating">
|
<c:forEach items="${ratings}" var="rating">
|
||||||
<li>
|
<li>
|
||||||
${rating.getMovie().id} - ${rating.score}
|
${rating.getMovie().id} - ${rating.getMovie().title} - ${rating.score}
|
||||||
</li>
|
</li>
|
||||||
</c:forEach>
|
</c:forEach>
|
||||||
</ul>
|
</ul>
|
||||||
|
Reference in New Issue
Block a user