This commit is contained in:
AnnZhimol 2023-04-03 13:22:51 +04:00
parent 2683ba9907
commit eb03fcc404
14 changed files with 365 additions and 61 deletions

View File

@ -12,12 +12,20 @@ repositories {
mavenCentral()
}
jar {
enabled = false
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.h2database:h2:2.1.210'
implementation 'org.hibernate.validator:hibernate-validator'
implementation 'org.springdoc:springdoc-openapi-ui:1.6.5'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

View File

@ -0,0 +1,45 @@
package ru.ulstu.is.cbapp.controller;
import org.springframework.web.bind.annotation.*;
import ru.ulstu.is.cbapp.dto.CategoryDto;
import ru.ulstu.is.cbapp.service.CategoryService;
import java.util.List;
@RestController
@RequestMapping("/category")
public class CategoryController {
private final CategoryService categoryService;
public CategoryController(CategoryService categoryService) {
this.categoryService = categoryService;
}
@GetMapping("/{id}")
public CategoryDto getCategory(@PathVariable long id) {
return new CategoryDto(categoryService.findCategory(id));
}
@GetMapping
public List<CategoryDto> getAllCategories() {
return categoryService.findAllCategories().stream()
.map(CategoryDto::new)
.toList();
}
@PostMapping
public CategoryDto create(@RequestParam("name") String name) {
return new CategoryDto(categoryService.addCategory(name));
}
@PutMapping("/{id}")
public CategoryDto update(@PathVariable Long id,
@RequestParam("name") String name) {
return new CategoryDto(categoryService.updateCategory(id, name));
}
@DeleteMapping("/{id}")
public CategoryDto delete(@PathVariable Long id) {
return new CategoryDto(categoryService.deleteCategory(id));
}
}

View File

@ -0,0 +1,63 @@
package ru.ulstu.is.cbapp.controller;
import org.springframework.web.bind.annotation.*;
import ru.ulstu.is.cbapp.dto.DrivingSchoolDto;
import ru.ulstu.is.cbapp.dto.StudentDto;
import ru.ulstu.is.cbapp.models.Student;
import ru.ulstu.is.cbapp.service.DrivingSchoolService;
import ru.ulstu.is.cbapp.service.StudentService;
import java.util.List;
@RestController
@RequestMapping("/drivingSchool")
public class DrivingSchoolController {
private final DrivingSchoolService drivingSchoolService;
private final StudentService studentService;
public DrivingSchoolController(DrivingSchoolService drivingSchoolService, StudentService studentService) {
this.drivingSchoolService = drivingSchoolService;
this.studentService = studentService;
}
@GetMapping("/{id}")
public DrivingSchoolDto getDrivingSchool(@PathVariable long id) {
return new DrivingSchoolDto(drivingSchoolService.findDrivingSchool(id));
}
@GetMapping
public List<DrivingSchoolDto> getAllDrivingSchools() {
return drivingSchoolService.findAllDrivingSchools().stream()
.map(DrivingSchoolDto::new)
.toList();
}
@PostMapping
public DrivingSchoolDto create(@RequestParam("name") String name) {
return new DrivingSchoolDto(drivingSchoolService.addDrivingSchool(name));
}
@PutMapping("/{id}")
public DrivingSchoolDto update(@PathVariable Long id,
@RequestParam("name") String name) {
return new DrivingSchoolDto(drivingSchoolService.updateDrivingSchool(id, name));
}
@DeleteMapping("/{id}")
public DrivingSchoolDto delete(@PathVariable Long id) {
return new DrivingSchoolDto(drivingSchoolService.deleteDrivingSchool(id));
}
@PutMapping("/{id}/hire")
public StudentDto hire(@PathVariable Long id, @RequestParam Long studentId) {
Student s = studentService.findStudent(studentId);
return new StudentDto(drivingSchoolService.addNewStudent(id, s));
}
@PutMapping("/{id}/dismiss")
public StudentDto dismiss(@PathVariable Long id, @RequestParam Long studentId) {
Student s = studentService.findStudent(studentId);
return new StudentDto(drivingSchoolService.deleteStudent(id, s));
}
}

View File

@ -0,0 +1,73 @@
package ru.ulstu.is.cbapp.controller;
import org.springframework.web.bind.annotation.*;
import ru.ulstu.is.cbapp.dto.StudentDto;
import ru.ulstu.is.cbapp.models.Category;
import ru.ulstu.is.cbapp.service.CategoryService;
import ru.ulstu.is.cbapp.service.StudentService;
import java.util.List;
@RestController
@RequestMapping("/student")
public class StudentController {
private final StudentService studentService;
private final CategoryService categoryService;
public StudentController(StudentService studentService, CategoryService categoryService) {
this.studentService = studentService;
this.categoryService = categoryService;
}
@GetMapping("/{id}")
public StudentDto getStudent(@PathVariable long id) {
return new StudentDto(studentService.findStudent(id));
}
@GetMapping
public List<StudentDto> getAllStudents() {
return studentService.findAllStudents().stream()
.map(StudentDto::new)
.toList();
}
@PostMapping
public StudentDto createStudent(@RequestParam("name") String name,
@RequestParam("surname") String surname,
@RequestParam("phoneNumber") String phoneNumber) {
return new StudentDto(studentService.addStudent(
surname,
name,
phoneNumber));
}
@PutMapping("/{id}")
public StudentDto updateStudent(@PathVariable Long id,
@RequestParam("name") String name,
@RequestParam("surname") String surname,
@RequestParam("phoneNumber") String phoneNumber) {
return new StudentDto(studentService.updateStudent(id, surname, name, phoneNumber));
}
@DeleteMapping("/{id}")
public StudentDto deleteEmployee(@PathVariable Long id) {
return new StudentDto(studentService.deleteStudent(id));
}
@PutMapping("/{id}/addCat")
public StudentDto addCategory(@PathVariable Long id,
@RequestParam("category") Long category) {
Category c = categoryService.findCategory(category);
if (c == null)
return null;
return new StudentDto(studentService.addCategory(id, c));
}
@PutMapping("/{id}/delCat")
public StudentDto delCategory(@PathVariable Long id,
@RequestParam("category") Long category) {
Category c = categoryService.findCategory(category);
if (c == null)
return null;
return new StudentDto(studentService.deleteCategory(id, c));
}
}

View File

@ -0,0 +1,10 @@
package ru.ulstu.is.cbapp.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.is.cbapp.models.Category;
import java.util.Optional;
public interface CategoryRepository extends JpaRepository<Category, Long> {
Optional<Category> findById(Long id);
}

View File

@ -0,0 +1,10 @@
package ru.ulstu.is.cbapp.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.is.cbapp.models.DrivingSchool;
import java.util.Optional;
public interface DrivingSchoolRepository extends JpaRepository<DrivingSchool, Long> {
Optional<DrivingSchool> findById(Long id);
}

View File

@ -0,0 +1,10 @@
package ru.ulstu.is.cbapp.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.is.cbapp.models.Student;
import java.util.Optional;
public interface StudentRepository extends JpaRepository<Student, Long> {
Optional<Student> findById(Long id);
}

View File

@ -0,0 +1,21 @@
package ru.ulstu.is.cbapp.dto;
import ru.ulstu.is.cbapp.models.Category;
public class CategoryDto {
private final Long Id;
private final String name;
public CategoryDto(Category category) {
Id = category.getId();
this.name = category.getName();
}
public Long getId() {
return Id;
}
public String getName() {
return name;
}
}

View File

@ -0,0 +1,29 @@
package ru.ulstu.is.cbapp.dto;
import ru.ulstu.is.cbapp.models.DrivingSchool;
import java.util.List;
public class DrivingSchoolDto {
private final Long Id;
private final String name;
private final List<StudentDto> students;
public DrivingSchoolDto(DrivingSchool drivingSchool) {
Id = drivingSchool.getId();
this.name = drivingSchool.getName();
this.students = drivingSchool.getStudents().stream().map(StudentDto::new).toList();
}
public Long getId() {
return Id;
}
public String getName() {
return name;
}
public List<StudentDto> getStudents() {
return students;
}
}

View File

@ -0,0 +1,41 @@
package ru.ulstu.is.cbapp.dto;
import ru.ulstu.is.cbapp.models.Student;
import java.util.List;
public class StudentDto {
private final Long Id;
private final String name;
private final String phoneNumber;
private final String surname;
private final List<CategoryDto> categories;
public StudentDto(Student student) {
Id = student.getId();
this.name = student.getName();
this.phoneNumber = student.getPhoneNumber();
this.surname = student.getSurname();
this.categories = student.getCategories().stream().map(CategoryDto::new).toList();
}
public Long getId() {
return Id;
}
public String getName() {
return name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public String getSurname() {
return surname;
}
public List<CategoryDto> getCategories() {
return categories;
}
}

View File

@ -11,7 +11,6 @@ public class Category {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long Id;
private String name;
//mappedBy - атрибут, указывающий, что классом-владельцем отношений является другой класс

View File

@ -1,9 +1,8 @@
package ru.ulstu.is.cbapp.service;
import ru.ulstu.is.cbapp.dao.CategoryRepository;
import ru.ulstu.is.cbapp.models.Category;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
@ -12,22 +11,25 @@ import java.util.List;
@Service
public class CategoryService {
@PersistenceContext
private EntityManager em;
private CategoryRepository categoryRepository;
public CategoryService(CategoryRepository categoryRepository) {
this.categoryRepository = categoryRepository;
}
@Transactional
public Category addCategory(String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Student's data is null or empty");
}
final Category category = new Category(name);
em.persist(category);
categoryRepository.save(category);
return category;
}
@Transactional
public Category findCategory(Long id) {
final Category category = em.find(Category.class, id);
final Category category = categoryRepository.findById(id).orElse(null);
if (category == null) {
throw new EntityNotFoundException(String.format("Category with id [%s] is not found", id));
}
@ -36,31 +38,30 @@ public class CategoryService {
@Transactional(readOnly = true)
public List<Category> findAllCategories() {
return em.createQuery("select p from Category p", Category.class)
.getResultList();
return categoryRepository.findAll();
}
@Transactional
public Category updateCategory(Long id, String surname, String name, String phoneNumber) {
if (!StringUtils.hasText(name) ||!StringUtils.hasText(surname) || !StringUtils.hasText(phoneNumber)) {
throw new IllegalArgumentException("Student's data is null or empty");
public Category updateCategory(Long id, String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Category's data is null or empty");
}
final Category currentCategory = findCategory(id);
currentCategory.setName(name);
return em.merge(currentCategory);
return categoryRepository.save(currentCategory);
}
@Transactional
public Category deleteCategory(Long id) {
final Category p = findCategory(id);
em.remove(p);
categoryRepository.delete(p);
return p;
}
@Transactional
public void deleteAllCategories() {
em.createQuery("delete from Category").executeUpdate();
categoryRepository.deleteAll();
}

View File

@ -1,11 +1,11 @@
package ru.ulstu.is.cbapp.service;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import ru.ulstu.is.cbapp.dao.DrivingSchoolRepository;
import ru.ulstu.is.cbapp.dao.StudentRepository;
import ru.ulstu.is.cbapp.models.DrivingSchool;
import ru.ulstu.is.cbapp.models.Student;
@ -13,8 +13,13 @@ import java.util.List;
@Service
public class DrivingSchoolService {
@PersistenceContext
private EntityManager em;
private DrivingSchoolRepository drivingSchoolRepository;
private StudentRepository studentRepository;
public DrivingSchoolService(DrivingSchoolRepository drivingSchoolRepository, StudentRepository studentRepository) {
this.drivingSchoolRepository = drivingSchoolRepository;
this.studentRepository = studentRepository;
}
@Transactional
public DrivingSchool addDrivingSchool(String name) {
@ -22,34 +27,22 @@ public class DrivingSchoolService {
throw new IllegalArgumentException("Student name is null or empty");
}
final DrivingSchool drivingSchool = new DrivingSchool(name);
em.persist(drivingSchool);
drivingSchoolRepository.save(drivingSchool);
return drivingSchool;
}
@Transactional(readOnly = true)
public DrivingSchool findDrivingSchool(Long id) {
final DrivingSchool drivingSchool = em.find(DrivingSchool.class, id);
final DrivingSchool drivingSchool = drivingSchoolRepository.findById(id).orElse(null);
if (drivingSchool == null) {
throw new EntityNotFoundException(String.format("DrivingSchool with id [%s] is not found", id));
}
return drivingSchool;
}
@Transactional(readOnly = true)
public DrivingSchool findDrivingSchool(String name) {
DrivingSchool drivingSchool = em.createQuery("select d from DrivingSchool d WHERE d.name = :drivingSchoolName", DrivingSchool.class)
.setParameter("drivingSchoolName",name)
.getSingleResult();
if (drivingSchool == null) {
throw new EntityNotFoundException(String.format("DrivingSchool with name [%s] is not found", name));
}
return drivingSchool;
}
@Transactional(readOnly = true)
public List<DrivingSchool> findAllDrivingSchools() {
return em.createQuery("select d from DrivingSchool d", DrivingSchool.class)
.getResultList();
return drivingSchoolRepository.findAll();
}
@Transactional
@ -60,26 +53,27 @@ public class DrivingSchoolService {
final DrivingSchool currentDrivingSchool = findDrivingSchool(id);
currentDrivingSchool.setName(name);
return em.merge(currentDrivingSchool);
return drivingSchoolRepository.save(currentDrivingSchool);
}
@Transactional
public DrivingSchool deleteDrivingSchool(Long id) {
final DrivingSchool currentDrivingSchool = findDrivingSchool(id);
em.remove(currentDrivingSchool);
drivingSchoolRepository.delete(currentDrivingSchool);
return currentDrivingSchool;
}
@Transactional
public void deleteAllDrivingSchools() {
em.createQuery("delete from DrivingSchool").executeUpdate();
drivingSchoolRepository.deleteAll();
}
@Transactional
public void addNewStudent(Long id, Student student) {
public Student addNewStudent(Long id, Student student) {
DrivingSchool currentDrivingSchool = findDrivingSchool(id);
currentDrivingSchool.addNewStudent(student);
em.merge(currentDrivingSchool);
drivingSchoolRepository.save(currentDrivingSchool);
return student;
}
@Transactional
@ -87,14 +81,14 @@ public class DrivingSchoolService {
DrivingSchool currentDrivingSchool = findDrivingSchool(id);
for(int i=0; i<students.size(); i++) {
currentDrivingSchool.addNewStudent(students.get(i));
em.merge(currentDrivingSchool);
drivingSchoolRepository.save(currentDrivingSchool);
}
}
@Transactional
public void deleteStudent(Long id, Student student) {
public Student deleteStudent(Long id, Student student) {
DrivingSchool currentDrivingSchool = findDrivingSchool(id);
currentDrivingSchool.deleteStudent(student);
em.merge(student);
return studentRepository.save(student);
}
}

View File

@ -1,11 +1,10 @@
package ru.ulstu.is.cbapp.service;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import ru.ulstu.is.cbapp.dao.StudentRepository;
import ru.ulstu.is.cbapp.models.DrivingSchool;
import ru.ulstu.is.cbapp.models.Student;
import ru.ulstu.is.cbapp.models.Category;
@ -14,8 +13,11 @@ import java.util.List;
@Service
public class StudentService {
@PersistenceContext
private EntityManager em;
private StudentRepository studentRepository;
public StudentService(StudentRepository studentRepository) {
this.studentRepository = studentRepository;
}
@Transactional
public Student addStudent(String surname, String name, String phoneNumber) {
@ -23,13 +25,13 @@ public class StudentService {
throw new IllegalArgumentException("Student's data is null or empty");
}
final Student student = new Student(surname, name, phoneNumber);
em.persist(student);
studentRepository.save(student);
return student;
}
@Transactional
public Student findStudent(Long id) {
final Student student = em.find(Student.class, id);
final Student student = studentRepository.findById(id).orElse(null);
if (student == null) {
throw new EntityNotFoundException(String.format("Student with id [%s] is not found", id));
}
@ -38,8 +40,7 @@ public class StudentService {
@Transactional(readOnly = true)
public List<Student> findAllStudents() {
return em.createQuery("select e from Student e", Student.class)
.getResultList();
return studentRepository.findAll();
}
@Transactional
@ -52,49 +53,48 @@ public class StudentService {
currentStudent.setSurname(surname);
currentStudent.setPhoneNumber(phoneNumber);
return em.merge(currentStudent);
return studentRepository.save(currentStudent);
}
@Transactional
public Student deleteStudent(Long id) {
final Student student = findStudent(id);
em.remove(student);
studentRepository.delete(student);
return student;
}
@Transactional
public void deleteAllStudents() {
em.createQuery("delete from Student").executeUpdate();
studentRepository.deleteAll();
}
@Transactional
public void addDrivingSchool(Long id, DrivingSchool d) {
final Student student = findStudent(id);
student.setDrivingSchool(d);
em.merge(student);
studentRepository.save(student);
}
@Transactional
public void deleteDrivingSchool(Long id) {
final Student student = findStudent(id);
student.deleteDrivingSchool();
em.merge(student);
studentRepository.save(student);
}
@Transactional
public void addCategory(Long id, Category p) {
public Student addCategory(Long id, Category p) {
Student e = findStudent(id);
e.addNewCategory(p);
System.out.println(e.getCategories().size());
em.merge(e);
return studentRepository.save(e);
}
@Transactional
public void deleteCategory(Long id, Category p) {
public Student deleteCategory(Long id, Category p) {
Student e = findStudent(id);
e.removeCategory(p);
System.out.println("Количество должностей после удаления: " + e.getCategories().size());
em.merge(e);
em.flush();
return studentRepository.save(e);
}
}