Сервисы переписаны на репозитории, не до конца, осталось дописать методы взаимодействия между сущностями
This commit is contained in:
parent
67b9d4fabf
commit
08fe574653
@ -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> group = groupRepository.findById(id);
|
||||
return group.orElseThrow(() -> new GroupNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Group> 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<Student> 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<Student> findStudentsInGroup(Long groupId) {
|
||||
// Group group = findGroup(groupId);
|
||||
// if (group == null) {
|
||||
// throw new EntityNotFoundException("Group not found");
|
||||
// }
|
||||
// return group.getStudents();
|
||||
// }
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
@ -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> student = studentRepository.findById(id);
|
||||
return student.orElseThrow(() -> new StudentNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Student> 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);
|
||||
// }
|
||||
}
|
||||
|
@ -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> subject = subjectRepository.findById(id);
|
||||
return subject.orElseThrow(() -> new SubjectNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Subject> 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<Group> 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<Group> findGroupsBySubject(Long subjectId) {
|
||||
// Subject subject = findSubject(subjectId);
|
||||
// return subject.getGroups();
|
||||
// }
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user