Сданная лаба
This commit is contained in:
parent
71a1250ae1
commit
7de8e172de
BIN
data.mv.db
BIN
data.mv.db
Binary file not shown.
3783
data.trace.db
3783
data.trace.db
File diff suppressed because it is too large
Load Diff
@ -1,9 +1,11 @@
|
||||
package ru.IP_LabWorks.IP.University.Model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import org.hibernate.annotations.Cascade;
|
||||
import org.hibernate.annotations.OnDelete;
|
||||
import org.hibernate.annotations.OnDeleteAction;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
@ -15,18 +17,24 @@ public class Group {
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
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;
|
||||
|
||||
@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;
|
||||
|
||||
public Group() {
|
||||
this.students = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Group(String name) {
|
||||
this.name = name;
|
||||
this.students = new ArrayList<>();
|
||||
}
|
||||
|
||||
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(){
|
||||
return students;
|
||||
}
|
||||
|
@ -1,8 +1,10 @@
|
||||
package ru.IP_LabWorks.IP.University.Model;
|
||||
import jakarta.persistence.*;
|
||||
import org.hibernate.annotations.OnDelete;
|
||||
import org.hibernate.annotations.OnDeleteAction;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.time.LocalDate;
|
||||
import java.time.Period;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@ -12,21 +14,32 @@ public class Student {
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String name;
|
||||
private Integer age;
|
||||
private LocalDate birthDate;
|
||||
|
||||
@ManyToOne(fetch = FetchType.EAGER)
|
||||
@JoinColumn(name = "group_id")
|
||||
private Group group;
|
||||
|
||||
public Student() {
|
||||
}
|
||||
|
||||
public Student(String name, Integer age) {
|
||||
public Student(String name, LocalDate birthDate) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.birthDate = birthDate;
|
||||
}
|
||||
|
||||
public Long getId() { return id; }
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
public Integer getAge() { return age;}
|
||||
public void setAge(Integer age) { this.age = age; }
|
||||
public LocalDate getBirthDate() { return birthDate;}
|
||||
public void setBirthDate(LocalDate birthDate) { this.birthDate = birthDate; }
|
||||
|
||||
public Integer getAge() {
|
||||
if (birthDate == null) {
|
||||
return 0;
|
||||
}
|
||||
return Period.between(birthDate, LocalDate.now()).getYears();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
@ -46,7 +59,19 @@ public class Student {
|
||||
return "Student{" +
|
||||
"id=" + id +
|
||||
", 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,11 @@
|
||||
package ru.IP_LabWorks.IP.University.Model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import org.hibernate.annotations.Cascade;
|
||||
import org.hibernate.annotations.OnDelete;
|
||||
import org.hibernate.annotations.OnDeleteAction;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@ -15,7 +17,9 @@ public class Subject {
|
||||
private Long id;
|
||||
private String name;
|
||||
@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;
|
||||
|
||||
public Subject() {
|
||||
|
@ -69,12 +69,8 @@ public class GroupService {
|
||||
if (group == null || student == null) {
|
||||
throw new EntityNotFoundException("Group or Student not found");
|
||||
}
|
||||
List<Student> students = group.getStudents();
|
||||
if (!students.contains(student)) {
|
||||
students.add(student);
|
||||
}
|
||||
group.setStudents(students);
|
||||
em.merge(group);
|
||||
student.setGroup(group);
|
||||
em.merge(student);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
|
@ -6,8 +6,10 @@ 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 java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@ -16,11 +18,11 @@ public class StudentService {
|
||||
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");
|
||||
public Student addStudent(String name, LocalDate date){
|
||||
if (!StringUtils.hasText(name) || !StringUtils.hasText(String.valueOf(date))) {
|
||||
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);
|
||||
return student;
|
||||
}
|
||||
@ -40,13 +42,13 @@ public class StudentService {
|
||||
}
|
||||
|
||||
@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");
|
||||
public Student updateStudent(Long id, String name, LocalDate date) {
|
||||
if (!StringUtils.hasText(name) || !StringUtils.hasText(String.valueOf(date))) {
|
||||
throw new IllegalArgumentException("Student name or date is null or empty");
|
||||
}
|
||||
final Student currentStudent = findStudent(id);
|
||||
currentStudent.setName(name);
|
||||
currentStudent.setAge(age);
|
||||
currentStudent.setBirthDate(date);
|
||||
return em.merge(currentStudent);
|
||||
}
|
||||
|
||||
@ -61,4 +63,15 @@ public class StudentService {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -69,7 +69,6 @@ public class SubjectService {
|
||||
Subject subject = findSubject(subjectId);
|
||||
Group group = em.find(Group.class, groupId);
|
||||
subject.getGroups().add(group);
|
||||
group.getSubjects().add(subject);
|
||||
em.merge(subject);
|
||||
em.merge(group);
|
||||
}
|
||||
@ -79,7 +78,6 @@ public class SubjectService {
|
||||
Subject subject = findSubject(subjectId);
|
||||
Group group = em.find(Group.class, groupId);
|
||||
subject.getGroups().remove(group);
|
||||
group.getSubjects().remove(subject);
|
||||
em.merge(subject);
|
||||
em.merge(group);
|
||||
}
|
||||
|
@ -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.StudentService;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootTest
|
||||
@ -31,7 +32,6 @@ public class GroupServiceTests {
|
||||
void TestFindGroup(){
|
||||
groupService.deleteAllGroups();
|
||||
final Group group = groupService.addGroup("ПИбд-21");
|
||||
|
||||
final Group findGroup = groupService.findGroup(group.getId());
|
||||
Assertions.assertEquals(group, findGroup);
|
||||
}
|
||||
@ -41,7 +41,6 @@ public class GroupServiceTests {
|
||||
groupService.deleteAllGroups();
|
||||
final Group group1 = groupService.addGroup("ПИбд-21");
|
||||
final Group group2 = groupService.addGroup("ПИбд-22");
|
||||
|
||||
final List<Group> groups = groupService.findAllGroups();
|
||||
Assertions.assertEquals(groups.size(), 2);
|
||||
}
|
||||
@ -50,7 +49,6 @@ public class GroupServiceTests {
|
||||
void TestUpdateGroup(){
|
||||
groupService.deleteAllGroups();
|
||||
Group group = groupService.addGroup("ПИбд-21");
|
||||
|
||||
group = groupService.updateGroup(group.getId(), "ПИбд-22");
|
||||
Assertions.assertEquals(group.getName(), "ПИбд-22");
|
||||
}
|
||||
@ -67,9 +65,10 @@ public class GroupServiceTests {
|
||||
|
||||
@Test
|
||||
void TestFindStudentsInGroup(){
|
||||
studentService.deleteAllStudent();
|
||||
groupService.deleteAllGroups();
|
||||
Student student1 = studentService.addStudent("Марков Данил Павлович", 19);
|
||||
Student student2 = studentService.addStudent("Емельянов Артём Сергеевич", 19);
|
||||
Student student1 = studentService.addStudent("Марков Данил Павлович", LocalDate.of(2003,8,19));
|
||||
Student student2 = studentService.addStudent("Емельянов Артём Сергеевич", LocalDate.of(2003,7,26));
|
||||
Group group = groupService.addGroup("ПИбд-21");
|
||||
groupService.addStudentToGroup(group.getId(), student1.getId());
|
||||
groupService.addStudentToGroup(group.getId(), student2.getId());
|
||||
|
@ -5,9 +5,12 @@ import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.Service.GroupService;
|
||||
import ru.IP_LabWorks.IP.University.Service.StudentService;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootTest
|
||||
@ -15,17 +18,20 @@ public class StudentServiceTests {
|
||||
@Autowired
|
||||
private StudentService studentService;
|
||||
|
||||
@Autowired
|
||||
private GroupService groupService;
|
||||
|
||||
@Test
|
||||
void TestAddStudent(){
|
||||
studentService.deleteAllStudent();
|
||||
final Student student = studentService.addStudent("Марков Данил Павлович", 19);
|
||||
final Student student = studentService.addStudent("Марков Данил Павлович", LocalDate.of(2003,8,19));
|
||||
Assertions.assertNotNull(student.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void TestFindStudent(){
|
||||
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());
|
||||
Assertions.assertEquals(student, findStudent);
|
||||
@ -34,9 +40,9 @@ public class StudentServiceTests {
|
||||
@Test
|
||||
void TestFindAllStudent(){
|
||||
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();
|
||||
Assertions.assertEquals(students.size(), 2);
|
||||
@ -45,19 +51,29 @@ public class StudentServiceTests {
|
||||
@Test
|
||||
void TestUpdateStudent(){
|
||||
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(), "Емельянов Артём Сергеевич");
|
||||
}
|
||||
|
||||
@Test
|
||||
void TestDeleteStudent(){
|
||||
studentService.deleteAllStudent();
|
||||
Student student = studentService.addStudent("Марков Данил Павлович", 19);
|
||||
Student student = studentService.addStudent("Марков Данил Павлович", LocalDate.of(2003,8,19));
|
||||
studentService.deleteStudent(student.getId());
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
@ -70,6 +70,8 @@ public class SubjectServiceTests {
|
||||
|
||||
@Test
|
||||
void testAddAndFindGroupsInSubject() {
|
||||
subjectService.deleteAllSubjects();
|
||||
groupService.deleteAllGroups();
|
||||
Subject subject = subjectService.addSubject("Math");
|
||||
Group group1 = groupService.addGroup("PIBD-21");
|
||||
Group group2 = groupService.addGroup("PIBD-22");
|
||||
@ -78,14 +80,14 @@ public class SubjectServiceTests {
|
||||
subjectService.addGroupToSubject(subject.getId(), group2.getId());
|
||||
|
||||
List<Group> groups = subjectService.findGroupsBySubject(subject.getId());
|
||||
|
||||
System.out.println(groups);
|
||||
assertEquals(2, groups.size());
|
||||
assertTrue(groups.contains(group1));
|
||||
assertTrue(groups.contains(group2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRemoveGroupFromSubject() {
|
||||
subjectService.deleteAllSubjects();
|
||||
groupService.deleteAllGroups();
|
||||
Subject subject = subjectService.addSubject("Math");
|
||||
Group group1 = groupService.addGroup("PIBD-21");
|
||||
Group group2 = groupService.addGroup("PIBD-22");
|
||||
|
Loading…
Reference in New Issue
Block a user