diff --git a/data.mv.db b/data.mv.db index faae2b6..959c296 100644 Binary files a/data.mv.db and b/data.mv.db differ 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 96fff87..fcfa5d2 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 @@ -2,6 +2,7 @@ package ru.IP_LabWorks.IP.University.Model; import jakarta.persistence.*; +import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -12,12 +13,19 @@ public class Group { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; - @OneToMany - @JoinTable(name = "students_in_groups", - joinColumns = @JoinColumn(name = "group_id"), - inverseJoinColumns = @JoinColumn(name = "student_id")) + @OneToMany(fetch = FetchType.EAGER) private List students; + @ManyToMany + private List subjects; + + public Group() { + } + + public Group(String name) { + this.name = name; + } + public Long getId() { return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @@ -42,4 +50,22 @@ public class Group { ", name='" + name + '\'' + '}'; } + + public List getStudents(){ + return students; + } + + public void addStudent(Student student){ + if (students == null) + students = new ArrayList<>(); + students.add(student); + } + + public List getSubjects() { return subjects; } + + public void addSubject(Subject subject){ + if (subjects == null) + subjects = new ArrayList<>(); + subjects.add(subject); + } } 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 8457a01..2623ca9 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 @@ -1,6 +1,7 @@ package ru.IP_LabWorks.IP.University.Model; import jakarta.persistence.*; +import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -12,11 +13,6 @@ public class Student { private Long id; private String name; private Integer age; - @ManyToMany(cascade = CascadeType.ALL) - @JoinTable(name = "student_subject", - joinColumns = @JoinColumn(name = "student_id"), - inverseJoinColumns = @JoinColumn(name = "subject_id")) - private List subjects; public Student() { } 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 5420995..3c62ff4 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 @@ -12,8 +12,8 @@ public class Subject { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; - @ManyToMany(mappedBy = "subjects") - private List students; + @ManyToMany + private List groups; public Subject() { } @@ -46,4 +46,9 @@ public class Subject { ", name='" + name + '\'' + '}'; } + + public List getGroups() { + return groups; + } + } 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 fc8a8eb..e2c650f 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,4 +1,84 @@ package ru.IP_LabWorks.IP.University.Service; +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityNotFoundException; +import jakarta.persistence.PersistenceContext; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; +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 java.util.List; + +@Service public class GroupService { + @PersistenceContext + private EntityManager em; + + @Transactional + public Group addGroup(String name){ + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Group name is null or empty"); + } + final Group group = new Group(name); + em.persist(group); + return group; + } + + @Transactional(readOnly = true) + public Group findGroup(Long id){ + final Group group = em.find(Group.class, id); + if (group == null) { + throw new EntityNotFoundException(String.format("Group with id [%s] is not found", id)); + } + return group; + } + + @Transactional(readOnly = true) + public List findAllGroups() { + return em.createQuery("select s from Group s", Group.class).getResultList(); + } + + @Transactional + public Group updateGroup(Long id, String name) { + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Group name is null or empty"); + } + final Group currentGroup = findGroup(id); + currentGroup.setName(name); + return em.merge(currentGroup); + } + + @Transactional + public Group deleteGroup(Long id) { + final Group currentGroup = findGroup(id); + em.remove(currentGroup); + return currentGroup; + } + + @Transactional + public void deleteAllGroups() { + em.createQuery("delete from Group").executeUpdate(); + } + + @Transactional + public Group deleteStudentInGroup(Long id, Student student) { + final Group currentGroup = findGroup(id); + final Student currentStudent = em.find(Student.class, student.getId()); + currentGroup.getStudents().remove(currentStudent); + em.merge(currentStudent); + return em.merge(currentGroup); + } + + public List findStudentsInGroup(Long id){ + final Group findGroup = findGroup(id); + return findGroup.getStudents(); + } + + public List findSubjectsInGroup(Long id){ + final Group findGroup = findGroup(id); + return findGroup.getSubjects(); + } } 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 6fbe9fd..b9e4fa5 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,6 +1,5 @@ package ru.IP_LabWorks.IP.University.Service; -import jakarta.persistence.Entity; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.PersistenceContext; @@ -8,6 +7,7 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import ru.IP_LabWorks.IP.University.Model.Student; +import ru.IP_LabWorks.IP.University.Model.Subject; import java.util.List; 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 new file mode 100644 index 0000000..181351a --- /dev/null +++ b/src/main/java/ru/IP_LabWorks/IP/University/Service/SubjectService.java @@ -0,0 +1,64 @@ +package ru.IP_LabWorks.IP.University.Service; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityNotFoundException; +import jakarta.persistence.PersistenceContext; +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 java.util.List; + +@Service +public class SubjectService { + @PersistenceContext + private EntityManager em; + + @Transactional + public Subject addSubject(String name){ + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Subject name is null or empty"); + } + final Subject subject = new Subject(name); + em.persist(subject); + return subject; + } + + @Transactional(readOnly = true) + public Subject findSubject(Long id){ + final Subject subject = em.find(Subject.class, id); + if (subject == null) { + throw new EntityNotFoundException(String.format("Subject with id [%s] is not found", id)); + } + return subject; + } + + @Transactional(readOnly = true) + public List findAllSubjects() { + return em.createQuery("select s from Subject s", Subject.class).getResultList(); + } + + @Transactional + public Subject updateSubject(Long id, String name) { + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Subject name is null or empty"); + } + final Subject currentSubject = findSubject(id); + currentSubject.setName(name); + return em.merge(currentSubject); + } + + @Transactional + public Subject deleteSubject(Long id) { + final Subject currentSubject = findSubject(id); + em.remove(currentSubject); + return currentSubject; + } + + @Transactional + public void deleteAllSubjects() { + em.createQuery("delete from Subject").executeUpdate(); + } +} diff --git a/src/test/java/ru/IP_LabWorks/IP/GroupServiceTests.java b/src/test/java/ru/IP_LabWorks/IP/GroupServiceTests.java new file mode 100644 index 0000000..18b9583 --- /dev/null +++ b/src/test/java/ru/IP_LabWorks/IP/GroupServiceTests.java @@ -0,0 +1,80 @@ +package ru.IP_LabWorks.IP; + +import jakarta.persistence.EntityNotFoundException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import ru.IP_LabWorks.IP.University.Model.Group; +import ru.IP_LabWorks.IP.University.Model.Student; +import ru.IP_LabWorks.IP.University.Service.GroupService; +import ru.IP_LabWorks.IP.University.Service.StudentService; + +import java.util.List; + +@SpringBootTest +public class GroupServiceTests { + @Autowired + private GroupService groupService; + + @Autowired + private StudentService studentService; + + @Test + void TestAddGroup(){ + groupService.deleteAllGroups(); + final Group group = groupService.addGroup("ПИбд-21"); + Assertions.assertNotNull(group.getId()); + } + + @Test + void TestFindGroup(){ + groupService.deleteAllGroups(); + final Group group = groupService.addGroup("ПИбд-21"); + + final Group findGroup = groupService.findGroup(group.getId()); + Assertions.assertEquals(group, findGroup); + } + + @Test + void TestFindAllGroup(){ + groupService.deleteAllGroups(); + final Group group1 = groupService.addGroup("ПИбд-21"); + final Group group2 = groupService.addGroup("ПИбд-22"); + + final List groups = groupService.findAllGroups(); + Assertions.assertEquals(groups.size(), 2); + } + + @Test + void TestUpdateGroup(){ + groupService.deleteAllGroups(); + Group group = groupService.addGroup("ПИбд-21"); + + group = groupService.updateGroup(group.getId(), "ПИбд-22"); + Assertions.assertEquals(group.getName(), "ПИбд-22"); + } + + @Test + void TestDeleteGroup(){ + groupService.deleteAllGroups(); + Group group = groupService.addGroup("ПИбд-21"); + groupService.deleteGroup(group.getId()); + Assertions.assertThrows(EntityNotFoundException.class, () -> { + groupService.findGroup(group.getId()); + }); + } + + @Test + void TestFindStudentsInGroup(){ + groupService.deleteAllGroups(); + Student student1 = studentService.addStudent("Марков Данил Павлович", 19); + Student student2 = studentService.addStudent("Емельянов Артём Сергеевич", 19); + Group group = groupService.addGroup("ПИбд-21"); + group.addStudent(student1); + group.addStudent(student2); + final List students = groupService.findStudentsInGroup(group.getId()); + System.out.println("DFGSDFGSDFGSDFG" + students.size()); + Assertions.assertEquals(students.size(), 2); + } +} diff --git a/src/test/java/ru/IP_LabWorks/IP/StudentServiceTests.java b/src/test/java/ru/IP_LabWorks/IP/StudentServiceTests.java new file mode 100644 index 0000000..2ce8af1 --- /dev/null +++ b/src/test/java/ru/IP_LabWorks/IP/StudentServiceTests.java @@ -0,0 +1,65 @@ +package ru.IP_LabWorks.IP; + +import jakarta.persistence.EntityNotFoundException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import ru.IP_LabWorks.IP.University.Model.Student; +import ru.IP_LabWorks.IP.University.Model.Subject; +import ru.IP_LabWorks.IP.University.Service.StudentService; +import ru.IP_LabWorks.IP.University.Service.SubjectService; + +import java.util.List; + +@SpringBootTest +public class StudentServiceTests { + @Autowired + private StudentService studentService; + + @Test + void TestAddStudent(){ + studentService.deleteAllStudent(); + final Student student = studentService.addStudent("Марков Данил Павлович", 19); + Assertions.assertNotNull(student.getId()); + } + + @Test + void TestFindStudent(){ + studentService.deleteAllStudent(); + final Student student = studentService.addStudent("Марков Данил Павлович", 19); + + final Student findStudent = studentService.findStudent(student.getId()); + Assertions.assertEquals(student, findStudent); + } + + @Test + void TestFindAllStudent(){ + studentService.deleteAllStudent(); + final Student student1 = studentService.addStudent("Марков Данил Павлович", 19); + + final Student student2 = studentService.addStudent("Емельянов Артём Сергеевич", 19); + + final List students = studentService.findAllStudents(); + Assertions.assertEquals(students.size(), 2); + } + + @Test + void TestUpdateStudent(){ + studentService.deleteAllStudent(); + Student student = studentService.addStudent("Марков Данил Павлович", 19); + + student = studentService.updateStudent(student.getId(), "Емельянов Артём Сергеевич", 19); + Assertions.assertEquals(student.getName(), "Емельянов Артём Сергеевич"); + } + + @Test + void TestDeleteStudent(){ + studentService.deleteAllStudent(); + Student student = studentService.addStudent("Марков Данил Павлович", 19); + studentService.deleteStudent(student.getId()); + Assertions.assertThrows(EntityNotFoundException.class, () -> { + studentService.findStudent(student.getId()); + }); + } +} diff --git a/src/test/java/ru/IP_LabWorks/IP/SubjectServiceTests.java b/src/test/java/ru/IP_LabWorks/IP/SubjectServiceTests.java new file mode 100644 index 0000000..40f100e --- /dev/null +++ b/src/test/java/ru/IP_LabWorks/IP/SubjectServiceTests.java @@ -0,0 +1,62 @@ +package ru.IP_LabWorks.IP; + +import jakarta.persistence.EntityNotFoundException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import ru.IP_LabWorks.IP.University.Model.Subject; +import ru.IP_LabWorks.IP.University.Service.SubjectService; + +import java.util.List; + +@SpringBootTest +public class SubjectServiceTests { + @Autowired + private SubjectService subjectService; + + @Test + void TestAddSubject(){ + subjectService.deleteAllSubjects(); + final Subject subject = subjectService.addSubject("Философия"); + Assertions.assertNotNull(subject.getId()); + } + + @Test + void TestFindSubject(){ + subjectService.deleteAllSubjects(); + final Subject subject = subjectService.addSubject("Философия"); + + final Subject findSubject = subjectService.findSubject(subject.getId()); + Assertions.assertEquals(subject, findSubject); + } + + @Test + void TestFindAllSubject(){ + subjectService.deleteAllSubjects(); + final Subject subject1 = subjectService.addSubject("Философия"); + final Subject subject2 = subjectService.addSubject("Математика"); + + final List subjects = subjectService.findAllSubjects(); + Assertions.assertEquals(subjects.size(), 2); + } + + @Test + void TestUpdateSubject(){ + subjectService.deleteAllSubjects(); + Subject subject = subjectService.addSubject("Философия"); + + subject = subjectService.updateSubject(subject.getId(), "Математика"); + Assertions.assertEquals(subject.getName(), "Математика"); + } + + @Test + void TestDeleteSubject(){ + subjectService.deleteAllSubjects(); + Subject subject = subjectService.addSubject("Математика"); + subjectService.deleteSubject(subject.getId()); + Assertions.assertThrows(EntityNotFoundException.class, () -> { + subjectService.findSubject(subject.getId()); + }); + } +}