Сервисы переписаны на репозитории, не до конца, осталось дописать методы взаимодействия между сущностями

This commit is contained in:
ArtemEmelyanov 2023-04-21 14:53:50 +04:00
parent 67b9d4fabf
commit 08fe574653
6 changed files with 127 additions and 107 deletions

View File

@ -1,20 +1,25 @@
package ru.IP_LabWorks.IP.University.Service; package ru.IP_LabWorks.IP.University.Service;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import ru.IP_LabWorks.IP.University.Model.Group; import ru.IP_LabWorks.IP.University.Model.Group;
import ru.IP_LabWorks.IP.University.Model.Student; 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.List;
import java.util.Optional;
@Service @Service
public class GroupService { public class GroupService {
@PersistenceContext private final GroupRepository groupRepository;
private EntityManager em;
public GroupService(GroupRepository groupRepository) {
this.groupRepository = groupRepository;
}
@Transactional @Transactional
public Group addGroup(String name){ public Group addGroup(String name){
@ -22,22 +27,18 @@ public class GroupService {
throw new IllegalArgumentException("Group name is null or empty"); throw new IllegalArgumentException("Group name is null or empty");
} }
final Group group = new Group(name); final Group group = new Group(name);
em.persist(group); return groupRepository.save(group);
return group;
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public Group findGroup(Long id){ public Group findGroup(Long id){
final Group group = em.find(Group.class, id); final Optional<Group> group = groupRepository.findById(id);
if (group == null) { return group.orElseThrow(() -> new GroupNotFoundException(id));
throw new EntityNotFoundException(String.format("Group with id [%s] is not found", id));
}
return group;
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<Group> findAllGroups() { public List<Group> findAllGroups() {
return em.createQuery("select s from Group s", Group.class).getResultList(); return groupRepository.findAll();
} }
@Transactional @Transactional
@ -47,45 +48,45 @@ public class GroupService {
} }
final Group currentGroup = findGroup(id); final Group currentGroup = findGroup(id);
currentGroup.setName(name); currentGroup.setName(name);
return em.merge(currentGroup); return groupRepository.save(currentGroup);
} }
@Transactional @Transactional
public Group deleteGroup(Long id) { public Group deleteGroup(Long id) {
final Group currentGroup = findGroup(id); final Group currentGroup = findGroup(id);
em.remove(currentGroup); groupRepository.delete(currentGroup);
return currentGroup; return currentGroup;
} }
@Transactional @Transactional
public void deleteAllGroups() { public void deleteAllGroups() {
em.createQuery("delete from Group").executeUpdate(); groupRepository.deleteAll();
} }
@Transactional // @Transactional
public void addStudentToGroup(Long groupId, Long studentId) { // public void addStudentToGroup(Long groupId, Long studentId) {
Group group = findGroup(groupId); // Group group = findGroup(groupId);
Student student = em.find(Student.class, studentId); // Student student = em.find(Student.class, studentId);
if (group == null || student == null) { // if (group == null || student == null) {
throw new EntityNotFoundException("Group or Student not found"); // throw new EntityNotFoundException("Group or Student not found");
} // }
student.setGroup(group); // student.setGroup(group);
em.merge(student); // em.merge(student);
} // }
//
@Transactional // @Transactional
public void removeStudentFromGroup(Long groupId, Long studentId) { // public void removeStudentFromGroup(Long groupId, Long studentId) {
Group group = findGroup(groupId); // Group group = findGroup(groupId);
Student student = em.find(Student.class, studentId); // Student student = em.find(Student.class, studentId);
group.getStudents().remove(student); // group.getStudents().remove(student);
} // }
//
@Transactional(readOnly = true) // @Transactional(readOnly = true)
public List<Student> findStudentsInGroup(Long groupId) { // public List<Student> findStudentsInGroup(Long groupId) {
Group group = findGroup(groupId); // Group group = findGroup(groupId);
if (group == null) { // if (group == null) {
throw new EntityNotFoundException("Group not found"); // throw new EntityNotFoundException("Group not found");
} // }
return group.getStudents(); // return group.getStudents();
} // }
} }

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -1,21 +1,25 @@
package ru.IP_LabWorks.IP.University.Service; package ru.IP_LabWorks.IP.University.Service;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import ru.IP_LabWorks.IP.University.Model.Group; import ru.IP_LabWorks.IP.University.Model.Group;
import ru.IP_LabWorks.IP.University.Model.Student; 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.time.LocalDate;
import java.util.List; import java.util.List;
import java.util.Optional;
@Service @Service
public class StudentService { public class StudentService {
@PersistenceContext private final StudentRepository studentRepository;
private EntityManager em;
public StudentService(StudentRepository studentRepository) {
this.studentRepository = studentRepository;
}
@Transactional @Transactional
public Student addStudent(String name, LocalDate date){ 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"); throw new IllegalArgumentException("Student name or date is null or empty");
} }
final Student student = new Student(name, date); final Student student = new Student(name, date);
em.persist(student); return studentRepository.save(student);
return student;
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public Student findStudent(Long id) { public Student findStudent(Long id) {
final Student student = em.find(Student.class, id); final Optional<Student> student = studentRepository.findById(id);
if (student == null) { return student.orElseThrow(() -> new StudentNotFoundException(id));
throw new EntityNotFoundException(String.format("Student with id [%s] is not found", id));
}
return student;
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<Student> findAllStudents() { public List<Student> findAllStudents() {
return em.createQuery("select s from Student s", Student.class).getResultList(); return studentRepository.findAll();
} }
@Transactional @Transactional
@ -49,29 +49,28 @@ public class StudentService {
final Student currentStudent = findStudent(id); final Student currentStudent = findStudent(id);
currentStudent.setName(name); currentStudent.setName(name);
currentStudent.setBirthDate(date); currentStudent.setBirthDate(date);
return em.merge(currentStudent); return studentRepository.save(currentStudent);
} }
@Transactional @Transactional
public Student deleteStudent(Long id) { public Student deleteStudent(Long id) {
final Student currentStudent = findStudent(id); final Student currentStudent = findStudent(id);
em.remove(currentStudent); studentRepository.delete(currentStudent);
return currentStudent; return currentStudent;
} }
@Transactional @Transactional
public void deleteAllStudent() { public void deleteAllStudent() {
em.createQuery("delete from Student").executeUpdate(); 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);
} }
// @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);
// }
} }

