Работа с сущностью Клиент. Добавление поля логин и пароль.

This commit is contained in:
Programmist73 2023-05-12 19:19:37 +04:00
parent ce4fbf0aeb
commit fc65e71ffa
10 changed files with 61 additions and 42 deletions

View File

@ -40,7 +40,6 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
}
}
*/
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.headers().frameOptions().sameOrigin().and()
@ -74,9 +73,4 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
.requestMatchers("/templates/**")
.requestMatchers("/webjars/**");
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userService);
}
}

View File

@ -17,7 +17,7 @@ public class ClientDTO {
public ClientDTO(GameClient gameClient){
this.id = gameClient.getId();
this.nickName = gameClient.getNickName();
this.nickName = gameClient.getLogin();
this.email = gameClient.getEmail();
this.balance = gameClient.getBalance();
this.tanks = gameClient.getTanks().stream()

View File

@ -3,12 +3,11 @@ package premium_store.controller.DTO;
import com.fasterxml.jackson.annotation.JsonProperty;
import premium_store.model.GameClient;
import java.util.List;
//класс, который соединяет танки клиента в одну строчку (нам так захотелось)
public class SupportClientDTO {
private long id;
private String nickName;
private String login;
private String password;
private String email;
private Integer balance;
private Long tankId;
@ -17,7 +16,8 @@ public class SupportClientDTO {
public SupportClientDTO(GameClient gameClient){
this.id = gameClient.getId();
this.nickName = gameClient.getNickName();
this.login = gameClient.getLogin();
this.password = gameClient.getPassword();
this.email = gameClient.getEmail();
this.balance = gameClient.getBalance();
@ -38,12 +38,20 @@ public class SupportClientDTO {
this.id = id;
}
public String getNickName(){
return nickName;
public String getLogin(){
return login;
}
public void setNickName(String nickName) {
this.nickName = nickName;
public void setLogin(String login) {
this.login = login;
}
public String getPassword(){
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail(){

View File

@ -75,7 +75,7 @@ public class ClientMvcController {
}
if(id == null || id <= 0){
gameClientService.addClient(clientDTO.getNickName(), clientDTO.getNickName(), clientDTO.getBalance());
gameClientService.addClient(clientDTO.getLogin(), clientDTO.getLogin(), clientDTO.getPassword(), clientDTO.getBalance());
} else {
gameClientService.updateClient(clientDTO);
}

View File

@ -34,19 +34,21 @@ public class GameClientController {
}
@PostMapping("/")
public ClientDTO createClient(@RequestParam("nickName") String nickName,
public ClientDTO createClient(@RequestParam("login") String login,
@RequestParam("password") String password,
@RequestParam("email") String email,
@RequestParam("balance") Integer balance) {
return new ClientDTO(gameClientService.addClient(nickName, email, balance));
return new ClientDTO(gameClientService.addClient(login, email, password, balance));
}
@PutMapping("/{id}")
public ClientDTO updateClient(@PathVariable Long id,
@RequestParam("nickName") String nickName,
@RequestParam("login") String login,
@RequestParam("password") String password,
@RequestParam("email") String email,
@RequestParam("balance") Integer balance,
@RequestParam("tankId") Long tankId) {
return new ClientDTO(gameClientService.updateClient(id, nickName, email, balance, tankService.findTank(tankId)));
return new ClientDTO(gameClientService.updateClient(id, login, password, email, balance, tankService.findTank(tankId)));
}
@DeleteMapping("/{id}")

View File

@ -7,7 +7,6 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import premium_store.controller.DTO.ClientDTO;
import premium_store.controller.DTO.UserSignupDto;
import premium_store.model.GameClient;
import premium_store.service.UserService;
@ -43,7 +42,7 @@ public class UserSignupMvcController {
try {
final GameClient user = userService.createUser(
userSignupDto.getLogin(), userSignupDto.getPassword(), userSignupDto.getPasswordConfirm());
return "redirect:/login?created=" + user.getNickName();
return "redirect:/login?created=" + user.getLogin();
} catch (ValidationException e) {
model.addAttribute("errors", e.getMessage());
return "signup";

View File

@ -13,12 +13,15 @@ public class GameClient {
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "nickName", nullable = false, length = 255)
private String nickName;
@Column(name = "login", nullable = false, length = 255)
private String login;
@Column(name = "email", nullable = false, length = 255)
private String email;
@Column(name = "password", nullable = false, length = 255)
private String password;
@Column(name = "balance")
private Integer balance;
@ -27,9 +30,10 @@ public class GameClient {
public GameClient(){ }
public GameClient(String nickName, String email, Integer balance){
this.nickName = nickName;
public GameClient(String login, String email, String password, Integer balance){
this.login = login;
this.email = email;
this.password = password;
this.balance = balance;
}
@ -37,12 +41,12 @@ public class GameClient {
return id;
}
public String getNickName(){
return nickName;
public String getLogin(){
return login;
}
public void setNickName(String nickName){
this.nickName = nickName;
public void setLogin(String login){
this.login = login;
}
public String getEmail(){
@ -53,6 +57,14 @@ public class GameClient {
this.email = email;
}
public void setPassword(String password){
this.password = password;
}
public String getPassword(){
return password;
}
public Integer getBalance(){
return balance;
}
@ -93,7 +105,7 @@ public class GameClient {
public String toString() {
return "Client{" +
"id=" + id +
", nickName='" + nickName + '\'' +
", nickName='" + login + '\'' +
", email='" + email + '\'' +
", balance='" + balance + '\'' +
", tanks='" + tanks.stream().map(Object::toString).collect(Collectors.joining(", ")) + '\'' +

View File

@ -3,7 +3,6 @@ package premium_store.service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import premium_store.controller.DTO.ClientDTO;
import premium_store.controller.DTO.SupportClientDTO;
import premium_store.model.GameClient;
import premium_store.model.Tank;
@ -28,8 +27,8 @@ public class GameClientService {
}
@Transactional
public GameClient addClient(String newNickName, String newEmail, Integer newBallance) {
final GameClient gameClient = new GameClient(newNickName, newEmail, newBallance);
public GameClient addClient(String login, String newEmail, String password, Integer newBallance) {
final GameClient gameClient = new GameClient(login, newEmail, password, newBallance);
validatorUtil.validate(gameClient);
return gameClientRepository.save(gameClient);
@ -48,15 +47,19 @@ public class GameClientService {
}
@Transactional
public GameClient updateClient(Long id, String newNickName, String newEmail, Integer newBalance, Tank newTank) {
public GameClient updateClient(Long id, String login, String newPassword, String newEmail, Integer newBalance, Tank newTank) {
if (id <= 0) {
throw new IllegalArgumentException("Invalid id");
}
final GameClient currentGameClient = findClient(id);
if (StringUtils.hasText(newNickName)){
currentGameClient.setNickName(newNickName);
if (StringUtils.hasText(login)){
currentGameClient.setLogin(login);
}
if(StringUtils.hasText(newPassword)){
currentGameClient.setPassword(newPassword);
}
if(StringUtils.hasText(newEmail)){
@ -82,7 +85,8 @@ public class GameClientService {
final GameClient currentGameClient = findClient(clientDTO.getId());
currentGameClient.setNickName(clientDTO.getNickName());
currentGameClient.setLogin(clientDTO.getLogin());
currentGameClient.setPassword(clientDTO.getPassword());
currentGameClient.setEmail(clientDTO.getEmail());
currentGameClient.setBalance(clientDTO.getBalance());

View File

@ -56,7 +56,7 @@ public class UserService implements UserDetailsService {
}
return new org.springframework.security.core.userdetails.User(
userEntity.getNickName(), userEntity.getEmail(), Collections.emptyList());
userEntity.getLogin(), userEntity.getEmail(), Collections.emptyList());
}
}

View File

@ -39,7 +39,7 @@ class PremiumStoreApplicationTests {
void testClientRead() {
gameClientService.deleteAllClients();
GameClient client = gameClientService.addClient("3tankista73", "fff@mail.ru", 3400);
GameClient client = gameClientService.addClient("3tankista73", "fff@mail.ru", "user", 3400);
log.info(client.toString());
GameClient findClient = gameClientService.findClient(client.getId());
@ -126,10 +126,10 @@ class PremiumStoreApplicationTests {
void testClientCreate() {
gameClientService.deleteAllClients();
GameClient firstClient = gameClientService.addClient("Barbarian", "dsfg@gmail.com", 56000);
GameClient firstClient = gameClientService.addClient("Barbarian", "dsfg@gmail.com", "user", 56000);
log.info(firstClient.toString());
GameClient secondClient = gameClientService.addClient("KorbenDallas", "tankoviyGeniy@mail.ru", 37000);
GameClient secondClient = gameClientService.addClient("KorbenDallas", "tankoviyGeniy@mail.ru", "user", 37000);
log.info(secondClient.toString());
Assertions.assertEquals(gameClientService.findAllClients().size(), 2);