Another fixation :)
This commit is contained in:
parent
c3577d6ad5
commit
cefa4a0c02
@ -2,6 +2,7 @@ package premium_store.controller.DTO;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import premium_store.model.GameClient;
|
||||
import premium_store.model.UserRole;
|
||||
|
||||
//класс, который соединяет танки клиента в одну строчку (нам так захотелось)
|
||||
public class SupportClientDTO {
|
||||
@ -11,6 +12,7 @@ public class SupportClientDTO {
|
||||
private String email;
|
||||
private Integer balance;
|
||||
private Long tankId;
|
||||
private UserRole role;
|
||||
|
||||
public SupportClientDTO(){ }
|
||||
|
||||
@ -20,7 +22,7 @@ public class SupportClientDTO {
|
||||
this.password = gameClient.getPassword();
|
||||
this.email = gameClient.getEmail();
|
||||
this.balance = gameClient.getBalance();
|
||||
|
||||
this.role = gameClient.getRole();
|
||||
if(gameClient.getTanks().size() >= 1){
|
||||
this.tankId = gameClient.getTanks().get(gameClient.getTanks().size() - 1).getId();
|
||||
}
|
||||
@ -77,4 +79,12 @@ public class SupportClientDTO {
|
||||
public void setTankId(Long tankId) {
|
||||
this.tankId = tankId;
|
||||
}
|
||||
|
||||
public UserRole getRole() {
|
||||
return role;
|
||||
}
|
||||
|
||||
public void setRole(UserRole role) {
|
||||
this.role = role;
|
||||
}
|
||||
}
|
@ -1,15 +1,25 @@
|
||||
package premium_store.controller.controller;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.security.access.annotation.Secured;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import premium_store.controller.DTO.*;
|
||||
import premium_store.model.UserRole;
|
||||
import premium_store.service.GameClientService;
|
||||
import premium_store.service.TankService;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.ValidationException;
|
||||
import java.security.Principal;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/client")
|
||||
@ -23,18 +33,40 @@ public class ClientMvcController {
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public String getClients(Model model){
|
||||
model.addAttribute("clients",
|
||||
gameClientService.findAllClients().stream()
|
||||
.map(ClientDTO::new)
|
||||
.toList());
|
||||
|
||||
return "client";
|
||||
public String showUpdateUserForm(Principal principal, Model model) {
|
||||
ClientDTO clientDTO = new ClientDTO(gameClientService.findByLogin(principal.getName()));
|
||||
model.addAttribute("clientDTO", clientDTO);
|
||||
return "client-edit";
|
||||
}
|
||||
|
||||
@GetMapping(value = {"/edit", "/edit/{id}"})
|
||||
public String editClient(@PathVariable(required = false) Long id, Model model){
|
||||
if(id == null || id <= 0){
|
||||
@GetMapping(value = "/all")
|
||||
@Secured({UserRole.AsString.ADMIN})
|
||||
public String getClients(@RequestParam(defaultValue = "1") int page,
|
||||
@RequestParam(defaultValue = "5") int size,
|
||||
Principal principal, Model model) {
|
||||
final Page<ClientDTO> users = gameClientService.findAllPages(page, size)
|
||||
.map(ClientDTO::new);
|
||||
|
||||
model.addAttribute("users", users);
|
||||
|
||||
final int totalPages = users.getTotalPages();
|
||||
|
||||
final List<Integer> pageNumbers = IntStream.rangeClosed(1, totalPages)
|
||||
.boxed()
|
||||
.toList();
|
||||
|
||||
model.addAttribute("pages", pageNumbers);
|
||||
model.addAttribute("totalPages", totalPages);
|
||||
|
||||
return "clients";
|
||||
}
|
||||
|
||||
//@GetMapping(value = {"/edit", "/edit/{id}"})
|
||||
@PostMapping
|
||||
public String editClient(@ModelAttribute @Valid ClientDTO userDto,
|
||||
BindingResult bindingResult,
|
||||
Model model) {
|
||||
/*if(id == null || id <= 0){
|
||||
model.addAttribute("supportClientDTO", new SupportClientDTO());
|
||||
}
|
||||
else {
|
||||
@ -46,7 +78,29 @@ public class ClientMvcController {
|
||||
.map(TankDTO::new)
|
||||
.toList();
|
||||
|
||||
model.addAttribute("tanks", tanks);
|
||||
model.addAttribute("tanks", tanks);*/
|
||||
|
||||
if (bindingResult.hasErrors()) {
|
||||
model.addAttribute("errors", bindingResult.getAllErrors());
|
||||
return "user";
|
||||
}
|
||||
|
||||
try {
|
||||
gameClientService.updateClient(userDto);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Collection<SimpleGrantedAuthority> nowAuthorities =
|
||||
(Collection<SimpleGrantedAuthority>) SecurityContextHolder.getContext()
|
||||
.getAuthentication()
|
||||
.getAuthorities();
|
||||
|
||||
UsernamePasswordAuthenticationToken authentication =
|
||||
new UsernamePasswordAuthenticationToken(userDto.getLogin(), userDto.getPassword(), nowAuthorities);
|
||||
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
} catch (ValidationException e) {
|
||||
model.addAttribute("errors", e.getMessage());
|
||||
}
|
||||
|
||||
return "client-edit";
|
||||
}
|
||||
@ -66,7 +120,7 @@ public class ClientMvcController {
|
||||
|
||||
@PostMapping(value = {"", "/{id}"})
|
||||
public String saveClient(@PathVariable(required = false) Long id,
|
||||
@ModelAttribute @Valid SupportClientDTO clientDTO,
|
||||
@ModelAttribute @Valid ClientDTO clientDTO,
|
||||
BindingResult bindingResult,
|
||||
Model model){
|
||||
if(bindingResult.hasErrors()){
|
||||
@ -75,7 +129,7 @@ public class ClientMvcController {
|
||||
}
|
||||
|
||||
if(id == null || id <= 0){
|
||||
gameClientService.addClient(clientDTO.getLogin(), clientDTO.getLogin(), clientDTO.getPassword(), clientDTO.getBalance());
|
||||
gameClientService.addClient(clientDTO.getLogin(), clientDTO.getPassword(), clientDTO.getEmail(), clientDTO.getBalance(), clientDTO.getPassword(), clientDTO.getRole());
|
||||
} else {
|
||||
gameClientService.updateClient(clientDTO);
|
||||
}
|
||||
@ -84,7 +138,9 @@ public class ClientMvcController {
|
||||
}
|
||||
|
||||
@PostMapping("/delete/{id}")
|
||||
public String deleteClient(@PathVariable Long id){
|
||||
@Secured({UserRole.AsString.ADMIN})
|
||||
public String deleteClient(Principal principal, Model model,
|
||||
@PathVariable Long id){
|
||||
gameClientService.deleteClient(id);
|
||||
return "redirect:/tank";
|
||||
}
|
||||
|
@ -130,7 +130,7 @@ public class GameClientService implements UserDetailsService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public GameClient updateClient(SupportClientDTO clientDTO) {
|
||||
public GameClient updateClient(ClientDTO clientDTO) {
|
||||
if (clientDTO.getId() <= 0) {
|
||||
throw new IllegalArgumentException("Invalid id");
|
||||
}
|
||||
@ -152,9 +152,9 @@ public class GameClientService implements UserDetailsService {
|
||||
currentGameClient.setEmail(clientDTO.getEmail());
|
||||
currentGameClient.setBalance(clientDTO.getBalance());
|
||||
|
||||
if(clientDTO.getTankId() != null){
|
||||
/*if(clientDTO.getTankId() != null){
|
||||
currentGameClient.setTanks(tankRepository.getById(clientDTO.getTankId()));
|
||||
}
|
||||
}*/
|
||||
|
||||
return gameClientRepository.save(currentGameClient);
|
||||
}
|
||||
|
@ -4,7 +4,11 @@ import java.util.Set;
|
||||
|
||||
//класс для передачи списка ошибок, если они возникают
|
||||
public class ValidationException extends RuntimeException {
|
||||
public ValidationException(Set<String> errors) {
|
||||
public <T> ValidationException(Set<String> errors) {
|
||||
super(String.join("\n", errors));
|
||||
}
|
||||
|
||||
public <T> ValidationException(String error) {
|
||||
super(error);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user