Допилил админку пользователей, теперь доступны все поля

This commit is contained in:
Никита Потапов 2024-06-20 15:07:01 +04:00
parent a28e35797b
commit 2365d240cb
4 changed files with 130 additions and 20 deletions

View File

@ -73,6 +73,11 @@ public class AdminUserController {
userEditDTO.setId(userEntity.getId()); userEditDTO.setId(userEntity.getId());
userEditDTO.setUsername(userEntity.getUsername()); userEditDTO.setUsername(userEntity.getUsername());
userEditDTO.setRole(userEntity.getRole()); userEditDTO.setRole(userEntity.getRole());
userEditDTO.setName(userEntity.getName());
userEditDTO.setSurname(userEntity.getSurname());
userEditDTO.setCity(userEntity.getCity());
userEditDTO.setBirthday(userEntity.getBirthday());
userEditDTO.setStatus(userEntity.getStatus());
model.addAttribute(USER_ATTRIBUTE, userEditDTO); model.addAttribute(USER_ATTRIBUTE, userEditDTO);
model.addAttribute(PAGE_ATTRIBUTE, page); model.addAttribute(PAGE_ATTRIBUTE, page);
@ -91,6 +96,34 @@ public class AdminUserController {
return USER_EDIT_VIEW; return USER_EDIT_VIEW;
} }
UserEntity userEntity;
if (userEditDTO.getId() == null) {
userEntity = new UserEntity();
} else {
userEntity = userService.get(userEditDTO.getId());
}
userEntity.setUsername(userEditDTO.getUsername());
userEntity.setRole(userEditDTO.getRole());
userEntity.setName(userEditDTO.getName());
userEntity.setSurname(userEditDTO.getSurname());
userEntity.setCity(userEditDTO.getCity());
userEntity.setBirthday(userEditDTO.getBirthday());
userEntity.setStatus(userEditDTO.getStatus());
if (userEditDTO.getPassword() != null && userEditDTO.getPassword() != "") {
userEntity.setPassword(userEditDTO.getPassword());
}
if (userService.checkUserWithSameUsernameExist(userEntity)) {
bindingResult.rejectValue("username", "userEdit:username", "Пользователь с таким логином уже есть");
model.addAttribute(PAGE_ATTRIBUTE, page);
userEditDTO.setPasswordConfirm(null);
model.addAttribute(USER_ATTRIBUTE, userEditDTO);
return USER_EDIT_VIEW;
}
if (userEditDTO.getId() == null && (userEditDTO.getPassword() == null || userEditDTO.getPassword() == "")) { if (userEditDTO.getId() == null && (userEditDTO.getPassword() == null || userEditDTO.getPassword() == "")) {
bindingResult.rejectValue("password", "userEdit:passwords", "Укажите пароль"); bindingResult.rejectValue("password", "userEdit:passwords", "Укажите пароль");
model.addAttribute(PAGE_ATTRIBUTE, page); model.addAttribute(PAGE_ATTRIBUTE, page);
@ -109,21 +142,6 @@ public class AdminUserController {
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page); redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
UserEntity userEntity;
if (userEditDTO.getId() == null) {
userEntity = new UserEntity();
} else {
userEntity = userService.get(userEditDTO.getId());
}
userEntity.setUsername(userEditDTO.getUsername());
userEntity.setRole(userEditDTO.getRole());
if (userEditDTO.getPassword() != null && userEditDTO.getPassword() != "") {
userEntity.setPassword(userEditDTO.getPassword());
}
if (userEditDTO.getId() == null) { if (userEditDTO.getId() == null) {
userService.create(userEntity); userService.create(userEntity);
} else { } else {

View File

@ -1,7 +1,12 @@
package com.example.nekontakte.users.api; package com.example.nekontakte.users.api;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
import com.example.nekontakte.users.model.UserRole; import com.example.nekontakte.users.model.UserRole;
import groovyjarjarantlr4.v4.runtime.misc.NotNull;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size; import jakarta.validation.constraints.Size;
@ -15,8 +20,23 @@ public class UserEditDTO {
private String passwordConfirm; private String passwordConfirm;
@NotNull
@NotBlank
private UserRole role; private UserRole role;
private String name;
private String surname;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date birthday;
private String city;
private String avatarImg;
private String status;
public UserRole getRole() { public UserRole getRole() {
return role; return role;
} }
@ -56,4 +76,52 @@ public class UserEditDTO {
public void setPasswordConfirm(String passwordConfirm) { public void setPasswordConfirm(String passwordConfirm) {
this.passwordConfirm = passwordConfirm; this.passwordConfirm = passwordConfirm;
} }
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getAvatarImg() {
return avatarImg;
}
public void setAvatarImg(String avatarImg) {
this.avatarImg = avatarImg;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
} }

View File

@ -21,13 +21,17 @@ import java.util.stream.StreamSupport;
public class UserService implements UserDetailsService { public class UserService implements UserDetailsService {
private final UserRepository repository; private final UserRepository repository;
private boolean checkUserWithSameUsernameExist(String username) { public boolean checkUserWithSameUsernameExist(String username) {
return repository.findByUsernameIgnoreCase(username).isPresent(); return repository.findByUsernameIgnoreCase(username).isPresent();
} }
private boolean checkUserWithSameUsernameExist(UserEntity user) { public boolean checkUserWithSameUsernameExist(UserEntity user) {
var existUser = repository.findByUsernameIgnoreCase(user.getUsername()); var existUser = repository.findByUsernameIgnoreCase(user.getUsername());
return existUser.isPresent() && (int) existUser.get().getId() != (int) user.getId(); if (existUser.isEmpty())
return false;
if (user.getId() == null)
return true;
return (int) existUser.get().getId() != (int) user.getId();
} }
public UserService(UserRepository repository) { public UserService(UserRepository repository) {

View File

@ -10,12 +10,12 @@
<form action="#" th:action="@{/admin/users/edit(page=${page})}" th:object="${user}" method="post"> <form action="#" th:action="@{/admin/users/edit(page=${page})}" th:object="${user}" method="post">
<input type="hidden" th:field="*{id}" id="id" class="form-control" readonly> <input type="hidden" th:field="*{id}" id="id" class="form-control" readonly>
<div class="mb-3"> <div class="mb-3">
<label for="username" class="form-label">Имя пользователя</label> <label for="username" class="form-label">Логин*</label>
<input type="text" th:field="*{username}" id="username" class="form-control"> <input type="text" th:field="*{username}" id="username" class="form-control">
<div th:if="${#fields.hasErrors('username')}" th:errors="*{username}" class="invalid-feedback"></div> <div th:if="${#fields.hasErrors('username')}" th:errors="*{username}" class="invalid-feedback"></div>
</div> </div>
<div class="mb-3"> <div class="mb-3">
<label for="userRole" class="form-label">Роль в системе</label> <label for="userRole" class="form-label">Роль в системе*</label>
<select th:field="*{role}" id="userRole" class="form-control"> <select th:field="*{role}" id="userRole" class="form-control">
<option <option
th:each="userRoleOpt : ${T(com.example.nekontakte.users.model.UserRole).values()}" th:each="userRoleOpt : ${T(com.example.nekontakte.users.model.UserRole).values()}"
@ -34,6 +34,26 @@
<input type="password" th:field="*{passwordConfirm}" id="passwordConfirm" class="form-control"> <input type="password" th:field="*{passwordConfirm}" id="passwordConfirm" class="form-control">
<div th:if="${#fields.hasErrors('passwordConfirm')}" th:errors="*{passwordConfirm}" class="invalid-feedback"></div> <div th:if="${#fields.hasErrors('passwordConfirm')}" th:errors="*{passwordConfirm}" class="invalid-feedback"></div>
</div> </div>
<div class="mb-3">
<label for="name" class="form-label">Имя</label>
<input type="text" th:field="*{name}" id="name" class="form-control">
</div>
<div class="mb-3">
<label for="surname" class="form-label">Имя пользователя</label>
<input type="text" th:field="*{surname}" id="surname" class="form-control">
</div>
<div class="mb-3">
<label for="birthday" class="form-label">Дата рождения</label>
<input type="date" th:field="*{birthday}" id="birthday" class="form-control">
</div>
<div class="mb-3">
<label for="city" class="form-label">Место проживания</label>
<input type="text" th:field="*{city}" id="city" class="form-control">
</div>
<div class="mb-3">
<label for="status" class="form-label">Статус</label>
<input type="text" th:field="*{status}" id="status" class="form-control">
</div>
<button class="btn btn-success me-3" type="submit">Применить</button> <button class="btn btn-success me-3" type="submit">Применить</button>
<a th:href="@{/admin/users(page=${page})}" class="btn btn-danger" >Отмена</a> <a th:href="@{/admin/users(page=${page})}" class="btn btn-danger" >Отмена</a>
</form> </form>