Сданная лаба

This commit is contained in:
ArtemEmelyanov 2023-04-21 13:38:51 +04:00
parent 71a1250ae1
commit 7de8e172de
11 changed files with 3892 additions and 42 deletions

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +1,11 @@
package ru.IP_LabWorks.IP.University.Model; package ru.IP_LabWorks.IP.University.Model;
import jakarta.persistence.*; import jakarta.persistence.*;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.OnDelete; import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction; import org.hibernate.annotations.OnDeleteAction;
import javax.swing.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -15,18 +17,24 @@ public class Group {
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; private Long id;
private String name; private String name;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@OneToMany(orphanRemoval = true, fetch = FetchType.EAGER, mappedBy = "group")
@OnDelete(action = OnDeleteAction.CASCADE)
private List<Student> students; private List<Student> students;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "subject_group",
joinColumns = {@JoinColumn(name = "group_id")},
inverseJoinColumns = {@JoinColumn(name = "subject_id")})
private List<Subject> subjects; private List<Subject> subjects;
public Group() { public Group() {
this.students = new ArrayList<>();
} }
public Group(String name) { public Group(String name) {
this.name = name; this.name = name;
this.students = new ArrayList<>();
} }
public Long getId() { return id; } public Long getId() { return id; }
@ -54,6 +62,12 @@ public class Group {
'}'; '}';
} }
public void setStudent(Student student){
this.students.add(student);
if(student.getGroup() != this){
student.setGroup(this);
}
}
public List<Student> getStudents(){ public List<Student> getStudents(){
return students; return students;
} }

View File

@ -1,8 +1,10 @@
package ru.IP_LabWorks.IP.University.Model; package ru.IP_LabWorks.IP.University.Model;
import jakarta.persistence.*; import jakarta.persistence.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import java.util.ArrayList; import java.time.LocalDate;
import java.util.List; import java.time.Period;
import java.util.Objects; import java.util.Objects;
@Entity @Entity
@ -12,21 +14,32 @@ public class Student {
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; private Long id;
private String name; private String name;
private Integer age; private LocalDate birthDate;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "group_id")
private Group group;
public Student() { public Student() {
} }
public Student(String name, Integer age) { public Student(String name, LocalDate birthDate) {
this.name = name; this.name = name;
this.age = age; this.birthDate = birthDate;
} }
public Long getId() { return id; } public Long getId() { return id; }
public String getName() { return name; } public String getName() { return name; }
public void setName(String name) { this.name = name; } public void setName(String name) { this.name = name; }
public Integer getAge() { return age;} public LocalDate getBirthDate() { return birthDate;}
public void setAge(Integer age) { this.age = age; } public void setBirthDate(LocalDate birthDate) { this.birthDate = birthDate; }
public Integer getAge() {
if (birthDate == null) {
return 0;
}
return Period.between(birthDate, LocalDate.now()).getYears();
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
@ -46,7 +59,19 @@ public class Student {
return "Student{" + return "Student{" +
"id=" + id + "id=" + id +
", name='" + name + '\'' + ", name='" + name + '\'' +
", age='" + age + '\'' + ", birthDate='" + birthDate + '\'' +
", age='" + getAge() + '\'' +
'}'; '}';
} }
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
if(!group.getStudents().contains(this)){
group.getStudents().add(this);
}
}
} }

View File

@ -1,9 +1,11 @@
package ru.IP_LabWorks.IP.University.Model; package ru.IP_LabWorks.IP.University.Model;
import jakarta.persistence.*; import jakarta.persistence.*;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.OnDelete; import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction; import org.hibernate.annotations.OnDeleteAction;
import javax.swing.*;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -15,7 +17,9 @@ public class Subject {
private Long id; private Long id;
private String name; private String name;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@OnDelete(action = OnDeleteAction.CASCADE) @JoinTable(name = "subject_group",
joinColumns = {@JoinColumn(name = "subject_id")},
inverseJoinColumns = {@JoinColumn(name = "group_id")})
private List<Group> groups; private List<Group> groups;
public Subject() { public Subject() {

View File

@ -69,12 +69,8 @@ public class GroupService {
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");
} }
List<Student> students = group.getStudents(); student.setGroup(group);
if (!students.contains(student)) { em.merge(student);
students.add(student);
}
group.setStudents(students);
em.merge(group);
} }
@Transactional @Transactional

