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 com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import premium_store.model.GameClient;
|
import premium_store.model.GameClient;
|
||||||
|
import premium_store.model.UserRole;
|
||||||
|
|
||||||
//класс, который соединяет танки клиента в одну строчку (нам так захотелось)
|
//класс, который соединяет танки клиента в одну строчку (нам так захотелось)
|
||||||
public class SupportClientDTO {
|
public class SupportClientDTO {
|
||||||
@ -11,6 +12,7 @@ public class SupportClientDTO {
|
|||||||
private String email;
|
private String email;
|
||||||
private Integer balance;
|
private Integer balance;
|
||||||
private Long tankId;
|
private Long tankId;
|
||||||
|
private UserRole role;
|
||||||
|
|
||||||
public SupportClientDTO(){ }
|
public SupportClientDTO(){ }
|
||||||
|
|
||||||
@ -20,7 +22,7 @@ public class SupportClientDTO {
|
|||||||
this.password = gameClient.getPassword();
|
this.password = gameClient.getPassword();
|
||||||
this.email = gameClient.getEmail();
|
this.email = gameClient.getEmail();
|
||||||
this.balance = gameClient.getBalance();
|
this.balance = gameClient.getBalance();
|
||||||
|
this.role = gameClient.getRole();
|
||||||
if(gameClient.getTanks().size() >= 1){
|
if(gameClient.getTanks().size() >= 1){
|
||||||
this.tankId = gameClient.getTanks().get(gameClient.getTanks().size() - 1).getId();
|
this.tankId = gameClient.getTanks().get(gameClient.getTanks().size() - 1).getId();
|
||||||
}
|
}
|
||||||
@ -77,4 +79,12 @@ public class SupportClientDTO {
|
|||||||
public void setTankId(Long tankId) {
|
public void setTankId(Long tankId) {
|
||||||
this.tankId = 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;
|
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.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import premium_store.controller.DTO.*;
|
import premium_store.controller.DTO.*;
|
||||||
|
import premium_store.model.UserRole;
|
||||||
import premium_store.service.GameClientService;
|
import premium_store.service.GameClientService;
|
||||||
import premium_store.service.TankService;
|
import premium_store.service.TankService;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
import javax.validation.ValidationException;
|
||||||
|
import java.security.Principal;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping("/client")
|
@RequestMapping("/client")
|
||||||
@ -23,18 +33,40 @@ public class ClientMvcController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public String getClients(Model model){
|
public String showUpdateUserForm(Principal principal, Model model) {
|
||||||
model.addAttribute("clients",
|
ClientDTO clientDTO = new ClientDTO(gameClientService.findByLogin(principal.getName()));
|
||||||
gameClientService.findAllClients().stream()
|
model.addAttribute("clientDTO", clientDTO);
|
||||||
.map(ClientDTO::new)
|
return "client-edit";
|
||||||
.toList());
|
|
||||||
|
|
||||||
return "client";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(value = {"/edit", "/edit/{id}"})
|
@GetMapping(value = "/all")
|
||||||
public String editClient(@PathVariable(required = false) Long id, Model model){
|
@Secured({UserRole.AsString.ADMIN})
|
||||||
if(id == null || id <= 0){
|
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());
|
model.addAttribute("supportClientDTO", new SupportClientDTO());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -46,7 +78,29 @@ public class ClientMvcController {
|
|||||||
.map(TankDTO::new)
|
.map(TankDTO::new)
|
||||||
.toList();
|
.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";
|
return "client-edit";
|
||||||
}
|
}
|
||||||
@ -66,7 +120,7 @@ public class ClientMvcController {
|
|||||||
|
|
||||||
@PostMapping(value = {"", "/{id}"})
|
@PostMapping(value = {"", "/{id}"})
|
||||||
public String saveClient(@PathVariable(required = false) Long id,
|
public String saveClient(@PathVariable(required = false) Long id,
|
||||||
@ModelAttribute @Valid SupportClientDTO clientDTO,
|
@ModelAttribute @Valid ClientDTO clientDTO,
|
||||||
BindingResult bindingResult,
|
BindingResult bindingResult,
|
||||||
Model model){
|
Model model){
|
||||||
if(bindingResult.hasErrors()){
|
if(bindingResult.hasErrors()){
|
||||||
@ -75,7 +129,7 @@ public class ClientMvcController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(id == null || id <= 0){
|
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 {
|
} else {
|
||||||
gameClientService.updateClient(clientDTO);
|
gameClientService.updateClient(clientDTO);
|
||||||
}
|
}
|
||||||
@ -84,7 +138,9 @@ public class ClientMvcController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/delete/{id}")
|
@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);
|
gameClientService.deleteClient(id);
|
||||||
return "redirect:/tank";
|
return "redirect:/tank";
|
||||||
}
|
}
|
||||||
|
@ -130,7 +130,7 @@ public class GameClientService implements UserDetailsService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public GameClient updateClient(SupportClientDTO clientDTO) {
|
public GameClient updateClient(ClientDTO clientDTO) {
|
||||||
if (clientDTO.getId() <= 0) {
|
if (clientDTO.getId() <= 0) {
|
||||||
throw new IllegalArgumentException("Invalid id");
|
throw new IllegalArgumentException("Invalid id");
|
||||||
}
|
}
|
||||||
@ -152,9 +152,9 @@ public class GameClientService implements UserDetailsService {
|
|||||||
currentGameClient.setEmail(clientDTO.getEmail());
|
currentGameClient.setEmail(clientDTO.getEmail());
|
||||||
currentGameClient.setBalance(clientDTO.getBalance());
|
currentGameClient.setBalance(clientDTO.getBalance());
|
||||||
|
|
||||||
if(clientDTO.getTankId() != null){
|
/*if(clientDTO.getTankId() != null){
|
||||||
currentGameClient.setTanks(tankRepository.getById(clientDTO.getTankId()));
|
currentGameClient.setTanks(tankRepository.getById(clientDTO.getTankId()));
|
||||||
}
|
}*/
|
||||||
|
|
||||||
return gameClientRepository.save(currentGameClient);
|
return gameClientRepository.save(currentGameClient);
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,11 @@ import java.util.Set;
|
|||||||
|
|
||||||
//класс для передачи списка ошибок, если они возникают
|
//класс для передачи списка ошибок, если они возникают
|
||||||
public class ValidationException extends RuntimeException {
|
public class ValidationException extends RuntimeException {
|
||||||
public ValidationException(Set<String> errors) {
|
public <T> ValidationException(Set<String> errors) {
|
||||||
super(String.join("\n", errors));
|
super(String.join("\n", errors));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <T> ValidationException(String error) {
|
||||||
|
super(error);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user