This commit is contained in:
AnnZhimol 2023-04-02 17:10:24 +04:00
parent d90389cfbe
commit 92e9b29a1e
5 changed files with 70 additions and 18 deletions

View File

@ -11,7 +11,7 @@ public class DrivingSchool {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long Id;
@Column(unique = true)
private String name;
@OneToMany(mappedBy = "drivingSchool", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Student> students = new ArrayList<>();

View File

@ -13,7 +13,7 @@ public class Student {
private String name;
private String surname;
@ManyToOne(fetch = FetchType.EAGER)
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private DrivingSchool drivingSchool;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)

View File

@ -82,6 +82,15 @@ public class DrivingSchoolService {
em.merge(currentDrivingSchool);
}
@Transactional
public void addAllStudent(Long id, List<Student> students) {
DrivingSchool currentDrivingSchool = findDrivingSchool(id);
for(int i=0; i<students.size(); i++) {
currentDrivingSchool.addNewStudent(students.get(i));
em.merge(currentDrivingSchool);
}
}
@Transactional
public void deleteStudent(Long id, Student student) {
DrivingSchool currentDrivingSchool = findDrivingSchool(id);

View File

@ -41,12 +41,12 @@ public class CategoryServiceTests {
categoryService.deleteAllCategories();
Category p = categoryService.addCategory("Category");
Student e = studentService.addStudent("1", "2", "33");
Student e1 = studentService.addStudent("1", "2", "33");
studentService.addCategory(e.getId(), p);
studentService.deleteCategory(e.getId(), p);
Assertions.assertFalse(studentService.findStudent(e.getId()).getCategories().contains(p));
Assertions.assertFalse(categoryService.findCategory(p.getId()).getStudents().contains(e));
studentService.addCategory(e1.getId(), p);
studentService.deleteCategory(e1.getId(), p);
Assertions.assertFalse(studentService.findStudent(e1.getId()).getCategories().contains(p));
Assertions.assertFalse(categoryService.findCategory(p.getId()).getStudents().contains(e1));
studentService.deleteAllStudents();
categoryService.deleteAllCategories();

View File

@ -10,6 +10,9 @@ import ru.ulstu.is.cbapp.models.Student;
import ru.ulstu.is.cbapp.service.DrivingSchoolService;
import ru.ulstu.is.cbapp.service.StudentService;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
public class DrivingSchoolServiceTests {
@Autowired
@ -81,14 +84,23 @@ public class DrivingSchoolServiceTests {
studentService.deleteAllStudents();
drivingSchoolService.deleteAllDrivingSchools();
final String name = "DrivingSchool";
DrivingSchool d = drivingSchoolService.addDrivingSchool(name);
Student newStudent = studentService.addStudent("cha", "chacha", "111");
drivingSchoolService.addNewStudent(d.getId(), newStudent);
Assertions.assertTrue(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(newStudent));
Student s1 = studentService.addStudent("cha", "chacha", "111");
Student s2 = studentService.addStudent("cha", "chacha", "111");
Student s3 = studentService.addStudent("cha", "chacha", "111");
List<Student> studs = new ArrayList<>();
studs.add(s1);
studs.add(s2);
studs.add(s3);
drivingSchoolService.addAllStudent(d.getId(), studs);
Assertions.assertTrue(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(s1));
Assertions.assertTrue(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(s2));
Assertions.assertTrue(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(s3));
studentService.deleteAllStudents();
drivingSchoolService.deleteAllDrivingSchools();
@ -99,20 +111,51 @@ public class DrivingSchoolServiceTests {
studentService.deleteAllStudents();
drivingSchoolService.deleteAllDrivingSchools();
final String name = "DrivingSchool";
DrivingSchool d = drivingSchoolService.addDrivingSchool(name);
Student newStudent = studentService.addStudent("cha", "chacha", "111");
Student student1 = studentService.addStudent("cha1", "chacha1", "111");
Student student2 = studentService.addStudent("cha2", "chacha2", "1111");
Student student3 = studentService.addStudent("cha3", "chacha3", "11111");
drivingSchoolService.addNewStudent(d.getId(), newStudent);
Assertions.assertTrue(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(newStudent));
drivingSchoolService.addNewStudent(d.getId(), student1);
drivingSchoolService.addNewStudent(d.getId(), student2);
drivingSchoolService.addNewStudent(d.getId(), student3);
drivingSchoolService.deleteStudent(d.getId(), newStudent);
DrivingSchool cFromDB = drivingSchoolService.findDrivingSchool(d.getId());
Assertions.assertFalse(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(newStudent));
Assertions.assertTrue(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(student1));
Assertions.assertTrue(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(student2));
Assertions.assertTrue(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(student3));
DrivingSchool ds=drivingSchoolService.findDrivingSchool(d.getId());
Assertions.assertNotNull(ds);
Assertions.assertTrue(ds.getStudents().contains(student1));
Assertions.assertTrue(ds.getStudents().contains(student2));
Assertions.assertTrue(ds.getStudents().contains(student3));
studentService.deleteAllStudents();
drivingSchoolService.deleteAllDrivingSchools();
}
@Test
public void testFindAllStudent(){
studentService.deleteAllStudents();
drivingSchoolService.deleteAllDrivingSchools();
DrivingSchool d = drivingSchoolService.addDrivingSchool("ds");
Student student1 = studentService.addStudent("cha1", "chacha1", "1111");
Student student2 = studentService.addStudent("cha2", "chacha2", "11111");
Student student3 = studentService.addStudent("cha3", "chacha3", "111111");
drivingSchoolService.addNewStudent(d.getId(), student1);
drivingSchoolService.addNewStudent(d.getId(), student2);
drivingSchoolService.addNewStudent(d.getId(), student3);
Assertions.assertTrue(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(student1));
Assertions.assertTrue(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(student2));
Assertions.assertTrue(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(student3));
studentService.deleteAllStudents();
drivingSchoolService.deleteAllDrivingSchools();
}
}