Partially add aspirant user

This commit is contained in:
Anton Romanov 2025-01-30 16:40:36 +04:00
parent fc480dd559
commit d36bdf6eca
5 changed files with 72 additions and 14 deletions

View File

@ -9,24 +9,17 @@ import java.util.Date;
public class AspirantForm {
private Integer id;
private String surname;
private String name;
private String patronymic;
private Course course;
private Integer managerId;
private Date birthDate;
private Speciality speciality;
private String theme;
private Base base;
private Integer userId;
private String email;
public AspirantForm() {
}
@ -42,6 +35,8 @@ public class AspirantForm {
this.birthDate = aspirant.getBirthDate();
this.speciality = aspirant.getSpeciality();
this.theme = aspirant.getTheme();
this.userId = aspirant.getUser() == null ? null : aspirant.getUser().getId();
this.email = aspirant.getUser() == null ? null : aspirant.getUser().getLogin();
}
public Integer getId() {
@ -123,4 +118,20 @@ public class AspirantForm {
public void setBase(Base base) {
this.base = base;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}

View File

@ -6,6 +6,8 @@ import ru.ulstu.aspirant.model.Aspirant;
import ru.ulstu.aspirant.service.AspirantService;
import ru.ulstu.manager.model.Manager;
import ru.ulstu.manager.service.ManagerService;
import ru.ulstu.model.User;
import ru.ulstu.user.UserService;
import java.util.List;
@ -13,11 +15,14 @@ import java.util.List;
public class AdminAspirantService {
private final AspirantService aspirantService;
private final ManagerService managerService;
private final UserService userService;
public AdminAspirantService(AspirantService aspirantService,
ManagerService managerService) {
ManagerService managerService,
UserService userService) {
this.aspirantService = aspirantService;
this.managerService = managerService;
this.userService = userService;
}
public List<Aspirant> getAspirants() {
@ -41,6 +46,13 @@ public class AdminAspirantService {
aspirant.setBirthDate(aspirantForm.getBirthDate());
aspirant.setSpeciality(aspirantForm.getSpeciality());
aspirant.setTheme(aspirantForm.getTheme());
User user = userService.getUserById(aspirantForm.getUserId());
if (!user.getLogin().equals(aspirantForm.getEmail())) {
user.setLogin(aspirantForm.getEmail());
userService.createUser(user);
}
aspirant.setUser(user);
aspirantService.save(aspirant);
}

View File

@ -11,6 +11,7 @@ import jakarta.validation.constraints.NotNull;
import ru.ulstu.indicator.model.Course;
import ru.ulstu.manager.model.Manager;
import ru.ulstu.model.BaseEntity;
import ru.ulstu.model.User;
import java.util.Date;
@ -41,6 +42,9 @@ public class Aspirant extends BaseEntity {
@Enumerated(EnumType.STRING)
private Base base;
@ManyToOne
private User user;
public Aspirant() {
}
@ -120,4 +124,12 @@ public class Aspirant extends BaseEntity {
public void setBase(Base base) {
this.base = base;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}

View File

@ -57,11 +57,19 @@ public class UserService implements UserDetailsService {
if (getUserByLogin(user.getLogin()) != null) {
throw new RuntimeException(user.getLogin());
}
User dbUser = (user.getId() == null)
? user
: getUserById(user.getId());
//user.setRoles(Collections.singleton(new UserRole(UserRoleConstants.USER)));
user.setPassword(passwordEncoder.encode(user.getPassword()));
user = userRepository.save(user);
log.debug("Created Information for User: {}", user.getLogin());
return user;
dbUser.setPassword(passwordEncoder.encode(user.getPassword()));
dbUser.setLogin(user.getLogin());
dbUser = userRepository.save(dbUser);
log.debug("Created Information for User: {}", dbUser.getLogin());
return dbUser;
}
public User getUserById(Integer id) {
return userRepository.findById(id).orElseThrow(() -> new RuntimeException("User not found by id"));
}
private void createDefaultUser(String login, String userRole) {

View File

@ -8,6 +8,7 @@
th:object="${aspirant}"
method="post">
<input type="hidden" th:field="*{id}">
<input type="hidden" th:field="*{userId}">
<div class="form-group">
<label for="surname">Фамилия</label>
<input th:field="*{surname}"
@ -49,6 +50,20 @@
</p>
</div>
<div class="form-group">
<label for="email">Email</label>
<input th:field="*{email}"
id="email"
required
type="email"
class="form-control"
placeholder="Email">
<p th:if="${#fields.hasErrors('email')}"
th:class="${#fields.hasErrors('email')}? error">
Не может быть пустым
</p>
</div>
<div class="form-group">
<label for="course">Курс</label>
<select class="form-select form-control"