diff --git a/data.mv.db b/data.mv.db index 134e71b..d3799c1 100644 Binary files a/data.mv.db and b/data.mv.db differ diff --git a/frontend/vue-project/src/models/Group.js b/frontend/vue-project/src/models/Group.js index 7467db3..5da7745 100644 --- a/frontend/vue-project/src/models/Group.js +++ b/frontend/vue-project/src/models/Group.js @@ -2,5 +2,6 @@ export default class Group{ constructor(data) { this.id = data?.id; this.name = data?.name; + this.subjectIds = data?.subjectIds; } } \ No newline at end of file diff --git a/frontend/vue-project/src/pages/groups.vue b/frontend/vue-project/src/pages/groups.vue index d46c1a4..2825b3e 100644 --- a/frontend/vue-project/src/pages/groups.vue +++ b/frontend/vue-project/src/pages/groups.vue @@ -6,30 +6,102 @@ Имя - + Действия + Работа с группой + Работа с предметами {{ grp.name }} - - - - - - - - - - - - +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + + + - - diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Contoller/DTO/GroupDTO.java b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/DTO/GroupDTO.java index 11714bb..4ad902b 100644 --- a/src/main/java/ru/IP_LabWorks/IP/University/Contoller/DTO/GroupDTO.java +++ b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/DTO/GroupDTO.java @@ -2,16 +2,21 @@ package ru.IP_LabWorks.IP.University.Contoller.DTO; import ru.IP_LabWorks.IP.University.Model.Group; +import java.util.List; + public class GroupDTO { private long id; private String name; + private List subjectIds; + public GroupDTO(){ } public GroupDTO(Group group){ this.id = group.getId(); this.name = group.getName(); + this.subjectIds = group.getSubjectIds(); } public Long getId(){ @@ -29,4 +34,8 @@ public class GroupDTO { public void setName(String name){ this.name = name; } + + public List getSubjectIds(){ + return subjectIds; + } } diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Contoller/DTO/StudentDTO.java b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/DTO/StudentDTO.java index 07a2262..be63849 100644 --- a/src/main/java/ru/IP_LabWorks/IP/University/Contoller/DTO/StudentDTO.java +++ b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/DTO/StudentDTO.java @@ -20,8 +20,9 @@ public class StudentDTO { this.id = student.getId(); this.name = student.getName(); this.birthDate = student.getBirthDate(); - this.groupName = student.getGroup().getName(); - this.groupId = student.getGroup().getId(); + if(student.getGroup() != null){ + this.groupName = student.getGroup().getName(); + } } public String getGroupName() { diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Contoller/REST/GroupController.java b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/REST/GroupController.java index 286941e..8f201ec 100644 --- a/src/main/java/ru/IP_LabWorks/IP/University/Contoller/REST/GroupController.java +++ b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/REST/GroupController.java @@ -59,6 +59,28 @@ public class GroupController { return ResponseEntity.ok(subjects); } + @GetMapping("/getStudentsUndefined") + public ResponseEntity> getStudentsFromUndefinedGroup(){ + List students = groupService.getStudentsUndefined(); + return ResponseEntity.ok(students); + } + + @PostMapping("/{id}/addStudents") + public void addStudentToGroup(@PathVariable Long id, @RequestBody @Valid List studentsIds){ + groupService.addStudentToGroup(id, studentsIds); + } + + @DeleteMapping("deleteStudentFromGroup/{id}") + public void deleteStudentFromGroup(@PathVariable Long id){ + groupService.deleteStudentFromGroup(id); + } + + @PostMapping("/{id}/addSubjectToGroup") + public void addSubjectToGroup(@PathVariable Long id, @RequestBody @Valid List subjectIds){ + groupService.addSubjectToGroup(id, subjectIds); + } + + // @PostMapping("/{id}/student") // public StudentDTO setStudentToGroup(@PathVariable Long id, // @RequestParam("studentId") Long studentId) { diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Contoller/REST/StudentController.java b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/REST/StudentController.java index 38e24ae..a168eda 100644 --- a/src/main/java/ru/IP_LabWorks/IP/University/Contoller/REST/StudentController.java +++ b/src/main/java/ru/IP_LabWorks/IP/University/Contoller/REST/StudentController.java @@ -1,20 +1,24 @@ package ru.IP_LabWorks.IP.University.Contoller.REST; import org.springframework.web.bind.annotation.*; +import ru.IP_LabWorks.IP.University.Contoller.DTO.GroupDTO; import ru.IP_LabWorks.IP.University.Contoller.DTO.StudentDTO; +import ru.IP_LabWorks.IP.University.Service.GroupService; import ru.IP_LabWorks.IP.University.Service.StudentService; import jakarta.validation.Valid; -import java.time.LocalDate; + import java.util.List; @RestController @RequestMapping("/student") public class StudentController { private final StudentService studentService; + private final GroupService groupService; - public StudentController(StudentService studentService) { + public StudentController(StudentService studentService, GroupService groupService) { this.studentService = studentService; + this.groupService = groupService; } @GetMapping("/{id}") @@ -31,12 +35,12 @@ public class StudentController { @PostMapping public StudentDTO createStudent(@RequestBody @Valid StudentDTO studentDTO){ - return new StudentDTO(studentService.addStudent(studentDTO.getName(), studentDTO.getBirthDate(), studentDTO.getGroupId())); + return new StudentDTO(studentService.addStudent(studentDTO.getName(), studentDTO.getBirthDate())); } @PutMapping("/{id}") public StudentDTO updateStudent(@PathVariable Long id, @RequestBody @Valid StudentDTO studentDTO) { - return new StudentDTO(studentService.updateStudent(id, studentDTO.getName(), studentDTO.getBirthDate(), studentDTO.getGroupId())); + return new StudentDTO(studentService.updateStudent(id, studentDTO.getName(), studentDTO.getBirthDate())); } @DeleteMapping("/{id}") @@ -44,4 +48,11 @@ public class StudentController { return new StudentDTO(studentService.deleteStudent(id)); } + @GetMapping("/groups") + public List getGroups() { + return groupService.findAllGroups().stream() + .map(GroupDTO::new) + .toList(); + } + } 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 44d40de..b840921 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 @@ -8,6 +8,7 @@ import org.hibernate.annotations.OnDeleteAction; import javax.swing.*; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Objects; @@ -89,4 +90,18 @@ public class Group { subjects.add(subject); subject.getGroups().add(this); } + + public List getSubjectIds() { + if (subjects.isEmpty()) { + return Collections.emptyList(); + } else { + List subjectIds = new ArrayList<>(); + for (Subject subject : subjects) { + if (!subjectIds.contains(subject.getId())) { + subjectIds.add(subject.getId()); + } + } + return subjectIds; + } + } } diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Model/Student.java b/src/main/java/ru/IP_LabWorks/IP/University/Model/Student.java index 2970765..be46044 100644 --- a/src/main/java/ru/IP_LabWorks/IP/University/Model/Student.java +++ b/src/main/java/ru/IP_LabWorks/IP/University/Model/Student.java @@ -25,10 +25,9 @@ public class Student { public Student() { } - public Student(String name, LocalDate birthDate, Group group) { + public Student(String name, LocalDate birthDate) { this.name = name; this.birthDate = birthDate; - this.group = group; } public Long getId() { return id; } @@ -59,11 +58,13 @@ public class Student { @Override public String toString() { + String groupStr = (group != null) ? group.getName() : "No Group"; return "Student{" + "id=" + id + ", name='" + name + '\'' + ", birthDate='" + birthDate + '\'' + ", age='" + getAge() + '\'' + + ", group='" + groupStr + '\'' + '}'; } diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Service/GroupService.java b/src/main/java/ru/IP_LabWorks/IP/University/Service/GroupService.java index 689307e..e7efe57 100644 --- a/src/main/java/ru/IP_LabWorks/IP/University/Service/GroupService.java +++ b/src/main/java/ru/IP_LabWorks/IP/University/Service/GroupService.java @@ -1,6 +1,6 @@ package ru.IP_LabWorks.IP.University.Service; -import jakarta.persistence.EntityNotFoundException; +import org.springframework.context.annotation.Lazy; import org.springframework.transaction.annotation.Transactional; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; @@ -8,10 +8,9 @@ import ru.IP_LabWorks.IP.University.Model.Group; import ru.IP_LabWorks.IP.University.Model.Student; import ru.IP_LabWorks.IP.University.Model.Subject; import ru.IP_LabWorks.IP.University.Repository.GroupRepository; -import ru.IP_LabWorks.IP.University.Repository.StudentRepository; import ru.IP_LabWorks.IP.University.Service.NotFoundException.GroupNotFoundException; -import java.awt.desktop.OpenFilesEvent; +import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -19,8 +18,13 @@ import java.util.Optional; public class GroupService { private final GroupRepository groupRepository; - public GroupService(GroupRepository groupRepository) { + private final StudentService studentService; + private final SubjectService subjectService; + + public GroupService(GroupRepository groupRepository, StudentService studentService, @Lazy SubjectService subjectService) { this.groupRepository = groupRepository; + this.studentService = studentService; + this.subjectService = subjectService; } @Transactional @@ -87,6 +91,41 @@ public class GroupService { } } + @Transactional + public List getStudentsUndefined(){ + List students = new ArrayList<>(); + for(Student student : studentService.findAllStudents()){ + if(student.getGroup() == null){ + students.add(student); + } + } + return students; + } + + @Transactional + public void addStudentToGroup(Long groupId, List studentIds){ + final Group currentGroup = findGroup(groupId); + for(Long studentId : studentIds) { + studentService.findStudent(studentId).setGroup(currentGroup); + } + groupRepository.save(currentGroup); + } + + @Transactional + public void deleteStudentFromGroup(Long id){ + studentService.findStudent(id).setGroup(null); + } + + @Transactional + public void addSubjectToGroup(Long groupId, List subjectIds){ + final Group currentGroup = findGroup(groupId); + currentGroup.setSubjects(new ArrayList<>()); + for(Long subjectId : subjectIds) { + currentGroup.getSubjects().add(subjectService.findSubject(subjectId)); + } + groupRepository.save(currentGroup); + } + /* @Transactional public Group addStudentToGroup(Long groupId, Long studentId) { Group group = findGroup(groupId); diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Service/StudentService.java b/src/main/java/ru/IP_LabWorks/IP/University/Service/StudentService.java index 9796c4f..4368434 100644 --- a/src/main/java/ru/IP_LabWorks/IP/University/Service/StudentService.java +++ b/src/main/java/ru/IP_LabWorks/IP/University/Service/StudentService.java @@ -1,5 +1,7 @@ package ru.IP_LabWorks.IP.University.Service; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Lazy; import org.springframework.transaction.annotation.Transactional; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; @@ -17,23 +19,17 @@ public class StudentService { private final StudentRepository studentRepository; private final GroupService groupService; - public StudentService(StudentRepository studentRepository, GroupService groupService) { + public StudentService(StudentRepository studentRepository,@Lazy GroupService groupService) { this.studentRepository = studentRepository; this.groupService = groupService; } @Transactional - public Student addStudent(String name, LocalDate birthDate, Long groupId){ + public Student addStudent(String name, LocalDate birthDate){ if (!StringUtils.hasText(name) || !StringUtils.hasText(String.valueOf(birthDate))) { throw new IllegalArgumentException("Student name or date is null or empty"); } - Group group = null; - try{ - group = groupService.findGroup(groupId); - }catch (Exception ex){ - group = null; - } - final Student student = new Student(name, birthDate,group); + final Student student = new Student(name, birthDate); return studentRepository.save(student); } @@ -49,14 +45,13 @@ public class StudentService { } @Transactional - public Student updateStudent(Long id, String name, LocalDate date, Long groupId) { + public Student updateStudent(Long id, String name, LocalDate date) { if (!StringUtils.hasText(name) || !StringUtils.hasText(String.valueOf(date))) { throw new IllegalArgumentException("Student name or date is null or empty"); } final Student currentStudent = findStudent(id); currentStudent.setName(name); currentStudent.setBirthDate(date); - currentStudent.setGroup(groupService.findGroup(groupId)); return studentRepository.save(currentStudent); }