View File

@ -1,22 +1,25 @@
package ru.IP_LabWorks.IP.University.Service; package ru.IP_LabWorks.IP.University.Service;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import ru.IP_LabWorks.IP.University.Model.Group; 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.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.List;
import java.util.Optional;
@Service @Service
public class SubjectService { public class SubjectService {
@PersistenceContext private final SubjectRepository subjectRepository;
private EntityManager em;
public SubjectService(SubjectRepository subjectRepository) {
this.subjectRepository = subjectRepository;
}
@Transactional @Transactional
public Subject addSubject(String name){ public Subject addSubject(String name){
@ -24,22 +27,18 @@ public class SubjectService {
throw new IllegalArgumentException("Subject name is null or empty"); throw new IllegalArgumentException("Subject name is null or empty");
} }
final Subject subject = new Subject(name); final Subject subject = new Subject(name);
em.persist(subject); return subjectRepository.save(subject);
return subject;
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public Subject findSubject(Long id){ public Subject findSubject(Long id){
final Subject subject = em.find(Subject.class, id); final Optional<Subject> subject = subjectRepository.findById(id);
if (subject == null) { return subject.orElseThrow(() -> new SubjectNotFoundException(id));
throw new EntityNotFoundException(String.format("Subject with id [%s] is not found", id));
}
return subject;
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<Subject> findAllSubjects() { public List<Subject> findAllSubjects() {
return em.createQuery("select s from Subject s", Subject.class).getResultList(); return subjectRepository.findAll();
} }
@Transactional @Transactional
@ -49,42 +48,42 @@ public class SubjectService {
} }
final Subject currentSubject = findSubject(id); final Subject currentSubject = findSubject(id);
currentSubject.setName(name); currentSubject.setName(name);
return em.merge(currentSubject); return subjectRepository.save(currentSubject);
} }
@Transactional @Transactional
public Subject deleteSubject(Long id) { public Subject deleteSubject(Long id) {
final Subject currentSubject = findSubject(id); final Subject currentSubject = findSubject(id);
em.remove(currentSubject); subjectRepository.delete(currentSubject);
return currentSubject; return currentSubject;
} }
@Transactional @Transactional
public void deleteAllSubjects() { public void deleteAllSubjects() {
em.createQuery("delete from Subject").executeUpdate(); subjectRepository.deleteAll();
} }
@Transactional // @Transactional
public void addGroupToSubject(Long subjectId, Long groupId) { // public void addGroupToSubject(Long subjectId, Long groupId) {
Subject subject = findSubject(subjectId); // Subject subject = findSubject(subjectId);
Group group = em.find(Group.class, groupId); // Group group = em.find(Group.class, groupId);
subject.getGroups().add(group); // subject.getGroups().add(group);
em.merge(subject); // em.merge(subject);
em.merge(group); // em.merge(group);
} // }
//
@Transactional // @Transactional
public void removeGroupFromSubject(Long subjectId, Long groupId) { // public void removeGroupFromSubject(Long subjectId, Long groupId) {
Subject subject = findSubject(subjectId); // Subject subject = findSubject(subjectId);
Group group = em.find(Group.class, groupId); // Group group = em.find(Group.class, groupId);
subject.getGroups().remove(group); // subject.getGroups().remove(group);
em.merge(subject); // em.merge(subject);
em.merge(group); // em.merge(group);
} // }
//
@Transactional(readOnly = true) // @Transactional(readOnly = true)
public List<Group> findGroupsBySubject(Long subjectId) { // public List<Group> findGroupsBySubject(Long subjectId) {
Subject subject = findSubject(subjectId); // Subject subject = findSubject(subjectId);
return subject.getGroups(); // return subject.getGroups();
} // }
} }