Все дописано, осталось наладить связи

This commit is contained in:
ArtemEmelyanov 2023-04-15 17:09:21 +04:00
parent 97d5ae0cf4
commit 3ecb50cec8
7 changed files with 114 additions and 29 deletions

Binary file not shown.

View File

@ -55,17 +55,15 @@ public class Group {
return students;
}
public void addStudent(Student student){
if (students == null)
students = new ArrayList<>();
students.add(student);
public void setStudents(List<Student> students) {
this.students = students;
}
public List<Subject> getSubjects() { return subjects; }
public List<Subject> getSubjects(){
return subjects;
}
public void addSubject(Subject subject){
if (subjects == null)
subjects = new ArrayList<>();
subjects.add(subject);
public void setSubjects(List<Subject> subjects) {
this.subjects = subjects;
}
}

View File

@ -12,7 +12,7 @@ public class Subject {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@ManyToMany
@ManyToMany(fetch = FetchType.EAGER)
private List<Group> groups;
public Subject() {
@ -51,4 +51,7 @@ public class Subject {
return groups;
}
public void setGroups(List<Group> groups) {
this.groups = groups;
}
}

View File

@ -64,21 +64,33 @@ public class GroupService {
}
@Transactional
public Group deleteStudentInGroup(Long id, Student student) {
final Group currentGroup = findGroup(id);
final Student currentStudent = em.find(Student.class, student.getId());
currentGroup.getStudents().remove(currentStudent);
em.merge(currentStudent);
return em.merge(currentGroup);
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");
}
List<Student> students = group.getStudents();
if (!students.contains(student)) {
students.add(student);
}
group.setStudents(students);
em.merge(group);
}
public List<Student> findStudentsInGroup(Long id){
final Group findGroup = findGroup(id);
return findGroup.getStudents();
@Transactional
public void removeStudentFromGroup(Long groupId, Long studentId) {
Group group = findGroup(groupId);
Student student = em.find(Student.class, studentId);
group.getStudents().remove(student);
}
public List<Subject> findSubjectsInGroup(Long id){
final Group findGroup = findGroup(id);
return findGroup.getSubjects();
@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();
}
}

View File

@ -3,6 +3,7 @@ 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;
@ -16,6 +17,9 @@ public class SubjectService {
@PersistenceContext
private EntityManager em;
@Autowired
private GroupService groupService;
@Transactional
public Subject addSubject(String name){
if (!StringUtils.hasText(name)) {
@ -61,4 +65,30 @@ public class SubjectService {
public void deleteAllSubjects() {
em.createQuery("delete from Subject").executeUpdate();
}
@Transactional
public void addGroupToSubject(Long subjectId, Long groupId) {
Subject subject = findSubject(subjectId);
Group group = groupService.findGroup(groupId);
subject.getGroups().add(group);
group.getSubjects().add(subject);
em.merge(subject);
em.merge(group);
}
@Transactional
public void removeGroupFromSubject(Long subjectId, Long groupId) {
Subject subject = findSubject(subjectId);
Group group = groupService.findGroup(groupId);
subject.getGroups().remove(group);
group.getSubjects().remove(subject);
em.merge(subject);
em.merge(group);
}
@Transactional(readOnly = true)
public List<Group> findGroupsBySubject(Long subjectId) {
Subject subject = findSubject(subjectId);
return subject.getGroups();
}
}

View File

@ -71,10 +71,9 @@ public class GroupServiceTests {
Student student1 = studentService.addStudent("Марков Данил Павлович", 19);
Student student2 = studentService.addStudent("Емельянов Артём Сергеевич", 19);
Group group = groupService.addGroup("ПИбд-21");
group.addStudent(student1);
group.addStudent(student2);
groupService.addStudentToGroup(group.getId(), student1.getId());
groupService.addStudentToGroup(group.getId(), student2.getId());
final List<Student> students = groupService.findStudentsInGroup(group.getId());
System.out.println("DFGSDFGSDFGSDFG" + students.size());
Assertions.assertEquals(students.size(), 2);
}
}

View File

@ -5,16 +5,24 @@ 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.Subject;
import ru.IP_LabWorks.IP.University.Service.GroupService;
import ru.IP_LabWorks.IP.University.Service.SubjectService;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@SpringBootTest
public class SubjectServiceTests {
@Autowired
private SubjectService subjectService;
@Autowired
private GroupService groupService;
@Test
void TestAddSubject(){
subjectService.deleteAllSubjects();
@ -28,7 +36,7 @@ public class SubjectServiceTests {
final Subject subject = subjectService.addSubject("Философия");
final Subject findSubject = subjectService.findSubject(subject.getId());
Assertions.assertEquals(subject, findSubject);
assertEquals(subject, findSubject);
}
@Test
@ -38,7 +46,7 @@ public class SubjectServiceTests {
final Subject subject2 = subjectService.addSubject("Математика");
final List<Subject> subjects = subjectService.findAllSubjects();
Assertions.assertEquals(subjects.size(), 2);
assertEquals(subjects.size(), 2);
}
@Test
@ -47,7 +55,7 @@ public class SubjectServiceTests {
Subject subject = subjectService.addSubject("Философия");
subject = subjectService.updateSubject(subject.getId(), "Математика");
Assertions.assertEquals(subject.getName(), "Математика");
assertEquals(subject.getName(), "Математика");
}
@Test
@ -59,4 +67,39 @@ public class SubjectServiceTests {
subjectService.findSubject(subject.getId());
});
}
@Test
void testAddAndFindGroupsInSubject() {
Subject subject = subjectService.addSubject("Math");
Group group1 = groupService.addGroup("PIBD-21");
Group group2 = groupService.addGroup("PIBD-22");
subjectService.addGroupToSubject(subject.getId(), group1.getId());
subjectService.addGroupToSubject(subject.getId(), group2.getId());
List<Group> groups = subjectService.findGroupsBySubject(subject.getId());
assertEquals(2, groups.size());
assertTrue(groups.contains(group1));
assertTrue(groups.contains(group2));
}
@Test
void testRemoveGroupFromSubject() {
Subject subject = subjectService.addSubject("Math");
Group group1 = groupService.addGroup("PIBD-21");
Group group2 = groupService.addGroup("PIBD-22");
subjectService.addGroupToSubject(subject.getId(), group1.getId());
subjectService.addGroupToSubject(subject.getId(), group2.getId());
List<Group> groupsBeforeRemove = subjectService.findGroupsBySubject(subject.getId());
assertEquals(2, groupsBeforeRemove.size());
subjectService.removeGroupFromSubject(subject.getId(), group1.getId());
List<Group> groupsAfterRemove = subjectService.findGroupsBySubject(subject.getId());
assertEquals(1, groupsAfterRemove.size());
assertTrue(groupsAfterRemove.contains(group2));
}
}