View File

@ -6,8 +6,10 @@ 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.Student; import ru.IP_LabWorks.IP.University.Model.Student;
import java.time.LocalDate;
import java.util.List; import java.util.List;
@Service @Service
@ -16,11 +18,11 @@ public class StudentService {
private EntityManager em; private EntityManager em;
@Transactional @Transactional
public Student addStudent(String name, Integer age){ public Student addStudent(String name, LocalDate date){
if (!StringUtils.hasText(name) || !StringUtils.hasText(String.valueOf(age))) { if (!StringUtils.hasText(name) || !StringUtils.hasText(String.valueOf(date))) {
throw new IllegalArgumentException("Student name or age is null or empty"); throw new IllegalArgumentException("Student name or date is null or empty");
} }
final Student student = new Student(name, age); final Student student = new Student(name, date);
em.persist(student); em.persist(student);
return student; return student;
} }
@ -40,13 +42,13 @@ public class StudentService {
} }
@Transactional @Transactional
public Student updateStudent(Long id, String name, Integer age) { public Student updateStudent(Long id, String name, LocalDate date) {
if (!StringUtils.hasText(name) || !StringUtils.hasText(String.valueOf(age))) { if (!StringUtils.hasText(name) || !StringUtils.hasText(String.valueOf(date))) {
throw new IllegalArgumentException("Student name or age is null or empty"); throw new IllegalArgumentException("Student name or date is null or empty");
} }
final Student currentStudent = findStudent(id); final Student currentStudent = findStudent(id);
currentStudent.setName(name); currentStudent.setName(name);
currentStudent.setAge(age); currentStudent.setBirthDate(date);
return em.merge(currentStudent); return em.merge(currentStudent);
} }
@ -61,4 +63,15 @@ public class StudentService {
public void deleteAllStudent() { public void deleteAllStudent() {
em.createQuery("delete from Student").executeUpdate(); 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);
}
} }

View File

@ -69,7 +69,6 @@ public class SubjectService {
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);
group.getSubjects().add(subject);
em.merge(subject); em.merge(subject);
em.merge(group); em.merge(group);
} }
@ -79,7 +78,6 @@ public class SubjectService {
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);
group.getSubjects().remove(subject);
em.merge(subject); em.merge(subject);
em.merge(group); em.merge(group);
} }

View File

