mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-25 08:35:49 +00:00
Version 2 (abrir):
solo puedes modificar tu reseña aparece el username en la lista
This commit is contained in:
parent
ef846967a1
commit
7070ca6414
7 changed files with 89 additions and 15 deletions
|
@ -23,6 +23,17 @@ public class Review extends BaseEntity{
|
||||||
@Range(min = 1, max = 5)
|
@Range(min = 1, max = 5)
|
||||||
private Integer stars;
|
private Integer stars;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "username", referencedColumnName = "username")
|
||||||
|
private User escritor;
|
||||||
|
|
||||||
|
public User getEscritor() {
|
||||||
|
return escritor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEscritor(User escritor) {
|
||||||
|
this.escritor = escritor;
|
||||||
|
}
|
||||||
|
|
||||||
public String getOpinion() {
|
public String getOpinion() {
|
||||||
return opinion;
|
return opinion;
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
|
||||||
|
package org.springframework.cheapy.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.cheapy.model.User;
|
||||||
|
import org.springframework.cheapy.model.Usuario;
|
||||||
|
|
||||||
|
public interface UserRepository extends CrudRepository<Usuario, String> {
|
||||||
|
|
||||||
|
@Query("SELECT u FROM User u WHERE username =:username")
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
User findByUsername(String username);
|
||||||
|
}
|
|
@ -1,11 +0,0 @@
|
||||||
|
|
||||||
package org.springframework.cheapy.repository;
|
|
||||||
|
|
||||||
import org.springframework.data.repository.CrudRepository;
|
|
||||||
import org.springframework.cheapy.model.Usuario;
|
|
||||||
|
|
||||||
public interface UsuarioRepository extends CrudRepository<Usuario, String> {
|
|
||||||
|
|
||||||
//Usuario findByUsername(String currentPrincipalName);
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
package org.springframework.cheapy.service;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.cheapy.model.User;
|
||||||
|
import org.springframework.cheapy.repository.UserRepository;
|
||||||
|
import org.springframework.dao.DataAccessException;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class UserService {
|
||||||
|
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public UserService(final UserRepository userRepository) {
|
||||||
|
this.userRepository = userRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public User getCurrentUser() throws DataAccessException {
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
String username = authentication.getName();
|
||||||
|
return this.userRepository.findByUsername(username);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -6,8 +6,11 @@ import java.util.Map;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
|
||||||
|
import org.springframework.cheapy.model.Client;
|
||||||
import org.springframework.cheapy.model.Review;
|
import org.springframework.cheapy.model.Review;
|
||||||
|
import org.springframework.cheapy.model.User;
|
||||||
import org.springframework.cheapy.service.ReviewService;
|
import org.springframework.cheapy.service.ReviewService;
|
||||||
|
import org.springframework.cheapy.service.UserService;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.ModelMap;
|
import org.springframework.ui.ModelMap;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
|
@ -21,9 +24,21 @@ public class ReviewController {
|
||||||
|
|
||||||
private static final String VIEWS_REVIEWS_CREATE_OR_UPDATE_FORM = "reviews/createOrUpdateReviewForm";
|
private static final String VIEWS_REVIEWS_CREATE_OR_UPDATE_FORM = "reviews/createOrUpdateReviewForm";
|
||||||
private final ReviewService reviewService;
|
private final ReviewService reviewService;
|
||||||
|
private final UserService userService;
|
||||||
|
|
||||||
public ReviewController(final ReviewService reviewService) {
|
public ReviewController(final ReviewService reviewService, final UserService userService) {
|
||||||
this.reviewService = reviewService;
|
this.reviewService = reviewService;
|
||||||
|
this.userService = userService;
|
||||||
|
}
|
||||||
|
private boolean checkIdentity(final int reviewId) {
|
||||||
|
boolean res = false;
|
||||||
|
User user = this.userService.getCurrentUser();
|
||||||
|
Review review = this.reviewService.findReviewById(reviewId);
|
||||||
|
User reviewsAuthor = review.getEscritor();
|
||||||
|
if (user.equals(reviewsAuthor)) {
|
||||||
|
res = true;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
@GetMapping("/reviews")
|
@GetMapping("/reviews")
|
||||||
public String processFindForm( Map<String, Object> model) {
|
public String processFindForm( Map<String, Object> model) {
|
||||||
|
@ -47,6 +62,9 @@ public class ReviewController {
|
||||||
if (result.hasErrors()) {
|
if (result.hasErrors()) {
|
||||||
return VIEWS_REVIEWS_CREATE_OR_UPDATE_FORM;
|
return VIEWS_REVIEWS_CREATE_OR_UPDATE_FORM;
|
||||||
} else {
|
} else {
|
||||||
|
User escritor = this.userService.getCurrentUser();
|
||||||
|
review.setEscritor(escritor);
|
||||||
|
|
||||||
this.reviewService.saveReview(review);
|
this.reviewService.saveReview(review);
|
||||||
return "redirect:/reviews/" + review.getId();
|
return "redirect:/reviews/" + review.getId();
|
||||||
}
|
}
|
||||||
|
@ -68,7 +86,9 @@ public class ReviewController {
|
||||||
|
|
||||||
@GetMapping(value = "/reviews/{reviewId}/edit")
|
@GetMapping(value = "/reviews/{reviewId}/edit")
|
||||||
public String updateReview(@PathVariable("reviewId") final int reviewId, final ModelMap model) {
|
public String updateReview(@PathVariable("reviewId") final int reviewId, final ModelMap model) {
|
||||||
|
if (!this.checkIdentity(reviewId)) {
|
||||||
|
return "error";
|
||||||
|
}
|
||||||
|
|
||||||
Review review = this.reviewService.findReviewById(reviewId);
|
Review review = this.reviewService.findReviewById(reviewId);
|
||||||
model.addAttribute("review", review);
|
model.addAttribute("review", review);
|
||||||
|
@ -77,12 +97,17 @@ public class ReviewController {
|
||||||
|
|
||||||
@PostMapping(value = "/reviews/{reviewId}/edit")
|
@PostMapping(value = "/reviews/{reviewId}/edit")
|
||||||
public String updateReview(@Valid final Review reviewEdit, final BindingResult result, final ModelMap model) {
|
public String updateReview(@Valid final Review reviewEdit, final BindingResult result, final ModelMap model) {
|
||||||
|
if (!this.checkIdentity(reviewEdit.getId())) {
|
||||||
|
return "error";
|
||||||
|
}
|
||||||
if (result.hasErrors()) {
|
if (result.hasErrors()) {
|
||||||
model.addAttribute("review", reviewEdit);
|
model.addAttribute("review", reviewEdit);
|
||||||
return ReviewController.VIEWS_REVIEWS_CREATE_OR_UPDATE_FORM;
|
return ReviewController.VIEWS_REVIEWS_CREATE_OR_UPDATE_FORM;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
User escritor = this.userService.getCurrentUser();
|
||||||
|
reviewEdit.setEscritor(escritor);
|
||||||
|
|
||||||
this.reviewService.saveReview(reviewEdit);
|
this.reviewService.saveReview(reviewEdit);
|
||||||
return "redirect:/reviews/" + reviewEdit.getId();
|
return "redirect:/reviews/" + reviewEdit.getId();
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,3 +36,4 @@ review= Rese
|
||||||
reviews= Reseñas
|
reviews= Reseñas
|
||||||
stars= Estrellas
|
stars= Estrellas
|
||||||
opinion= Opinión
|
opinion= Opinión
|
||||||
|
user = Nombre de usuario
|
|
@ -13,6 +13,7 @@
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<!-- <th style="width: 150px;">Restaurante</th> -->
|
<!-- <th style="width: 150px;">Restaurante</th> -->
|
||||||
|
<th><fmt:message key="user"/></th>
|
||||||
<th><fmt:message key="stars"/></th>
|
<th><fmt:message key="stars"/></th>
|
||||||
<th><fmt:message key="opinion"/></th>
|
<th><fmt:message key="opinion"/></th>
|
||||||
<th> </th>
|
<th> </th>
|
||||||
|
@ -24,6 +25,9 @@
|
||||||
<!-- <td> -->
|
<!-- <td> -->
|
||||||
<%-- <c:out value="nombre por definir"/> <!-- ${review.usuario.nombre},${review.usuario.apellidos} --> --%>
|
<%-- <c:out value="nombre por definir"/> <!-- ${review.usuario.nombre},${review.usuario.apellidos} --> --%>
|
||||||
<!-- </td> -->
|
<!-- </td> -->
|
||||||
|
<td>
|
||||||
|
<c:out value="${review.escritor.username}"/>
|
||||||
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<c:out value="${review.stars}"/>
|
<c:out value="${review.stars}"/>
|
||||||
</td>
|
</td>
|
||||||
|
|
Loading…
Reference in a new issue