From 0a131ae48fb6f1fe0b64ba8cc790f360c4a50a91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Franco=20S=C3=A1nchez?= <56022165+pabfrasan@users.noreply.github.com> Date: Sat, 17 Apr 2021 13:46:35 +0200 Subject: [PATCH] Valorar restaurantes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Se han añadido las funcionalidades y vistas para añadir, editar, listar y enseñar las valoraciones creadas por los usuarios a los clientes --- .../cheapy/model/ReviewClient.java | 67 +++++++++ .../repository/ReviewClientRepository.java | 19 +++ .../cheapy/service/ClientService.java | 15 +- .../cheapy/service/ReviewClientService.java | 41 ++++++ .../cheapy/web/ClientController.java | 4 +- .../cheapy/web/ReviewClientController.java | 139 ++++++++++++++++++ src/main/resources/db/mysql/data.sql | 11 ++ .../WEB-INF/jsp/clients/restaurantShow.jsp | 29 +++- .../jsp/reviews/createOrUpdateReviewForm.jsp | 4 +- .../createOrUpdateReviewForm.jsp | 42 ++++++ .../WEB-INF/jsp/reviewsClient/reviewsList.jsp | 93 ++++++++++++ .../WEB-INF/jsp/reviewsClient/reviewsShow.jsp | 52 +++++++ src/main/webapp/WEB-INF/tags/showStars.tag | 7 +- 13 files changed, 513 insertions(+), 10 deletions(-) create mode 100644 src/main/java/org/springframework/cheapy/model/ReviewClient.java create mode 100644 src/main/java/org/springframework/cheapy/repository/ReviewClientRepository.java create mode 100644 src/main/java/org/springframework/cheapy/service/ReviewClientService.java create mode 100644 src/main/java/org/springframework/cheapy/web/ReviewClientController.java create mode 100644 src/main/webapp/WEB-INF/jsp/reviewsClient/createOrUpdateReviewForm.jsp create mode 100644 src/main/webapp/WEB-INF/jsp/reviewsClient/reviewsList.jsp create mode 100644 src/main/webapp/WEB-INF/jsp/reviewsClient/reviewsShow.jsp diff --git a/src/main/java/org/springframework/cheapy/model/ReviewClient.java b/src/main/java/org/springframework/cheapy/model/ReviewClient.java new file mode 100644 index 000000000..8fe0176f6 --- /dev/null +++ b/src/main/java/org/springframework/cheapy/model/ReviewClient.java @@ -0,0 +1,67 @@ +package org.springframework.cheapy.model; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; + +import org.hibernate.validator.constraints.Range; + +@Entity +@Table(name = "review_client") +public class ReviewClient extends BaseEntity{ + +private static final long serialVersionUID = 1L; + + @NotBlank(message = "Debe rellenar la valoración sobre el bar") + @Column(length=16777215) + private String opinion; + + @NotNull(message= "Por favor rellene este campo") + @Range(min = 1, max = 5,message="Las estrellas deben ir entre 1 y 5") + private Integer stars; + + @ManyToOne + @JoinColumn(name = "username", referencedColumnName = "username") + private User escritor; + + @ManyToOne + @JoinColumn(name = "client", referencedColumnName = "id") + private Client bar; + + public User getEscritor() { + return escritor; + } + + public Client getBar() { + return bar; + } + + public void setBar(Client bar) { + this.bar = bar; + } + + public void setEscritor(User escritor) { + this.escritor = escritor; + } + + public String getOpinion() { + return opinion; + } + + public void setOpinion(String opinion) { + this.opinion = opinion; + } + + public Integer getStars() { + return stars; + } + + public void setStars(Integer stars) { + this.stars = stars; + } + +} diff --git a/src/main/java/org/springframework/cheapy/repository/ReviewClientRepository.java b/src/main/java/org/springframework/cheapy/repository/ReviewClientRepository.java new file mode 100644 index 000000000..443bbf839 --- /dev/null +++ b/src/main/java/org/springframework/cheapy/repository/ReviewClientRepository.java @@ -0,0 +1,19 @@ +package org.springframework.cheapy.repository; + +import java.util.List; + +import org.springframework.cheapy.model.Client; +import org.springframework.cheapy.model.ReviewClient; +import org.springframework.data.domain.Pageable; +import org.springframework.data.repository.PagingAndSortingRepository; + +public interface ReviewClientRepository extends PagingAndSortingRepository { + + List findByBar(String bar); + + List findAllReviewClientByBar(Pageable p, Client client); + + List findAllReviewClientByBar(Client client); + + ReviewClient findReviewClientById(int id); +} diff --git a/src/main/java/org/springframework/cheapy/service/ClientService.java b/src/main/java/org/springframework/cheapy/service/ClientService.java index e65eec3a7..05b84400a 100644 --- a/src/main/java/org/springframework/cheapy/service/ClientService.java +++ b/src/main/java/org/springframework/cheapy/service/ClientService.java @@ -7,9 +7,11 @@ import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cheapy.model.Client; import org.springframework.cheapy.model.Code; +import org.springframework.cheapy.model.ReviewClient; import org.springframework.cheapy.model.Usuario; import org.springframework.cheapy.repository.ClientRepository; import org.springframework.cheapy.repository.CodeRepository; +import org.springframework.cheapy.repository.ReviewClientRepository; import org.springframework.dao.DataAccessException; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; @@ -21,11 +23,13 @@ public class ClientService { private ClientRepository clientRepository; private CodeRepository codeRepository; + private ReviewClientRepository reviewRepositoy; @Autowired - public ClientService(final ClientRepository clientRepository, CodeRepository codeRepository) { + public ClientService(final ClientRepository clientRepository, CodeRepository codeRepository, ReviewClientRepository reviewRepositoy) { this.clientRepository = clientRepository; this.codeRepository = codeRepository; + this.reviewRepositoy = reviewRepositoy; } @Transactional @@ -57,4 +61,13 @@ public class ClientService { public List findAllClient() throws DataAccessException { return this.clientRepository.findAllClient(); } + + public Integer mediaValoraciones(Client client) { + List valoraciones =this.reviewRepositoy.findAllReviewClientByBar(client); + if(valoraciones.size()!=0) { + return Integer.valueOf( (int) valoraciones.stream().mapToInt(x->x.getStars()).average().getAsDouble()); + }else { + return 0; + } + } } diff --git a/src/main/java/org/springframework/cheapy/service/ReviewClientService.java b/src/main/java/org/springframework/cheapy/service/ReviewClientService.java new file mode 100644 index 000000000..9bcf2e0f8 --- /dev/null +++ b/src/main/java/org/springframework/cheapy/service/ReviewClientService.java @@ -0,0 +1,41 @@ +package org.springframework.cheapy.service; + +import java.util.List; + +import javax.transaction.Transactional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cheapy.model.Client; +import org.springframework.cheapy.model.Review; +import org.springframework.cheapy.model.ReviewClient; +import org.springframework.cheapy.repository.ReviewClientRepository; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +@Service +public class ReviewClientService { + + private ReviewClientRepository repo; + + @Autowired + public ReviewClientService(ReviewClientRepository repo) { + super(); + this.repo = repo; + } + @Transactional + public void saveReview(final ReviewClient entity) { + this.repo.save(entity); + } + @Transactional + public List findByClient(String idClient){ + return this.repo.findByBar(idClient); + } + + public ReviewClient findReviewById(int reviewId) { + return this.repo.findReviewClientById(reviewId); + } + public List findAllReviewsByBar(Pageable p, Client client) { + + return this.repo.findAllReviewClientByBar(p, client); + } +} diff --git a/src/main/java/org/springframework/cheapy/web/ClientController.java b/src/main/java/org/springframework/cheapy/web/ClientController.java index 7bc63dbc7..a195802ac 100644 --- a/src/main/java/org/springframework/cheapy/web/ClientController.java +++ b/src/main/java/org/springframework/cheapy/web/ClientController.java @@ -161,9 +161,11 @@ public class ClientController { } @GetMapping(value = "/restaurant/{clientId}") public String showRestaurant(final ModelMap model, @PathVariable("clientId") Integer id) { - + Client client = this.clientRepo.findById(id).get(); + Integer valoraciones=this.clientService.mediaValoraciones(client); model.put("client", client); + model.put("reviews", valoraciones); return "clients/restaurantShow"; } } diff --git a/src/main/java/org/springframework/cheapy/web/ReviewClientController.java b/src/main/java/org/springframework/cheapy/web/ReviewClientController.java new file mode 100644 index 000000000..7e8a925fc --- /dev/null +++ b/src/main/java/org/springframework/cheapy/web/ReviewClientController.java @@ -0,0 +1,139 @@ +package org.springframework.cheapy.web; + +import java.util.List; +import java.util.Map; + +import javax.validation.Valid; + +import org.springframework.cheapy.model.Client; +import org.springframework.cheapy.model.Review; +import org.springframework.cheapy.model.ReviewClient; +import org.springframework.cheapy.model.User; +import org.springframework.cheapy.service.ClientService; +import org.springframework.cheapy.service.ReviewClientService; +import org.springframework.cheapy.service.UserService; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; + +import net.bytebuddy.asm.Advice.This; + +@Controller +public class ReviewClientController { + + private static final String VIEWS_REVIEWS_CREATE_OR_UPDATE_FORM = "reviewsClient/createOrUpdateReviewForm"; + private final ReviewClientService reviewService; + private final UserService userService; + private final ClientService clientService; + + public ReviewClientController(ReviewClientService reviewService, UserService userService, ClientService clientService) { + super(); + this.clientService = clientService; + this.reviewService = reviewService; + this.userService = userService; + } + + private boolean checkIdentity(final int reviewId) { + boolean res = false; + User user = this.userService.getCurrentUser(); + ReviewClient review = this.reviewService.findReviewById(reviewId); + User reviewsAuthor = review.getEscritor(); + if (user.equals(reviewsAuthor)) { + res = true; + } + return res; + } + + private boolean checkClient(final String client) { + User user = this.userService.getCurrentUser(); + Client bar = this.clientService.findByUsername(client); + return (bar == null||user==null)? false: true; + } + @GetMapping("/reviewsClient/new/{idClient}") + public String initCreationForm(final Map model, @PathVariable("idClient") final String idClient) { + if(!checkClient(idClient)) { + return "error"; + } + + ReviewClient review = new ReviewClient(); + + model.put("review", review); + return ReviewClientController.VIEWS_REVIEWS_CREATE_OR_UPDATE_FORM; + } + + @PostMapping("/reviewsClient/new/{idClient}") + public String processCreationForm(@Valid final ReviewClient review, @PathVariable("idClient") final String idClient,final BindingResult result) { + if (result.hasErrors()) { + return ReviewClientController.VIEWS_REVIEWS_CREATE_OR_UPDATE_FORM; + } else { + User escritor = this.userService.getCurrentUser(); + review.setEscritor(escritor); + Client bar = this.clientService.findByUsername(idClient); + review.setBar(bar); + + this.reviewService.saveReview(review); + return "redirect:/reviewsClient/" + review.getId(); + } + } + + @GetMapping("/reviewsClient/{reviewId}") + public String processShowForm(@PathVariable("reviewId") final int reviewId, final Map model) { + ReviewClient review = this.reviewService.findReviewById(reviewId); + + model.put("review", review); + + return "reviewsClient/reviewsShow"; + + } + @GetMapping("/reviewsClientList/{idClient}/{page}") + public String processFindForm(@PathVariable("page") final int page, @PathVariable("idClient") final String idClient, final Map model) { + Pageable elements = PageRequest.of(page, 6); + Client client = this.clientService.findByUsername(idClient); + + List reviewsLs = this.reviewService.findAllReviewsByBar(elements,client); + model.put("reviewsLs", reviewsLs); + model.put("client", idClient); + + return "reviewsClient/reviewsList"; + + } + + @GetMapping("/reviewsClient/{reviewId}/edit") + public String updateReviewInit(@PathVariable("reviewId") final int reviewId, final ModelMap model) { + if (!this.checkIdentity(reviewId)) { + return "error"; + } + ReviewClient review = this.reviewService.findReviewById(reviewId); + + model.addAttribute("review", review); + return ReviewClientController.VIEWS_REVIEWS_CREATE_OR_UPDATE_FORM; + } + + @PostMapping("/reviewsClient/{reviewId}/edit") + public String updateReviewPost(@Valid final ReviewClient reviewEdit, final BindingResult result, final ModelMap model) { + if (!this.checkIdentity(reviewEdit.getId())) { + return "error"; + } + if (result.hasErrors()) { + model.addAttribute("review", reviewEdit); + return ReviewClientController.VIEWS_REVIEWS_CREATE_OR_UPDATE_FORM; + + } else { + Client bar = this.reviewService.findReviewById(reviewEdit.getId()).getBar(); + User escritor = this.userService.getCurrentUser(); + reviewEdit.setEscritor(escritor); + reviewEdit.setBar(bar); + + this.reviewService.saveReview(reviewEdit); + return "redirect:/reviewsClient/" + reviewEdit.getId(); + } + + } + + +} diff --git a/src/main/resources/db/mysql/data.sql b/src/main/resources/db/mysql/data.sql index 0c8238235..995fd0cb6 100644 --- a/src/main/resources/db/mysql/data.sql +++ b/src/main/resources/db/mysql/data.sql @@ -63,3 +63,14 @@ INSERT INTO nu_offers(start, end, code, status, client_id, gold, discount_gold, INSERT INTO nu_offers(start, end, code, status, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'NU-6', 'active',2,20,30,15,10,10,5); INSERT INTO nu_offers(start, end, code, status, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'NU-7', 'active',2,20,35,15,15,10,5); INSERT INTO nu_offers(start, end, code, status, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-08-16 12:00:00', '2021-08-17 12:00:00', null, 'hidden',1,15,25,10,15,5,10); + +INSERT INTO review_client(opinion, stars, username, client ) VALUES ('Es un bar muy bueno para ir a comer',5,'paco',1); +INSERT INTO review_client(opinion, stars, username, client ) VALUES ('Las ofertas eran buenas pero el servicio mejorable',2,'lolo',1); +INSERT INTO review_client(opinion, stars, username, client ) VALUES ('Nos trataron genial a mi y a mi amigo',4,'pepe',1); +INSERT INTO review_client(opinion, stars, username, client ) VALUES ('Abren a todas horas!!!',5,'paco',1); +INSERT INTO review_client(opinion, stars, username, client ) VALUES ('La comida de hoy estaba muy rica',4,'lolo',1); +INSERT INTO review_client(opinion, stars, username, client ) VALUES ('Recomiendo ir por la noche, tiene muy buenas vistas',4,'pepe',1); +INSERT INTO review_client(opinion, stars, username, client ) VALUES ('No retrasmiten futbol',1,'pepe',1); +INSERT INTO review_client(opinion, stars, username, client ) VALUES ('Un sitio perfecto para llevar a tu pareja',5,'paco',2); +INSERT INTO review_client(opinion, stars, username, client ) VALUES ('En hora punta nunca hay sitio',2,'lolo',2); +INSERT INTO review_client(opinion, stars, username, client ) VALUES ('Fui una vez por probar y ahora voy todas las tardes',4,'pepe',2); \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/jsp/clients/restaurantShow.jsp b/src/main/webapp/WEB-INF/jsp/clients/restaurantShow.jsp index 9de6c43ed..59cfe490f 100644 --- a/src/main/webapp/WEB-INF/jsp/clients/restaurantShow.jsp +++ b/src/main/webapp/WEB-INF/jsp/clients/restaurantShow.jsp @@ -45,15 +45,36 @@ - + - +
+ + +
-
- + + +
+ + + + + + + + + + + + + +
+ diff --git a/src/main/webapp/WEB-INF/jsp/reviews/createOrUpdateReviewForm.jsp b/src/main/webapp/WEB-INF/jsp/reviews/createOrUpdateReviewForm.jsp index 2af06c0d8..db9d3f89e 100644 --- a/src/main/webapp/WEB-INF/jsp/reviews/createOrUpdateReviewForm.jsp +++ b/src/main/webapp/WEB-INF/jsp/reviews/createOrUpdateReviewForm.jsp @@ -10,7 +10,7 @@

- Nueva Reseña + Nueva Reseña

@@ -29,7 +29,7 @@ Crear reseña - +
diff --git a/src/main/webapp/WEB-INF/jsp/reviewsClient/createOrUpdateReviewForm.jsp b/src/main/webapp/WEB-INF/jsp/reviewsClient/createOrUpdateReviewForm.jsp new file mode 100644 index 000000000..b124b0e26 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/reviewsClient/createOrUpdateReviewForm.jsp @@ -0,0 +1,42 @@ +<%@ page session="false" trimDirectiveWhitespaces="true" %> +<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> +<%@ taglib prefix="cheapy" tagdir="/WEB-INF/tags" %> +<%@ page contentType="text/html; charset=UTF-8" %> + + + +

+ Nueva Reseña +

+ +
+ + + + +
+
+
+
+ + + + + + + + +
+
+
+ + + +
+
diff --git a/src/main/webapp/WEB-INF/jsp/reviewsClient/reviewsList.jsp b/src/main/webapp/WEB-INF/jsp/reviewsClient/reviewsList.jsp new file mode 100644 index 000000000..606ba90c9 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/reviewsClient/reviewsList.jsp @@ -0,0 +1,93 @@ +<%@ page session="false" trimDirectiveWhitespaces="true" %> +<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> +<%@ taglib prefix="cheapy" tagdir="/WEB-INF/tags" %> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> +<%@ page contentType="text/html; charset=UTF-8" %> + + + + +

+ + + + + + + + + + + + + + + + + + + + + + <%-- --%> + + + + + + + + + + + + +
+ + + + + + + + + + + + +
+ +
+
+
+ +
+ +
+ + + + + +
+
+ + +
+ + + + + +
+
+
diff --git a/src/main/webapp/WEB-INF/jsp/reviewsClient/reviewsShow.jsp b/src/main/webapp/WEB-INF/jsp/reviewsClient/reviewsShow.jsp new file mode 100644 index 000000000..094bab485 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/reviewsClient/reviewsShow.jsp @@ -0,0 +1,52 @@ +<%@ page session="false" trimDirectiveWhitespaces="true" %> +<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="cheapy" tagdir="/WEB-INF/tags" %> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> +<%@ page contentType="text/html; charset=UTF-8" %> + +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> + + + + +

+ + + + + + + + + + + + + + + + +
+ +
+ + + +
+ + + + + + + + +
+
+ +
diff --git a/src/main/webapp/WEB-INF/tags/showStars.tag b/src/main/webapp/WEB-INF/tags/showStars.tag index f68844d5c..1fb11cb10 100644 --- a/src/main/webapp/WEB-INF/tags/showStars.tag +++ b/src/main/webapp/WEB-INF/tags/showStars.tag @@ -2,11 +2,14 @@ <%@ tag language="java" pageEncoding="ISO-8859-1"%> <%@ attribute name="value" required="true" rtexprvalue="true" type="Integer" description="Number of starts" %> +<%@ attribute name="label" required="false" rtexprvalue="true" + description="Label appears in red color if input is considered as invalid after submission" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
- - + + +
/> />