diff --git a/data.mv.db b/data.mv.db index 17aec51..6ef2fa8 100644 Binary files a/data.mv.db and b/data.mv.db differ diff --git a/frontend/vue-project/src/models/Subject.js b/frontend/vue-project/src/models/Subject.js index b50996e..1b0cfd7 100644 --- a/frontend/vue-project/src/models/Subject.js +++ b/frontend/vue-project/src/models/Subject.js @@ -2,5 +2,6 @@ export default class Subject{ constructor(data) { this.id = data?.id; this.name = data?.name; + this.groupIds = data?.groupIds; } } \ No newline at end of file diff --git a/frontend/vue-project/src/pages/subjects.vue b/frontend/vue-project/src/pages/subjects.vue index 1cc2c2f..da9005f 100644 --- a/frontend/vue-project/src/pages/subjects.vue +++ b/frontend/vue-project/src/pages/subjects.vue @@ -19,11 +19,15 @@ + + + + + + diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Contoller/DTO/SubjectDTO.java b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/DTO/SubjectDTO.java index 67f772e..a3f851b 100644 --- a/src/main/java/ru/IP_LabWorks/IP/University/Contoller/DTO/SubjectDTO.java +++ b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/DTO/SubjectDTO.java @@ -2,16 +2,20 @@ package ru.IP_LabWorks.IP.University.Contoller.DTO; import ru.IP_LabWorks.IP.University.Model.Subject; +import java.util.List; +import java.util.stream.Collectors; + public class SubjectDTO { private long id; private String name; - + private List groupIds; public SubjectDTO(){ } public SubjectDTO(Subject subject){ this.id = subject.getId(); this.name = subject.getName(); + this.groupIds = subject.getGroupIds(); } public Long getId(){ @@ -21,4 +25,9 @@ public class SubjectDTO { public String getName(){ return name; } + + public List getGroupIds(){ + return groupIds; + } + } diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Contoller/REST/SubjectController.java b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/REST/SubjectController.java index 4b7fb58..17d936c 100644 --- a/src/main/java/ru/IP_LabWorks/IP/University/Contoller/REST/SubjectController.java +++ b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/REST/SubjectController.java @@ -8,6 +8,7 @@ import ru.IP_LabWorks.IP.University.Service.GroupService; import ru.IP_LabWorks.IP.University.Service.SubjectService; import java.util.List; +import java.util.Objects; @RestController @RequestMapping("/subject") @@ -44,4 +45,14 @@ public class SubjectController { public SubjectDTO deleteSubject(@PathVariable Long id){ return new SubjectDTO(subjectService.deleteSubject(id)); } + + @GetMapping("/getAllSubjectGroup") + public List getAllSubjectGroup(){ + return subjectService.getAllSubjectGroup(); + } + + @PostMapping("/{id}/addSubjectToGroup") + public void addSubjectToGroup(@PathVariable Long id, @RequestBody @Valid List groupsIds){ + subjectService.addSubjectToGroup(id, groupsIds); + } } diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Model/Group.java b/src/main/java/ru/IP_LabWorks/IP/University/Model/Group.java index e041ae6..d41beb7 100644 --- a/src/main/java/ru/IP_LabWorks/IP/University/Model/Group.java +++ b/src/main/java/ru/IP_LabWorks/IP/University/Model/Group.java @@ -85,4 +85,9 @@ public class Group { public void setSubjects(List subjects) { this.subjects = subjects; } + + public void addSubject(Subject subject) { + subjects.add(subject); + subject.getGroups().add(this); + } } diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Model/Subject.java b/src/main/java/ru/IP_LabWorks/IP/University/Model/Subject.java index 95702b4..bcf7904 100644 --- a/src/main/java/ru/IP_LabWorks/IP/University/Model/Subject.java +++ b/src/main/java/ru/IP_LabWorks/IP/University/Model/Subject.java @@ -6,6 +6,7 @@ import org.hibernate.annotations.OnDelete; import org.hibernate.annotations.OnDeleteAction; import javax.swing.*; +import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -61,4 +62,14 @@ public class Subject { public void setGroups(List groups) { this.groups = groups; } + + public List getGroupIds(){ + List GroupsIds = new ArrayList<>(); + for(Group group : groups){ + if(!GroupsIds.contains(group.getId())){ + GroupsIds.add(group.getId()); + } + } + return GroupsIds; + } } diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Repository/SubjectRepository.java b/src/main/java/ru/IP_LabWorks/IP/University/Repository/SubjectRepository.java index 98360b9..1d29413 100644 --- a/src/main/java/ru/IP_LabWorks/IP/University/Repository/SubjectRepository.java +++ b/src/main/java/ru/IP_LabWorks/IP/University/Repository/SubjectRepository.java @@ -1,7 +1,12 @@ package ru.IP_LabWorks.IP.University.Repository; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import ru.IP_LabWorks.IP.University.Model.Subject; +import java.util.List; + public interface SubjectRepository extends JpaRepository { + @Query(value = "SELECT * FROM subject_group", nativeQuery = true) + List getAllSubjectGroup(); } diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Service/SubjectService.java b/src/main/java/ru/IP_LabWorks/IP/University/Service/SubjectService.java index fe4d443..27b8202 100644 --- a/src/main/java/ru/IP_LabWorks/IP/University/Service/SubjectService.java +++ b/src/main/java/ru/IP_LabWorks/IP/University/Service/SubjectService.java @@ -1,24 +1,25 @@ package ru.IP_LabWorks.IP.University.Service; -import jakarta.persistence.EntityNotFoundException; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; import ru.IP_LabWorks.IP.University.Model.Group; import ru.IP_LabWorks.IP.University.Model.Subject; import ru.IP_LabWorks.IP.University.Repository.SubjectRepository; -import ru.IP_LabWorks.IP.University.Service.NotFoundException.GroupNotFoundException; import ru.IP_LabWorks.IP.University.Service.NotFoundException.SubjectNotFoundException; +import java.util.ArrayList; import java.util.List; import java.util.Optional; @Service public class SubjectService { private final SubjectRepository subjectRepository; + private final GroupService groupService; - public SubjectService(SubjectRepository subjectRepository) { + public SubjectService(SubjectRepository subjectRepository, GroupService groupService) { this.subjectRepository = subjectRepository; + this.groupService = groupService; } @Transactional @@ -63,6 +64,30 @@ public class SubjectService { subjectRepository.deleteAll(); } + @Transactional + public List getAllSubjectGroup(){ + return subjectRepository.getAllSubjectGroup(); + } + + @Transactional + public void addSubjectToGroup(Long subjectId, List groupIds){ + final Subject currentSubject = findSubject(subjectId); + currentSubject.setGroups(new ArrayList<>()); + for(Long groupId : groupIds) { + currentSubject.getGroups().add(groupService.findGroup(groupId)); + } + subjectRepository.save(currentSubject); + } + +// @Transactional +// public void addSubjectToGroup(List groupsId, Subject subject) { +// List groups = groupRepository.findAllById(groupsId); +// for (Group group : groups) { +// group.addSubject(subject); +// } +// groupRepository.saveAll(groups); +// } + // @Transactional // public void addGroupToSubject(Long subjectId, Long groupId) { // Subject subject = findSubject(subjectId);