106 lines
3.8 KiB
Java
106 lines
3.8 KiB
Java
package com.camillepradel.movierecommender.controller;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.LinkedList;
|
|
import java.util.List;
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.servlet.ModelAndView;
|
|
|
|
import com.camillepradel.movierecommender.model.Genre;
|
|
import com.camillepradel.movierecommender.model.Movie;
|
|
import com.mongodb.BasicDBList;
|
|
import com.mongodb.BasicDBObject;
|
|
import com.mongodb.DBCollection;
|
|
import com.mongodb.DBCursor;
|
|
import com.mongodb.DBObject;
|
|
import com.mongodb.client.FindIterable;
|
|
import com.mongodb.client.MongoCollection;
|
|
import com.mongodb.client.MongoCursor;
|
|
import com.mongodb.client.MongoDatabase;
|
|
import java.util.ArrayList;
|
|
import org.bson.Document;
|
|
|
|
@Controller
|
|
public class MainController {
|
|
String message = "Welcome to Spring MVC!";
|
|
|
|
@RequestMapping("/hello")
|
|
public ModelAndView showMessage(
|
|
@RequestParam(value = "name", required = false, defaultValue = "World") String name) {
|
|
System.out.println("in controller");
|
|
|
|
ModelAndView mv = new ModelAndView("helloworld");
|
|
mv.addObject("message", message);
|
|
mv.addObject("name", name);
|
|
return mv;
|
|
}
|
|
|
|
@RequestMapping("/movies")
|
|
public ModelAndView showMovies(
|
|
@RequestParam(value = "user_id", required = false) Integer userId) {
|
|
System.out.println("show Movies of user " + userId);
|
|
|
|
// TODO: write query to retrieve all movies from DB or all movies rated by user with id userId,
|
|
// depending on whether or not a value was given for userId
|
|
|
|
List<Movie> movies = getMoviesMongoDBByUser(userId);
|
|
|
|
ModelAndView mv = new ModelAndView("movies");
|
|
mv.addObject("userId", userId);
|
|
mv.addObject("movies", movies);
|
|
return mv;
|
|
}
|
|
|
|
public List<Movie>getMoviesMongoDBByUser(Integer user_id){
|
|
ArrayList<Movie> listMovies= new ArrayList<Movie>();
|
|
MongoCursor<Document> cursor;
|
|
|
|
MongoDatabase db = MongoDBConnector.getInstance().getConnexion();
|
|
MongoCollection<Document> movies = db.getCollection("movies");
|
|
if(user_id != null)
|
|
{
|
|
MongoCollection<Document> users = db.getCollection("users");
|
|
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<Movie>();
|
|
}
|
|
ArrayList<Document> user_movies;
|
|
user_movies = (ArrayList) user.get("movies");
|
|
BasicDBObject inQuery = new BasicDBObject();
|
|
List<Integer> list = new ArrayList<Integer>();
|
|
for(Document movie : user_movies)
|
|
list.add(movie.getInteger("movieid"));
|
|
|
|
inQuery.put("_id", new BasicDBObject("$in", list));
|
|
cursor = movies.find(inQuery).iterator();
|
|
}
|
|
else
|
|
{
|
|
cursor = movies.find().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));
|
|
}
|
|
|
|
listMovies.add(new Movie(movie.getInteger("_id"), movie.getString("title"), listGenres));
|
|
}
|
|
return listMovies;
|
|
}
|
|
}
|