Add a view to show user movie ratings and add/update ratings.

This commit is contained in:
Camille31 2016-11-27 11:10:31 +01:00
parent 5284966d4a
commit 3afd36cb06
4 changed files with 169 additions and 1 deletions

View File

@ -5,12 +5,15 @@ import java.util.LinkedList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestMethod;
import com.camillepradel.movierecommender.model.Genre;
import com.camillepradel.movierecommender.model.Movie;
import com.camillepradel.movierecommender.model.Rating;
@Controller
public class MainController {
@ -48,4 +51,45 @@ public class MainController {
mv.addObject("movies", movies);
return mv;
}
@RequestMapping(value = "/movieratings", method = RequestMethod.GET)
public ModelAndView showMoviesRattings(
@RequestParam(value = "user_id", required = true) Integer userId) {
System.out.println("GET /movieratings for user " + userId);
// TODO: write query to retrieve all movies from DB
List<Movie> allMovies = new LinkedList<Movie>();
Genre genre0 = new Genre(0, "genre0");
Genre genre1 = new Genre(1, "genre1");
Genre genre2 = new Genre(2, "genre2");
allMovies.add(new Movie(0, "Titre 0", Arrays.asList(new Genre[] {genre0, genre1})));
allMovies.add(new Movie(1, "Titre 1", Arrays.asList(new Genre[] {genre0, genre2})));
allMovies.add(new Movie(2, "Titre 2", Arrays.asList(new Genre[] {genre1})));
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
List<Rating> ratings = new LinkedList<Rating>();
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");
mv.addObject("userId", userId);
mv.addObject("allMovies", allMovies);
mv.addObject("ratings", ratings);
return mv;
}
@RequestMapping(value = "/movieratings", method = RequestMethod.POST)
public String saveOrUpdateRating(@ModelAttribute("rating") Rating rating) {
System.out.println("POST /movieratings for user " + rating.getUserId()
+ ", movie " + rating.getMovie().getId()
+ ", score " + rating.getScore());
// TODO: add query which
// - add rating between specified user and movie if it doesn't exist
// - update it if it does exist
return "redirect:/movieratings?user_id=" + rating.getUserId();
}
}

View File

@ -14,7 +14,7 @@ public class Movie {
this.genres = genres;
}
public long getId() {
public int getId() {
return this.id;
}

View File

@ -0,0 +1,67 @@
package com.camillepradel.movierecommender.model;
import java.util.Arrays;
public class Rating {
private Movie movie;
private int userId;
private int score;
public Rating() {
this.movie = null;
this.userId = 0;
this.score = 0;
}
public Rating(Movie movie, int userId, int score) {
this.movie = movie;
this.userId = userId;
this.score = score;
}
public Rating(Movie movie, int userId) {
this.movie = movie;
this.userId = userId;
this.score = 0;
}
public Movie getMovie() {
return this.movie;
}
public void setMovie(Movie movie) {
this.movie = movie;
}
// pseudo getter and setter for movie id
// (in order to automatically serialize Rating object on form submission)
public int getMovieId() {
return this.movie.getId();
}
public void setMovieId(int movieId) {
// TODO: get movie with id movieId from database
String title = "Titre";
Genre genre0 = new Genre(0, "genre0");
Genre genre1 = new Genre(1, "genre1");
this.movie = new Movie(movieId, title, Arrays.asList(new Genre[] {genre0, genre1}));
}
public int getUserId() {
return this.userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getScore() {
return this.score;
}
public void setScore(int score) {
this.score = score;
}
}

View File

@ -0,0 +1,57 @@
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Films</title>
</head>
<body>
<h1>
Vos films notés
</h1>
<ul>
<c:forEach items="${ratings}" var="rating">
<li>
${rating.getMovie().id} - ${rating.score}
</li>
</c:forEach>
</ul>
<h1>
Ajouter une note
</h1>
<ul>
<c:forEach items="${allMovies}" var="movie">
<!-- get movie rating if any -->
<c:set var="score" value="0" />
<c:forEach items="${ratings}" var="rating">
<c:if test="${rating.getMovie().id eq movie.id}">
<c:set var="score" value="${rating.score}" />
</c:if>
</c:forEach>
<li>
${movie.title} - ${score} (user id: ${userId}, movie id: ${movie.id})
<form method="post" action="/MovieRecommender/movieratings">
<input type="number" name="userId" value="${userId}" hidden="hidden">
<input type="number" name="movieId" value="${movie.id}" hidden="hidden">
<input type="radio" name="score" onclick="this.form.submit();" value="1" <c:if test="${score eq 1}">checked</c:if>>
<input type="radio" name="score" onclick="this.form.submit();" value="2" <c:if test="${score eq 2}">checked</c:if>>
<input type="radio" name="score" onclick="this.form.submit();" value="3" <c:if test="${score eq 3}">checked</c:if>>
<input type="radio" name="score" onclick="this.form.submit();" value="4" <c:if test="${score eq 4}">checked</c:if>>
<input type="radio" name="score" onclick="this.form.submit();" value="5" <c:if test="${score eq 5}">checked</c:if>>
</form>
<ul>
<c:forEach items="${movie.genres}" var="genre">
<li>
${genre.name}
</li>
</c:forEach>
</ul>
</li>
</c:forEach>
</ul>
</body>
</html>