mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-23 07:45:49 +00:00
Version 1 de reseñas. falta:
añadir nombre del usuario el boton de editar no aparezca en todas
This commit is contained in:
parent
39cfb27ef5
commit
5c459a2d90
10 changed files with 335 additions and 4 deletions
43
src/main/java/org/springframework/cheapy/model/Review.java
Normal file
43
src/main/java/org/springframework/cheapy/model/Review.java
Normal file
|
@ -0,0 +1,43 @@
|
|||
package org.springframework.cheapy.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
import org.hibernate.validator.constraints.Range;
|
||||
|
||||
import com.sun.istack.NotNull;
|
||||
|
||||
@Entity
|
||||
@Table(name = "review")
|
||||
public class Review extends BaseEntity{
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@NotBlank
|
||||
private String opinion;
|
||||
|
||||
@NotNull
|
||||
@Range(min = 1, max = 5)
|
||||
private Integer stars;
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package org.springframework.cheapy.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.cheapy.model.Review;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
public interface ReviewRepository extends Repository<Review, Integer> {
|
||||
|
||||
@Query("SELECT r FROM Review r")
|
||||
@Transactional(readOnly = true)
|
||||
List<Review> findAllReviews();
|
||||
|
||||
void save(Review review);
|
||||
|
||||
@Query("SELECT r FROM Review r WHERE id =:id")
|
||||
@Transactional(readOnly = true)
|
||||
Review findReviewById(@Param("id") Integer id);
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package org.springframework.cheapy.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cheapy.model.Review;
|
||||
import org.springframework.cheapy.model.StatusOffer;
|
||||
import org.springframework.cheapy.model.TimeOffer;
|
||||
import org.springframework.cheapy.repository.ReviewRepository;
|
||||
import org.springframework.cheapy.repository.TimeOfferRepository;
|
||||
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class ReviewService {
|
||||
private ReviewRepository reviewRepository;
|
||||
|
||||
@Autowired
|
||||
public ReviewService(final ReviewRepository reviewRepository) {
|
||||
this.reviewRepository = reviewRepository;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Review findReviewById(final int id) {
|
||||
return this.reviewRepository.findReviewById(id);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<Review> findAllReviews() {
|
||||
return this.reviewRepository.findAllReviews();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void saveReview(final Review Review) throws DataAccessException {
|
||||
this.reviewRepository.save(Review);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
package org.springframework.cheapy.web;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.springframework.cheapy.model.Review;
|
||||
import org.springframework.cheapy.service.ReviewService;
|
||||
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;
|
||||
|
||||
@Controller
|
||||
public class ReviewController {
|
||||
|
||||
|
||||
private static final String VIEWS_REVIEWS_CREATE_OR_UPDATE_FORM = "reviews/createOrUpdateReviewForm";
|
||||
private final ReviewService reviewService;
|
||||
|
||||
public ReviewController(final ReviewService reviewService) {
|
||||
this.reviewService = reviewService;
|
||||
}
|
||||
@GetMapping("/reviews")
|
||||
public String processFindForm( Map<String, Object> model) {
|
||||
|
||||
List<Review> reviewsLs=this.reviewService.findAllReviews();
|
||||
model.put("reviewsLs", reviewsLs);
|
||||
|
||||
return "reviews/reviewsList";
|
||||
|
||||
}
|
||||
|
||||
@GetMapping("/reviews/new")
|
||||
public String initCreationForm(Map<String, Object> model) {
|
||||
Review review = new Review();
|
||||
model.put("review", review);
|
||||
return VIEWS_REVIEWS_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
|
||||
@PostMapping("/reviews/new")
|
||||
public String processCreationForm(@Valid Review review, BindingResult result) {
|
||||
if (result.hasErrors()) {
|
||||
return VIEWS_REVIEWS_CREATE_OR_UPDATE_FORM;
|
||||
} else {
|
||||
this.reviewService.saveReview(review);
|
||||
return "redirect:/reviews/" + review.getId();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@GetMapping("/reviews/{reviewId}")
|
||||
public String processShowForm(@PathVariable("reviewId") int reviewId, Map<String, Object> model) {
|
||||
|
||||
Review review = this.reviewService.findReviewById(reviewId);
|
||||
|
||||
model.put("review", review);
|
||||
|
||||
|
||||
return "reviews/reviewsShow";
|
||||
|
||||
}
|
||||
|
||||
@GetMapping(value = "/reviews/{reviewId}/edit")
|
||||
public String updateReview(@PathVariable("reviewId") final int reviewId, final ModelMap model) {
|
||||
|
||||
|
||||
Review review = this.reviewService.findReviewById(reviewId);
|
||||
model.addAttribute("review", review);
|
||||
return ReviewController.VIEWS_REVIEWS_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
|
||||
@PostMapping(value = "/reviews/{reviewId}/edit")
|
||||
public String updateReview(@Valid final Review reviewEdit, final BindingResult result, final ModelMap model) {
|
||||
|
||||
if (result.hasErrors()) {
|
||||
model.addAttribute("review", reviewEdit);
|
||||
return ReviewController.VIEWS_REVIEWS_CREATE_OR_UPDATE_FORM;
|
||||
|
||||
} else {
|
||||
this.reviewService.saveReview(reviewEdit);
|
||||
return "redirect:/reviews/" + reviewEdit.getId();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -31,4 +31,8 @@ duplicate=Ya se encuentra en uso
|
|||
nonNumeric=Solo debe contener números
|
||||
duplicateFormSubmission=No se permite el envío de formularios duplicados
|
||||
typeMismatch.date=Fecha inválida
|
||||
typeMismatch.birthDate=Fecha inválida
|
||||
typeMismatch.birthDate=Fecha inválida
|
||||
review= Reseña
|
||||
reviews= Reseñas
|
||||
stars= Estrellas
|
||||
opinion= Opinión
|
|
@ -6,7 +6,7 @@
|
|||
<%@ taglib prefix="cheapy" tagdir="/WEB-INF/tags" %>
|
||||
<link href='https://fonts.googleapis.com/css?family=Lobster' rel='stylesheet'>
|
||||
|
||||
<cheapy:layout pageName="myOffers">
|
||||
<cheapy:layout pageName="ofertasM">
|
||||
<h2 style="text-align:center;padding:5px"><fmt:message key="foodOffers"/></h2>
|
||||
|
||||
<table id="foodOfferTable" class="table table-striped">
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
<%@ 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" %>
|
||||
|
||||
<cheapy:layout pageName="reviewsN">
|
||||
<h2>
|
||||
<c:if test="${review['new']}">Nueva </c:if> Opinión
|
||||
</h2>
|
||||
<form:form modelAttribute="review" class="form-horizontal" id="add-review-form">
|
||||
<div class="form-group has-feedback">
|
||||
<form:hidden path="id"/>
|
||||
|
||||
<cheapy:inputField label="Opinión" name="opinion"/>
|
||||
<cheapy:inputField label="Estrellas" name="stars"/>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<div class="col-sm-offset-2 col-sm-10">
|
||||
<c:choose>
|
||||
<c:when test="${review['new']}">
|
||||
<button class="btn btn-default" type="submit">Crear Opinión</button>
|
||||
</c:when>
|
||||
<c:otherwise>
|
||||
<button class="btn btn-default" type="submit">Modificar Opinión</button>
|
||||
</c:otherwise>
|
||||
</c:choose>
|
||||
</div>
|
||||
</div>
|
||||
</form:form>
|
||||
</cheapy:layout>
|
50
src/main/webapp/WEB-INF/jsp/reviews/reviewsList.jsp
Normal file
50
src/main/webapp/WEB-INF/jsp/reviews/reviewsList.jsp
Normal file
|
@ -0,0 +1,50 @@
|
|||
<%@ 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" %>
|
||||
<link href='https://fonts.googleapis.com/css?family=Lobster' rel='stylesheet'>
|
||||
|
||||
<cheapy:layout pageName="reviews">
|
||||
<h2 style="text-align:center;padding:5px"><fmt:message key="reviews"/></h2>
|
||||
|
||||
<table id="reviewTable" class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<!-- <th style="width: 150px;">Restaurante</th> -->
|
||||
<th><fmt:message key="stars"/></th>
|
||||
<th><fmt:message key="opinion"/></th>
|
||||
<th> </th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<c:forEach items="${reviewsLs}" var="review">
|
||||
<tr>
|
||||
<!-- <td> -->
|
||||
<%-- <c:out value="nombre por definir"/> <!-- ${review.usuario.nombre},${review.usuario.apellidos} --> --%>
|
||||
<!-- </td> -->
|
||||
<td>
|
||||
<c:out value="${review.stars}"/>
|
||||
</td>
|
||||
<td>
|
||||
<c:out value="${review.opinion}"/>
|
||||
</td>
|
||||
<td>
|
||||
<spring:url value="/reviews/{reviewId}" var="reviewUrl">
|
||||
<spring:param name="reviewId" value="${review.id}"/>
|
||||
</spring:url>
|
||||
<div class="btn-detalles">
|
||||
<button type="button" role="link" onclick="window.location='${fn:escapeXml(reviewUrl)}'" style="font-family: 'Lobster'; font-size: 20px;">
|
||||
<span class="glyphicon glyphicon-info-sign" aria-hidden="true" style="padding: 5px"> </span>
|
||||
<fmt:message key="details"/></button>
|
||||
</div>
|
||||
</td>
|
||||
|
||||
</tr>
|
||||
</c:forEach>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
|
||||
</cheapy:layout>
|
38
src/main/webapp/WEB-INF/jsp/reviews/reviewsShow.jsp
Normal file
38
src/main/webapp/WEB-INF/jsp/reviews/reviewsShow.jsp
Normal file
|
@ -0,0 +1,38 @@
|
|||
<%@ 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" %>
|
||||
<link href='https://fonts.googleapis.com/css?family=Lobster' rel='stylesheet'>
|
||||
|
||||
<cheapy:layout pageName="review">
|
||||
|
||||
<h2 style="text-align:center;padding:5px"><fmt:message key="review"/></h2>
|
||||
|
||||
|
||||
<table class="table table-striped" id="review-table">
|
||||
<tr>
|
||||
<th><fmt:message key="stars"/></th>
|
||||
<td><c:out value="${review.stars}"/></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><fmt:message key="opinion"/></th>
|
||||
<td><c:out value="${review.opinion}"/></td>
|
||||
</tr>
|
||||
|
||||
</table>
|
||||
|
||||
<div class="btn-return">
|
||||
<button type="button" role="link" onclick="window.location='/reviews'" style="font-family: 'Lobster'; font-size: 20px;">
|
||||
<span class="glyphicon glyphicon-arrow-left" aria-hidden="true" style="padding: 5px"> </span>
|
||||
<fmt:message key="return"/> </button>
|
||||
</div>
|
||||
|
||||
<spring:url value="{reviewId}/edit" var="editUrl">
|
||||
<spring:param name="reviewId" value="${review.id}"/>
|
||||
</spring:url>
|
||||
<a href="${fn:escapeXml(editUrl)}" class="btn btn-default">Editar opinión</a>
|
||||
|
||||
|
||||
</cheapy:layout>
|
|
@ -35,7 +35,7 @@
|
|||
</cheapy:menuItem>
|
||||
|
||||
<sec:authorize access="hasAnyAuthority('client')">
|
||||
<cheapy:menuItem active="${name eq 'ofertas'}" url="/myOffers" title="misOfertas">
|
||||
<cheapy:menuItem active="${name eq 'ofertasM'}" url="/myOffers" title="misOfertas">
|
||||
<span class="glyphicon glyphicon-cutlery" aria-hidden="true"></span>
|
||||
<span>Mis ofertas</span>
|
||||
</cheapy:menuItem>
|
||||
|
@ -47,7 +47,16 @@
|
|||
<span>Contáctanos</span>
|
||||
</cheapy:menuItem>
|
||||
-->
|
||||
|
||||
<sec:authorize access="isAuthenticated()">
|
||||
<cheapy:menuItem active="${name eq 'reviews'}" url="/reviews" title="opiniones">
|
||||
<span class="glyphicon glyphicon-cutlery" aria-hidden="true"></span>
|
||||
<span>Opiniones</span>
|
||||
</cheapy:menuItem>
|
||||
<cheapy:menuItem active="${name eq 'reviewsN'}" url="/reviews/new" title="valóranos">
|
||||
<span class="glyphicon glyphicon-cutlery" aria-hidden="true"></span>
|
||||
<span>Valóranos</span>
|
||||
</cheapy:menuItem>
|
||||
</sec:authorize>
|
||||
|
||||
</ul>
|
||||
|
||||
|
|
Loading…
Reference in a new issue