Lab 3
This commit is contained in:
parent
2683ba9907
commit
d6136f8e3c
30
src/main/java/ru/ulstu/is/cbapp/models/StudentCategory.java
Normal file
30
src/main/java/ru/ulstu/is/cbapp/models/StudentCategory.java
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package ru.ulstu.is.cbapp.models;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "StudentCategory")
|
||||||
|
public class StudentCategory {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
private Student student;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
private Category category;
|
||||||
|
|
||||||
|
public StudentCategory(Student student, Category Category) {
|
||||||
|
this.student = student;
|
||||||
|
this.category = category;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StudentCategory() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -3,13 +3,19 @@ package ru.ulstu.is.cbapp.service;
|
|||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.EntityNotFoundException;
|
import javax.persistence.EntityNotFoundException;
|
||||||
import javax.persistence.PersistenceContext;
|
import javax.persistence.PersistenceContext;
|
||||||
|
import javax.persistence.TypedQuery;
|
||||||
|
|
||||||
|
import org.springframework.data.util.Pair;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
import ru.ulstu.is.cbapp.models.Category;
|
||||||
import ru.ulstu.is.cbapp.models.DrivingSchool;
|
import ru.ulstu.is.cbapp.models.DrivingSchool;
|
||||||
import ru.ulstu.is.cbapp.models.Student;
|
import ru.ulstu.is.cbapp.models.Student;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class DrivingSchoolService {
|
public class DrivingSchoolService {
|
||||||
@ -82,6 +88,28 @@ public class DrivingSchoolService {
|
|||||||
em.merge(currentDrivingSchool);
|
em.merge(currentDrivingSchool);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public List<Pair<Long, Integer>> CountStudentsCategory(Long dsID) {
|
||||||
|
TypedQuery<Object[]> query = em.createQuery("SELECT sc.category.Id, COUNT(sc.category.Id) AS count " +
|
||||||
|
"FROM StudentCategory sc " +
|
||||||
|
"JOIN sc.student s " +
|
||||||
|
"JOIN s.drivingSchool ds " +
|
||||||
|
"WHERE ds.Id=:dsID " +
|
||||||
|
"GROUP BY sc.category.Id", Object[].class)
|
||||||
|
.setParameter("dsID", dsID);
|
||||||
|
|
||||||
|
List<Object[]> resultList = query.getResultList();
|
||||||
|
List<Pair<Long, Integer>> pairs = new ArrayList<>();
|
||||||
|
|
||||||
|
for (Object[] result : resultList) {
|
||||||
|
Long categoryId = (Long) result[0];
|
||||||
|
Integer count = ((Number) result[1]).intValue();
|
||||||
|
Pair<Long, Integer> pair = Pair.of(categoryId, count);
|
||||||
|
pairs.add(pair);
|
||||||
|
}
|
||||||
|
return pairs;
|
||||||
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void addAllStudent(Long id, List<Student> students) {
|
public void addAllStudent(Long id, List<Student> students) {
|
||||||
DrivingSchool currentDrivingSchool = findDrivingSchool(id);
|
DrivingSchool currentDrivingSchool = findDrivingSchool(id);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
spring.main.banner-mode=off
|
spring.main.banner-mode=off
|
||||||
#server.port=8080
|
server.port=8080
|
||||||
spring.datasource.url=jdbc:h2:file:./data
|
spring.datasource.url=jdbc:h2:file:./data
|
||||||
spring.datasource.driverClassName=org.h2.Driver
|
spring.datasource.driverClassName=org.h2.Driver
|
||||||
spring.datasource.username=sa
|
spring.datasource.username=sa
|
||||||
|
@ -5,8 +5,11 @@ 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 org.springframework.data.util.Pair;
|
||||||
|
import ru.ulstu.is.cbapp.models.Category;
|
||||||
import ru.ulstu.is.cbapp.models.DrivingSchool;
|
import ru.ulstu.is.cbapp.models.DrivingSchool;
|
||||||
import ru.ulstu.is.cbapp.models.Student;
|
import ru.ulstu.is.cbapp.models.Student;
|
||||||
|
import ru.ulstu.is.cbapp.service.CategoryService;
|
||||||
import ru.ulstu.is.cbapp.service.DrivingSchoolService;
|
import ru.ulstu.is.cbapp.service.DrivingSchoolService;
|
||||||
import ru.ulstu.is.cbapp.service.StudentService;
|
import ru.ulstu.is.cbapp.service.StudentService;
|
||||||
|
|
||||||
@ -17,7 +20,8 @@ import java.util.List;
|
|||||||
public class DrivingSchoolServiceTests {
|
public class DrivingSchoolServiceTests {
|
||||||
@Autowired
|
@Autowired
|
||||||
private DrivingSchoolService drivingSchoolService;
|
private DrivingSchoolService drivingSchoolService;
|
||||||
|
@Autowired
|
||||||
|
private CategoryService categoryService;
|
||||||
@Autowired
|
@Autowired
|
||||||
private StudentService studentService;
|
private StudentService studentService;
|
||||||
|
|
||||||
@ -78,6 +82,43 @@ public class DrivingSchoolServiceTests {
|
|||||||
drivingSchoolService.deleteAllDrivingSchools();
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGroupStudentCategory() {
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
|
categoryService.deleteAllCategories();
|
||||||
|
|
||||||
|
final String name = "DrivingSchool";
|
||||||
|
DrivingSchool d = drivingSchoolService.addDrivingSchool(name);
|
||||||
|
|
||||||
|
Student s1 = studentService.addStudent("cha", "chacha", "111");
|
||||||
|
Student s2 = studentService.addStudent("cha", "chacha", "111");
|
||||||
|
Student s3 = studentService.addStudent("cha", "chacha", "111");
|
||||||
|
|
||||||
|
Category c1 = categoryService.addCategory("Category1");
|
||||||
|
Category c2 = categoryService.addCategory("Category2");
|
||||||
|
Category c3 = categoryService.addCategory("Category3");
|
||||||
|
|
||||||
|
studentService.addCategory(s1.getId(), c1);
|
||||||
|
studentService.addCategory(s2.getId(), c2);
|
||||||
|
studentService.addCategory(s3.getId(), c3);
|
||||||
|
|
||||||
|
List<Student> studs = new ArrayList<>();
|
||||||
|
studs.add(s1);
|
||||||
|
studs.add(s2);
|
||||||
|
studs.add(s3);
|
||||||
|
|
||||||
|
drivingSchoolService.addAllStudent(d.getId(), studs);
|
||||||
|
|
||||||
|
List<Pair<Long,Integer>> list=drivingSchoolService.CountStudentsCategory(d.getId());
|
||||||
|
|
||||||
|
Assertions.assertNotNull(list);
|
||||||
|
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
|
categoryService.deleteAllCategories();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAddStudent() {
|
public void testAddStudent() {
|
||||||
|
|
||||||
@ -91,6 +132,7 @@ public class DrivingSchoolServiceTests {
|
|||||||
Student s2 = studentService.addStudent("cha", "chacha", "111");
|
Student s2 = studentService.addStudent("cha", "chacha", "111");
|
||||||
Student s3 = studentService.addStudent("cha", "chacha", "111");
|
Student s3 = studentService.addStudent("cha", "chacha", "111");
|
||||||
|
|
||||||
|
|
||||||
List<Student> studs = new ArrayList<>();
|
List<Student> studs = new ArrayList<>();
|
||||||
studs.add(s1);
|
studs.add(s1);
|
||||||
studs.add(s2);
|
studs.add(s2);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user