@ -10,6 +10,7 @@ import ru.IP_LabWorks.IP.University.Model.Student;
import ru.IP_LabWorks.IP.University.Service.GroupService; import ru.IP_LabWorks.IP.University.Service.GroupService;
import ru.IP_LabWorks.IP.University.Service.StudentService; import ru.IP_LabWorks.IP.University.Service.StudentService;
import java.time.LocalDate;
import java.util.List; import java.util.List;
@SpringBootTest @SpringBootTest
@ -31,7 +32,6 @@ public class GroupServiceTests {
void TestFindGroup(){ void TestFindGroup(){
groupService.deleteAllGroups(); groupService.deleteAllGroups();
final Group group = groupService.addGroup("ПИбд-21"); final Group group = groupService.addGroup("ПИбд-21");
final Group findGroup = groupService.findGroup(group.getId()); final Group findGroup = groupService.findGroup(group.getId());
Assertions.assertEquals(group, findGroup); Assertions.assertEquals(group, findGroup);
} }
@ -41,7 +41,6 @@ public class GroupServiceTests {
groupService.deleteAllGroups(); groupService.deleteAllGroups();
final Group group1 = groupService.addGroup("ПИбд-21"); final Group group1 = groupService.addGroup("ПИбд-21");
final Group group2 = groupService.addGroup("ПИбд-22"); final Group group2 = groupService.addGroup("ПИбд-22");
final List<Group> groups = groupService.findAllGroups(); final List<Group> groups = groupService.findAllGroups();
Assertions.assertEquals(groups.size(), 2); Assertions.assertEquals(groups.size(), 2);
} }
@ -50,7 +49,6 @@ public class GroupServiceTests {
void TestUpdateGroup(){ void TestUpdateGroup(){
groupService.deleteAllGroups(); groupService.deleteAllGroups();
Group group = groupService.addGroup("ПИбд-21"); Group group = groupService.addGroup("ПИбд-21");
group = groupService.updateGroup(group.getId(), "ПИбд-22"); group = groupService.updateGroup(group.getId(), "ПИбд-22");
Assertions.assertEquals(group.getName(), "ПИбд-22"); Assertions.assertEquals(group.getName(), "ПИбд-22");
} }
@ -67,9 +65,10 @@ public class GroupServiceTests {
@Test @Test
void TestFindStudentsInGroup(){ void TestFindStudentsInGroup(){
studentService.deleteAllStudent();
groupService.deleteAllGroups(); groupService.deleteAllGroups();
Student student1 = studentService.addStudent("Марков Данил Павлович", 19); Student student1 = studentService.addStudent("Марков Данил Павлович", LocalDate.of(2003,8,19));
Student student2 = studentService.addStudent("Емельянов Артём Сергеевич", 19); Student student2 = studentService.addStudent("Емельянов Артём Сергеевич", LocalDate.of(2003,7,26));
Group group = groupService.addGroup("ПИбд-21"); Group group = groupService.addGroup("ПИбд-21");
groupService.addStudentToGroup(group.getId(), student1.getId()); groupService.addStudentToGroup(group.getId(), student1.getId());
groupService.addStudentToGroup(group.getId(), student2.getId()); groupService.addStudentToGroup(group.getId(), student2.getId());

View File

@ -5,9 +5,12 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; 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.Model.Student;
import ru.IP_LabWorks.IP.University.Service.GroupService;
import ru.IP_LabWorks.IP.University.Service.StudentService; import ru.IP_LabWorks.IP.University.Service.StudentService;
import java.time.LocalDate;
import java.util.List; import java.util.List;
@SpringBootTest @SpringBootTest
@ -15,17 +18,20 @@ public class StudentServiceTests {
@Autowired @Autowired
private StudentService studentService; private StudentService studentService;
@Autowired
private GroupService groupService;
@Test @Test
void TestAddStudent(){ void TestAddStudent(){
studentService.deleteAllStudent(); studentService.deleteAllStudent();
final Student student = studentService.addStudent("Марков Данил Павлович", 19); final Student student = studentService.addStudent("Марков Данил Павлович", LocalDate.of(2003,8,19));
Assertions.assertNotNull(student.getId()); Assertions.assertNotNull(student.getId());
} }
@Test @Test
void TestFindStudent(){ void TestFindStudent(){
studentService.deleteAllStudent(); studentService.deleteAllStudent();
final Student student = studentService.addStudent("Марков Данил Павлович", 19); final Student student = studentService.addStudent("Марков Данил Павлович", LocalDate.of(2003,8,19));
final Student findStudent = studentService.findStudent(student.getId()); final Student findStudent = studentService.findStudent(student.getId());
Assertions.assertEquals(student, findStudent); Assertions.assertEquals(student, findStudent);
@ -34,9 +40,9 @@ public class StudentServiceTests {
@Test @Test
void TestFindAllStudent(){ void TestFindAllStudent(){
studentService.deleteAllStudent(); studentService.deleteAllStudent();
final Student student1 = studentService.addStudent("Марков Данил Павлович", 19); final Student student1 = studentService.addStudent("Марков Данил Павлович", LocalDate.of(2003,8,19));
final Student student2 = studentService.addStudent("Емельянов Артём Сергеевич", 19); final Student student2 = studentService.addStudent("Емельянов Артём Сергеевич", LocalDate.of(2003,7,26));
final List<Student> students = studentService.findAllStudents(); final List<Student> students = studentService.findAllStudents();
Assertions.assertEquals(students.size(), 2); Assertions.assertEquals(students.size(), 2);
@ -45,19 +51,29 @@ public class StudentServiceTests {
@Test @Test
void TestUpdateStudent(){ void TestUpdateStudent(){
studentService.deleteAllStudent(); studentService.deleteAllStudent();
Student student = studentService.addStudent("Марков Данил Павлович", 19); Student student = studentService.addStudent("Марков Данил Павлович", LocalDate.of(2003,8,19));
student = studentService.updateStudent(student.getId(), "Емельянов Артём Сергеевич", 19); student = studentService.updateStudent(student.getId(), "Емельянов Артём Сергеевич", LocalDate.of(2003,7,26));
Assertions.assertEquals(student.getName(), "Емельянов Артём Сергеевич"); Assertions.assertEquals(student.getName(), "Емельянов Артём Сергеевич");
} }
@Test @Test
void TestDeleteStudent(){ void TestDeleteStudent(){
studentService.deleteAllStudent(); studentService.deleteAllStudent();
Student student = studentService.addStudent("Марков Данил Павлович", 19); Student student = studentService.addStudent("Марков Данил Павлович", LocalDate.of(2003,8,19));
studentService.deleteStudent(student.getId()); studentService.deleteStudent(student.getId());
Assertions.assertThrows(EntityNotFoundException.class, () -> { Assertions.assertThrows(EntityNotFoundException.class, () -> {
studentService.findStudent(student.getId()); studentService.findStudent(student.getId());
}); });
} }
@Test
void TestAddStudentToGroup(){
studentService.deleteAllStudent();
groupService.deleteAllGroups();
Student student = studentService.addStudent("Марков Данил Павлович", LocalDate.of(2003,8,19));
Group group = groupService.addGroup("ПИбд-21");
studentService.AddStudentToGroup(student.getId(), group.getId());
Assertions.assertEquals(group,studentService.findStudent(student.getId()).getGroup());
}
} }

View File

@ -70,6 +70,8 @@ public class SubjectServiceTests {
@Test @Test
void testAddAndFindGroupsInSubject() { void testAddAndFindGroupsInSubject() {
subjectService.deleteAllSubjects();
groupService.deleteAllGroups();
Subject subject = subjectService.addSubject("Math"); Subject subject = subjectService.addSubject("Math");
Group group1 = groupService.addGroup("PIBD-21"); Group group1 = groupService.addGroup("PIBD-21");
Group group2 = groupService.addGroup("PIBD-22"); Group group2 = groupService.addGroup("PIBD-22");
@ -78,14 +80,14 @@ public class SubjectServiceTests {
subjectService.addGroupToSubject(subject.getId(), group2.getId()); subjectService.addGroupToSubject(subject.getId(), group2.getId());
List<Group> groups = subjectService.findGroupsBySubject(subject.getId()); List<Group> groups = subjectService.findGroupsBySubject(subject.getId());
System.out.println(groups);
assertEquals(2, groups.size()); assertEquals(2, groups.size());
assertTrue(groups.contains(group1));
assertTrue(groups.contains(group2));
} }
@Test @Test
void testRemoveGroupFromSubject() { void testRemoveGroupFromSubject() {
subjectService.deleteAllSubjects();
groupService.deleteAllGroups();
Subject subject = subjectService.addSubject("Math"); Subject subject = subjectService.addSubject("Math");
Group group1 = groupService.addGroup("PIBD-21"); Group group1 = groupService.addGroup("PIBD-21");
Group group2 = groupService.addGroup("PIBD-22"); Group group2 = groupService.addGroup("PIBD-22");