From 7e4d8d491845e70885e184e02ea6acf514bb2670 Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Wed, 17 Apr 2019 14:51:19 +0400 Subject: [PATCH] #69 added method take part in conference --- .../controller/ConferenceController.java | 9 ++++ .../ru/ulstu/conference/model/Conference.java | 9 ++-- .../ulstu/conference/model/ConferenceDto.java | 18 ++++--- .../repository/ConferenceUserRepository.java | 7 +++ .../conference/service/ConferenceService.java | 17 ++++++- .../service/ConferenceUserService.java | 48 +++++++++++++++++++ .../ru/ulstu/user/model/UserConference.java | 47 ++++++++++++++++++ .../templates/conferences/conference.html | 25 ++++++---- 8 files changed, 154 insertions(+), 26 deletions(-) create mode 100644 src/main/java/ru/ulstu/conference/repository/ConferenceUserRepository.java create mode 100644 src/main/java/ru/ulstu/conference/service/ConferenceUserService.java diff --git a/src/main/java/ru/ulstu/conference/controller/ConferenceController.java b/src/main/java/ru/ulstu/conference/controller/ConferenceController.java index 823f853..c25009a 100644 --- a/src/main/java/ru/ulstu/conference/controller/ConferenceController.java +++ b/src/main/java/ru/ulstu/conference/controller/ConferenceController.java @@ -104,6 +104,15 @@ public class ConferenceController { return CONFERENCE_PAGE; } + @PostMapping(value = "/conference", params = "takePart") + public String takePart(@Valid ConferenceDto conferenceDto, Errors errors) throws IOException { + if (errors.hasErrors()) { + return CONFERENCE_PAGE; + } + conferenceService.takePart(conferenceDto); + return CONFERENCE_PAGE; + } + public List getNotSelectPapers(List paperIds) { return conferenceService.getConferencePapers(paperIds); } diff --git a/src/main/java/ru/ulstu/conference/model/Conference.java b/src/main/java/ru/ulstu/conference/model/Conference.java index c9cf20e..bd1d907 100644 --- a/src/main/java/ru/ulstu/conference/model/Conference.java +++ b/src/main/java/ru/ulstu/conference/model/Conference.java @@ -23,9 +23,7 @@ import javax.persistence.Temporal; import javax.persistence.TemporalType; import java.util.ArrayList; import java.util.Date; -import java.util.HashSet; import java.util.List; -import java.util.Set; @Entity @Table(name = "conference") @@ -66,7 +64,8 @@ public class Conference extends BaseEntity { @JoinTable(name = "users_conference", joinColumns = {@JoinColumn(name = "conference_id")}, inverseJoinColumns = {@JoinColumn(name = "users_id")}) - private Set users = new HashSet<>(); + @Fetch(FetchMode.SUBSELECT) + private List users = new ArrayList<>(); public String getTitle() { return title; @@ -132,11 +131,11 @@ public class Conference extends BaseEntity { this.papers = papers; } - public Set getUsers() { + public List getUsers() { return users; } - public void setUsers(Set users) { + public void setUsers(List users) { this.users = users; } } diff --git a/src/main/java/ru/ulstu/conference/model/ConferenceDto.java b/src/main/java/ru/ulstu/conference/model/ConferenceDto.java index 279b13f..c45cd05 100644 --- a/src/main/java/ru/ulstu/conference/model/ConferenceDto.java +++ b/src/main/java/ru/ulstu/conference/model/ConferenceDto.java @@ -13,9 +13,7 @@ import javax.persistence.TemporalType; import javax.validation.constraints.Size; import java.util.ArrayList; import java.util.Date; -import java.util.HashSet; import java.util.List; -import java.util.Set; import static ru.ulstu.core.util.StreamApiUtils.convert; @@ -38,11 +36,11 @@ public class ConferenceDto { private Date endDate = new Date(); private List deadlines = new ArrayList<>(); private List removedDeadlineIds = new ArrayList<>(); - private Set userIds = new HashSet<>(); + private List userIds = new ArrayList<>(); private List paperIds = new ArrayList<>(); private List papers = new ArrayList<>(); private List notSelectedPapers = new ArrayList<>(); - private Set users = new HashSet<>(); + private List users = new ArrayList<>(); private Integer filterUserId; public ConferenceDto() { @@ -57,9 +55,9 @@ public class ConferenceDto { @JsonProperty("beginDate") Date beginDate, @JsonProperty("endDate") Date endDate, @JsonProperty("deadlines") List deadlines, - @JsonProperty("userIds") Set userIds, + @JsonProperty("userIds") List userIds, @JsonProperty("paperIds") List paperIds, - @JsonProperty("users") Set users, + @JsonProperty("users") List users, @JsonProperty("papers") List papers, @JsonProperty("notSelectedPapers") List notSelectedPapers) { this.id = id; @@ -157,11 +155,11 @@ public class ConferenceDto { this.deadlines = deadlines; } - public Set getUserIds() { + public List getUserIds() { return userIds; } - public void setUserIds(Set userIds) { + public void setUserIds(List userIds) { this.userIds = userIds; } @@ -181,11 +179,11 @@ public class ConferenceDto { this.papers = papers; } - public Set getUsers() { + public List getUsers() { return users; } - public void setUsers(Set users) { + public void setUsers(List users) { this.users = users; } diff --git a/src/main/java/ru/ulstu/conference/repository/ConferenceUserRepository.java b/src/main/java/ru/ulstu/conference/repository/ConferenceUserRepository.java new file mode 100644 index 0000000..0eb546c --- /dev/null +++ b/src/main/java/ru/ulstu/conference/repository/ConferenceUserRepository.java @@ -0,0 +1,7 @@ +package ru.ulstu.conference.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import ru.ulstu.user.model.UserConference; + +public interface ConferenceUserRepository extends JpaRepository { +} diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceService.java b/src/main/java/ru/ulstu/conference/service/ConferenceService.java index 4d267d1..58e8da8 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceService.java @@ -9,6 +9,8 @@ import ru.ulstu.conference.repository.ConferenceRepository; import ru.ulstu.deadline.service.DeadlineService; import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.service.PaperService; +import ru.ulstu.user.model.UserConference; +import ru.ulstu.user.service.UserService; import java.io.IOException; import java.util.List; @@ -21,17 +23,24 @@ public class ConferenceService { private final static int MAX_DISPLAY_SIZE = 40; private final ConferenceRepository conferenceRepository; + private final ConferenceUserService conferenceUserService; private final DeadlineService deadlineService; private final PaperService paperService; + private final UserService userService; public ConferenceService(ConferenceRepository conferenceRepository, + ConferenceUserService conferenceUserService, DeadlineService deadlineService, - PaperService paperService) { + PaperService paperService, + UserService userService) { this.conferenceRepository = conferenceRepository; + this.conferenceUserService = conferenceUserService; this.deadlineService = deadlineService; this.paperService = paperService; + this.userService = userService; } + public List findAll() { return conferenceRepository.findAll(); } @@ -88,6 +97,10 @@ public class ConferenceService { conferenceDto.getNotSelectedPapers().add(removedPaper); } + public void takePart(ConferenceDto conferenceDto) throws IOException { + conferenceDto.getUsers().add(new UserConference("test", "test", userService.getCurrentUser())); + } + public List getConferencePapers(List paperIds) { return paperService.findAllNotSelect(paperIds); } @@ -101,6 +114,8 @@ public class ConferenceService { conference.setEndDate(conferenceDto.getEndDate()); conference.setPapers(conferenceDto.getPapers()); conference.setDeadlines(deadlineService.saveOrCreate(conferenceDto.getDeadlines())); + conferenceDto.getUsers().forEach(user -> userService.findById(user.getUser().getId())); + conference.setUsers(conferenceUserService.saveOrCreate(conferenceDto.getUsers())); if (conferenceDto.getPaperIds() != null && !conferenceDto.getPaperIds().isEmpty()) { conferenceDto.getPaperIds().forEach(paperId -> conference.getPapers().add(paperService.findEntityById(paperId))); diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceUserService.java b/src/main/java/ru/ulstu/conference/service/ConferenceUserService.java new file mode 100644 index 0000000..e05491e --- /dev/null +++ b/src/main/java/ru/ulstu/conference/service/ConferenceUserService.java @@ -0,0 +1,48 @@ +package ru.ulstu.conference.service; + +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import ru.ulstu.conference.repository.ConferenceUserRepository; +import ru.ulstu.user.model.UserConference; + +import java.util.List; +import java.util.stream.Collectors; + +@Service +public class ConferenceUserService { + + private final ConferenceUserRepository conferenceUserRepository; + + public ConferenceUserService(ConferenceUserRepository conferenceUserRepository) { + this.conferenceUserRepository = conferenceUserRepository; + } + + public List saveOrCreate(List users) { + return users + .stream() + .map(user -> { + return user.getId() != null ? update(user) : create(user); + }).collect(Collectors.toList()); + } + + @Transactional + public UserConference update(UserConference user) { + UserConference updateUser = conferenceUserRepository.findOne(user.getId()); + updateUser.setDeposit(user.getDeposit()); + updateUser.setParticipation(user.getParticipation()); + conferenceUserRepository.save(updateUser); + return updateUser; + } + + @Transactional + public UserConference create(UserConference user) { + UserConference newUser = new UserConference(); + newUser.setDeposit(user.getDeposit()); + newUser.setParticipation(user.getParticipation()); + newUser.setUser(user.getUser()); + newUser = conferenceUserRepository.save(newUser); + return newUser; + } + + +} diff --git a/src/main/java/ru/ulstu/user/model/UserConference.java b/src/main/java/ru/ulstu/user/model/UserConference.java index 5ae1b12..9afef4c 100644 --- a/src/main/java/ru/ulstu/user/model/UserConference.java +++ b/src/main/java/ru/ulstu/user/model/UserConference.java @@ -1,5 +1,7 @@ package ru.ulstu.user.model; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; import ru.ulstu.core.model.BaseEntity; import javax.persistence.Column; @@ -25,4 +27,49 @@ public class UserConference extends BaseEntity { @JoinColumn(name = "users_id") private User user; + public UserConference() { + } + + public UserConference(String participation, String deposit, User user) { + this.participation = participation; + this.deposit = deposit; + this.user = user; + } + + @JsonCreator + public UserConference(@JsonProperty("id") Integer id, + @JsonProperty("participation") String participation, + @JsonProperty("deposit") String deposit, + @JsonProperty("user") User user) { + this.setId(id); + this.participation = participation; + this.deposit = deposit; + this.user = user; + } + + + public String getParticipation() { + return participation; + } + + public void setParticipation(String participation) { + this.participation = participation; + } + + public String getDeposit() { + return deposit; + } + + public void setDeposit(String deposit) { + this.deposit = deposit; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + } diff --git a/src/main/resources/templates/conferences/conference.html b/src/main/resources/templates/conferences/conference.html index 6d22273..35a6926 100644 --- a/src/main/resources/templates/conferences/conference.html +++ b/src/main/resources/templates/conferences/conference.html @@ -97,23 +97,28 @@
-
- - +
+ + + + - +
- +