diff --git a/src/main/java/ru/ulstu/admin/model/AspirantForm.java b/src/main/java/ru/ulstu/admin/model/AspirantForm.java index 828bdb1..abbfd84 100644 --- a/src/main/java/ru/ulstu/admin/model/AspirantForm.java +++ b/src/main/java/ru/ulstu/admin/model/AspirantForm.java @@ -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; + } } diff --git a/src/main/java/ru/ulstu/admin/service/AdminAspirantService.java b/src/main/java/ru/ulstu/admin/service/AdminAspirantService.java index 3889aa1..a0d35e7 100644 --- a/src/main/java/ru/ulstu/admin/service/AdminAspirantService.java +++ b/src/main/java/ru/ulstu/admin/service/AdminAspirantService.java @@ -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 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); } diff --git a/src/main/java/ru/ulstu/aspirant/model/Aspirant.java b/src/main/java/ru/ulstu/aspirant/model/Aspirant.java index a13d965..b0d92cd 100644 --- a/src/main/java/ru/ulstu/aspirant/model/Aspirant.java +++ b/src/main/java/ru/ulstu/aspirant/model/Aspirant.java @@ -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; + } } diff --git a/src/main/java/ru/ulstu/user/UserService.java b/src/main/java/ru/ulstu/user/UserService.java index 0911e41..9634e54 100644 --- a/src/main/java/ru/ulstu/user/UserService.java +++ b/src/main/java/ru/ulstu/user/UserService.java @@ -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) { diff --git a/src/main/resources/templates/admin/editAspirant.html b/src/main/resources/templates/admin/editAspirant.html index 98b104a..92797ed 100644 --- a/src/main/resources/templates/admin/editAspirant.html +++ b/src/main/resources/templates/admin/editAspirant.html @@ -8,6 +8,7 @@ th:object="${aspirant}" method="post"> +
+
+ + +

+ Не может быть пустым +

+
+