diff --git a/data.mv.db b/data.mv.db index da09d65..faae2b6 100644 Binary files a/data.mv.db and b/data.mv.db differ 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 new file mode 100644 index 0000000..fc8a8eb --- /dev/null +++ b/src/main/java/ru/IP_LabWorks/IP/University/Service/GroupService.java @@ -0,0 +1,4 @@ +package ru.IP_LabWorks.IP.University.Service; + +public class GroupService { +} 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 65f6b5a..6fbe9fd 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,4 +1,65 @@ package ru.IP_LabWorks.IP.University.Service; +import jakarta.persistence.Entity; +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.Student; + +import java.util.List; + +@Service public class StudentService { + @PersistenceContext + private EntityManager em; + + @Transactional + public Student addStudent(String name, Integer age){ + if (!StringUtils.hasText(name) || !StringUtils.hasText(String.valueOf(age))) { + throw new IllegalArgumentException("Student name or age is null or empty"); + } + final Student student = new Student(name, age); + em.persist(student); + return 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; + } + + @Transactional(readOnly = true) + public List findAllStudents() { + return em.createQuery("select s from Student s", Student.class).getResultList(); + } + + @Transactional + public Student updateStudent(Long id, String name, Integer age) { + if (!StringUtils.hasText(name) || !StringUtils.hasText(String.valueOf(age))) { + throw new IllegalArgumentException("Student name or age is null or empty"); + } + final Student currentStudent = findStudent(id); + currentStudent.setName(name); + currentStudent.setAge(age); + return em.merge(currentStudent); + } + + @Transactional + public Student deleteStudent(Long id) { + final Student currentStudent = findStudent(id); + em.remove(currentStudent); + return currentStudent; + } + + @Transactional + public void deleteAllStudent() { + em.createQuery("delete from Student").executeUpdate(); + } }