Version 2 (abrir):

solo puedes modificar tu reseña
aparece el username en la lista
This commit is contained in:
Flor 2021-03-31 19:42:53 +02:00
parent ef846967a1
commit 7070ca6414
7 changed files with 89 additions and 15 deletions

View file

@ -23,7 +23,18 @@ public class Review extends BaseEntity{
@Range(min = 1, max = 5)
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() {
return opinion;
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}
}

View file

@ -6,8 +6,11 @@ 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.User;
import org.springframework.cheapy.service.ReviewService;
import org.springframework.cheapy.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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 final ReviewService reviewService;
private final UserService userService;
public ReviewController(final ReviewService reviewService) {
public ReviewController(final ReviewService reviewService, final UserService userService) {
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")
public String processFindForm( Map<String, Object> model) {
@ -47,6 +62,9 @@ public class ReviewController {
if (result.hasErrors()) {
return VIEWS_REVIEWS_CREATE_OR_UPDATE_FORM;
} else {
User escritor = this.userService.getCurrentUser();
review.setEscritor(escritor);
this.reviewService.saveReview(review);
return "redirect:/reviews/" + review.getId();
}
@ -68,7 +86,9 @@ public class ReviewController {
@GetMapping(value = "/reviews/{reviewId}/edit")
public String updateReview(@PathVariable("reviewId") final int reviewId, final ModelMap model) {
if (!this.checkIdentity(reviewId)) {
return "error";
}
Review review = this.reviewService.findReviewById(reviewId);
model.addAttribute("review", review);
@ -77,12 +97,17 @@ public class ReviewController {
@PostMapping(value = "/reviews/{reviewId}/edit")
public String updateReview(@Valid final Review reviewEdit, final BindingResult result, final ModelMap model) {
if (!this.checkIdentity(reviewEdit.getId())) {
return "error";
}
if (result.hasErrors()) {
model.addAttribute("review", reviewEdit);
return ReviewController.VIEWS_REVIEWS_CREATE_OR_UPDATE_FORM;
} else {
User escritor = this.userService.getCurrentUser();
reviewEdit.setEscritor(escritor);
this.reviewService.saveReview(reviewEdit);
return "redirect:/reviews/" + reviewEdit.getId();
}

View file

@ -35,4 +35,5 @@ typeMismatch.birthDate=Fecha inválida
review= Reseña
reviews= Reseñas
stars= Estrellas
opinion= Opinión
opinion= Opinión
user = Nombre de usuario

View file

@ -13,6 +13,7 @@
<thead>
<tr>
<!-- <th style="width: 150px;">Restaurante</th> -->
<th><fmt:message key="user"/></th>
<th><fmt:message key="stars"/></th>
<th><fmt:message key="opinion"/></th>
<th> </th>
@ -24,6 +25,9 @@
<!-- <td> -->
<%-- <c:out value="nombre por definir"/> <!-- ${review.usuario.nombre},${review.usuario.apellidos} --> --%>
<!-- </td> -->
<td>
<c:out value="${review.escritor.username}"/>
</td>
<td>
<c:out value="${review.stars}"/>
</td>