From 08fe5746538fb773b42357eaf95f997ad8cb9492 Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Fri, 21 Apr 2023 14:53:50 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B2=D0=B8=D1=81=D1=8B=20?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=BF=D0=B8=D1=81=D0=B0=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=80=D0=B5=D0=BF=D0=BE=D0=B7=D0=B8=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=B8,=20=D0=BD=D0=B5=20=D0=B4=D0=BE=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BD=D1=86=D0=B0,=20=D0=BE=D1=81=D1=82=D0=B0=D0=BB?= =?UTF-8?q?=D0=BE=D1=81=D1=8C=20=D0=B4=D0=BE=D0=BF=D0=B8=D1=81=D0=B0=D1=82?= =?UTF-8?q?=D1=8C=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20=D0=B2=D0=B7?= =?UTF-8?q?=D0=B0=D0=B8=D0=BC=D0=BE=D0=B4=D0=B5=D0=B9=D1=81=D1=82=D0=B2?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BC=D0=B5=D0=B6=D0=B4=D1=83=20=D1=81=D1=83?= =?UTF-8?q?=D1=89=D0=BD=D0=BE=D1=81=D1=82=D1=8F=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IP/University/Service/GroupService.java | 83 ++++++++++--------- .../GroupNotFoundException.java | 7 ++ .../StudentNotFoundException.java | 7 ++ .../SubjectNotFoundException.java | 7 ++ .../IP/University/Service/StudentService.java | 51 ++++++------ .../IP/University/Service/SubjectService.java | 79 +++++++++--------- 6 files changed, 127 insertions(+), 107 deletions(-) create mode 100644 src/main/java/ru/IP_LabWorks/IP/University/Service/NotFoundException/GroupNotFoundException.java create mode 100644 src/main/java/ru/IP_LabWorks/IP/University/Service/NotFoundException/StudentNotFoundException.java create mode 100644 src/main/java/ru/IP_LabWorks/IP/University/Service/NotFoundException/SubjectNotFoundException.java 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 0ef1fcc..606686c 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,20 +1,25 @@ 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.Repository.GroupRepository; +import ru.IP_LabWorks.IP.University.Service.NotFoundException.GroupNotFoundException; +import java.awt.desktop.OpenFilesEvent; import java.util.List; +import java.util.Optional; @Service public class GroupService { - @PersistenceContext - private EntityManager em; + private final GroupRepository groupRepository; + + public GroupService(GroupRepository groupRepository) { + this.groupRepository = groupRepository; + } @Transactional public Group addGroup(String name){ @@ -22,22 +27,18 @@ public class GroupService { throw new IllegalArgumentException("Group name is null or empty"); } final Group group = new Group(name); - em.persist(group); - return group; + return groupRepository.save(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; + final Optional group = groupRepository.findById(id); + return group.orElseThrow(() -> new GroupNotFoundException(id)); } @Transactional(readOnly = true) public List findAllGroups() { - return em.createQuery("select s from Group s", Group.class).getResultList(); + return groupRepository.findAll(); } @Transactional @@ -47,45 +48,45 @@ public class GroupService { } final Group currentGroup = findGroup(id); currentGroup.setName(name); - return em.merge(currentGroup); + return groupRepository.save(currentGroup); } @Transactional public Group deleteGroup(Long id) { final Group currentGroup = findGroup(id); - em.remove(currentGroup); + groupRepository.delete(currentGroup); return currentGroup; } @Transactional public void deleteAllGroups() { - em.createQuery("delete from Group").executeUpdate(); + groupRepository.deleteAll(); } - @Transactional - public void addStudentToGroup(Long groupId, Long studentId) { - Group group = findGroup(groupId); - Student student = em.find(Student.class, studentId); - if (group == null || student == null) { - throw new EntityNotFoundException("Group or Student not found"); - } - student.setGroup(group); - em.merge(student); - } - - @Transactional - public void removeStudentFromGroup(Long groupId, Long studentId) { - Group group = findGroup(groupId); - Student student = em.find(Student.class, studentId); - group.getStudents().remove(student); - } - - @Transactional(readOnly = true) - public List findStudentsInGroup(Long groupId) { - Group group = findGroup(groupId); - if (group == null) { - throw new EntityNotFoundException("Group not found"); - } - return group.getStudents(); - } +// @Transactional +// public void addStudentToGroup(Long groupId, Long studentId) { +// Group group = findGroup(groupId); +// Student student = em.find(Student.class, studentId); +// if (group == null || student == null) { +// throw new EntityNotFoundException("Group or Student not found"); +// } +// student.setGroup(group); +// em.merge(student); +// } +// +// @Transactional +// public void removeStudentFromGroup(Long groupId, Long studentId) { +// Group group = findGroup(groupId); +// Student student = em.find(Student.class, studentId); +// group.getStudents().remove(student); +// } +// +// @Transactional(readOnly = true) +// public List findStudentsInGroup(Long groupId) { +// Group group = findGroup(groupId); +// if (group == null) { +// throw new EntityNotFoundException("Group not found"); +// } +// return group.getStudents(); +// } } diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Service/NotFoundException/GroupNotFoundException.java b/src/main/java/ru/IP_LabWorks/IP/University/Service/NotFoundException/GroupNotFoundException.java new file mode 100644 index 0000000..63bbce3 --- /dev/null +++ b/src/main/java/ru/IP_LabWorks/IP/University/Service/NotFoundException/GroupNotFoundException.java @@ -0,0 +1,7 @@ +package ru.IP_LabWorks.IP.University.Service.NotFoundException; + +public class GroupNotFoundException extends RuntimeException { + public GroupNotFoundException(Long id) { + super(String.format("Group with id [%s] is not found", id)); + } +} \ No newline at end of file diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Service/NotFoundException/StudentNotFoundException.java b/src/main/java/ru/IP_LabWorks/IP/University/Service/NotFoundException/StudentNotFoundException.java new file mode 100644 index 0000000..8ee71cd --- /dev/null +++ b/src/main/java/ru/IP_LabWorks/IP/University/Service/NotFoundException/StudentNotFoundException.java @@ -0,0 +1,7 @@ +package ru.IP_LabWorks.IP.University.Service.NotFoundException; + +public class StudentNotFoundException extends RuntimeException { + public StudentNotFoundException(Long id) { + super(String.format("Student with id [%s] is not found", id)); + } +} diff --git a/src/main/java/ru/IP_LabWorks/IP/University/Service/NotFoundException/SubjectNotFoundException.java b/src/main/java/ru/IP_LabWorks/IP/University/Service/NotFoundException/SubjectNotFoundException.java new file mode 100644 index 0000000..4121c61 --- /dev/null +++ b/src/main/java/ru/IP_LabWorks/IP/University/Service/NotFoundException/SubjectNotFoundException.java @@ -0,0 +1,7 @@ +package ru.IP_LabWorks.IP.University.Service.NotFoundException; + +public class SubjectNotFoundException extends RuntimeException { + public SubjectNotFoundException(Long id) { + super(String.format("Subject with id [%s] is not found", id)); + } +} 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 606ea05..35018ab 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,21 +1,25 @@ 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.Repository.StudentRepository; +import ru.IP_LabWorks.IP.University.Service.NotFoundException.StudentNotFoundException; import java.time.LocalDate; import java.util.List; +import java.util.Optional; @Service public class StudentService { - @PersistenceContext - private EntityManager em; + private final StudentRepository studentRepository; + + public StudentService(StudentRepository studentRepository) { + this.studentRepository = studentRepository; + } @Transactional public Student addStudent(String name, LocalDate date){ @@ -23,22 +27,18 @@ public class StudentService { throw new IllegalArgumentException("Student name or date is null or empty"); } final Student student = new Student(name, date); - em.persist(student); - return student; + return studentRepository.save(student); } @Transactional(readOnly = true) public Student findStudent(Long id) { - final Student student = em.find(Student.class, id); - if (student == null) { - throw new EntityNotFoundException(String.format("Student with id [%s] is not found", id)); - } - return student; + final Optional student = studentRepository.findById(id); + return student.orElseThrow(() -> new StudentNotFoundException(id)); } @Transactional(readOnly = true) public List findAllStudents() { - return em.createQuery("select s from Student s", Student.class).getResultList(); + return studentRepository.findAll(); } @Transactional @@ -49,29 +49,28 @@ public class StudentService { final Student currentStudent = findStudent(id); currentStudent.setName(name); currentStudent.setBirthDate(date); - return em.merge(currentStudent); + return studentRepository.save(currentStudent); } @Transactional public Student deleteStudent(Long id) { final Student currentStudent = findStudent(id); - em.remove(currentStudent); + studentRepository.delete(currentStudent); return currentStudent; } @Transactional public void deleteAllStudent() { - em.createQuery("delete from Student").executeUpdate(); - } - - @Transactional - public void AddStudentToGroup(Long idStudent, Long idGroup){ - Student student = findStudent(idStudent); - Group group = em.find(Group.class, idGroup); - if (group == null || student == null) { - throw new EntityNotFoundException("Group or Student not found"); - } - student.setGroup(group); - em.merge(student); + studentRepository.deleteAll(); } +// @Transactional +// public void AddStudentToGroup(Long idStudent, Long idGroup){ +// Student student = findStudent(idStudent); +// Group group = em.find(Group.class, idGroup); +// if (group == null || student == null) { +// throw new EntityNotFoundException("Group or Student not found"); +// } +// student.setGroup(group); +// em.merge(student); +// } } 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 8191624..fe4d443 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,22 +1,25 @@ package ru.IP_LabWorks.IP.University.Service; -import jakarta.persistence.EntityManager; import jakarta.persistence.EntityNotFoundException; -import jakarta.persistence.PersistenceContext; -import org.springframework.beans.factory.annotation.Autowired; 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.Student; 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.List; +import java.util.Optional; @Service public class SubjectService { - @PersistenceContext - private EntityManager em; + private final SubjectRepository subjectRepository; + + public SubjectService(SubjectRepository subjectRepository) { + this.subjectRepository = subjectRepository; + } @Transactional public Subject addSubject(String name){ @@ -24,22 +27,18 @@ public class SubjectService { throw new IllegalArgumentException("Subject name is null or empty"); } final Subject subject = new Subject(name); - em.persist(subject); - return subject; + return subjectRepository.save(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; + final Optional subject = subjectRepository.findById(id); + return subject.orElseThrow(() -> new SubjectNotFoundException(id)); } @Transactional(readOnly = true) public List findAllSubjects() { - return em.createQuery("select s from Subject s", Subject.class).getResultList(); + return subjectRepository.findAll(); } @Transactional @@ -49,42 +48,42 @@ public class SubjectService { } final Subject currentSubject = findSubject(id); currentSubject.setName(name); - return em.merge(currentSubject); + return subjectRepository.save(currentSubject); } @Transactional public Subject deleteSubject(Long id) { final Subject currentSubject = findSubject(id); - em.remove(currentSubject); + subjectRepository.delete(currentSubject); return currentSubject; } @Transactional public void deleteAllSubjects() { - em.createQuery("delete from Subject").executeUpdate(); + subjectRepository.deleteAll(); } - @Transactional - public void addGroupToSubject(Long subjectId, Long groupId) { - Subject subject = findSubject(subjectId); - Group group = em.find(Group.class, groupId); - subject.getGroups().add(group); - em.merge(subject); - em.merge(group); - } - - @Transactional - public void removeGroupFromSubject(Long subjectId, Long groupId) { - Subject subject = findSubject(subjectId); - Group group = em.find(Group.class, groupId); - subject.getGroups().remove(group); - em.merge(subject); - em.merge(group); - } - - @Transactional(readOnly = true) - public List findGroupsBySubject(Long subjectId) { - Subject subject = findSubject(subjectId); - return subject.getGroups(); - } +// @Transactional +// public void addGroupToSubject(Long subjectId, Long groupId) { +// Subject subject = findSubject(subjectId); +// Group group = em.find(Group.class, groupId); +// subject.getGroups().add(group); +// em.merge(subject); +// em.merge(group); +// } +// +// @Transactional +// public void removeGroupFromSubject(Long subjectId, Long groupId) { +// Subject subject = findSubject(subjectId); +// Group group = em.find(Group.class, groupId); +// subject.getGroups().remove(group); +// em.merge(subject); +// em.merge(group); +// } +// +// @Transactional(readOnly = true) +// public List findGroupsBySubject(Long subjectId) { +// Subject subject = findSubject(subjectId); +// return subject.getGroups(); +// } }