diff --git a/build.gradle b/build.gradle index 05772a0..0e1f7cf 100644 --- a/build.gradle +++ b/build.gradle @@ -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' } diff --git a/src/main/java/ru/ulstu/is/cbapp/controller/CategoryController.java b/src/main/java/ru/ulstu/is/cbapp/controller/CategoryController.java new file mode 100644 index 0000000..367119b --- /dev/null +++ b/src/main/java/ru/ulstu/is/cbapp/controller/CategoryController.java @@ -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 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)); + } +} diff --git a/src/main/java/ru/ulstu/is/cbapp/controller/DrivingSchoolController.java b/src/main/java/ru/ulstu/is/cbapp/controller/DrivingSchoolController.java new file mode 100644 index 0000000..5851926 --- /dev/null +++ b/src/main/java/ru/ulstu/is/cbapp/controller/DrivingSchoolController.java @@ -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 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)); + } +} diff --git a/src/main/java/ru/ulstu/is/cbapp/controller/StudentController.java b/src/main/java/ru/ulstu/is/cbapp/controller/StudentController.java new file mode 100644 index 0000000..0ec11f6 --- /dev/null +++ b/src/main/java/ru/ulstu/is/cbapp/controller/StudentController.java @@ -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 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)); + } +} diff --git a/src/main/java/ru/ulstu/is/cbapp/dao/CategoryRepository.java b/src/main/java/ru/ulstu/is/cbapp/dao/CategoryRepository.java new file mode 100644 index 0000000..5d3174d --- /dev/null +++ b/src/main/java/ru/ulstu/is/cbapp/dao/CategoryRepository.java @@ -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 { + Optional findById(Long id); +} diff --git a/src/main/java/ru/ulstu/is/cbapp/dao/DrivingSchoolRepository.java b/src/main/java/ru/ulstu/is/cbapp/dao/DrivingSchoolRepository.java new file mode 100644 index 0000000..fea5291 --- /dev/null +++ b/src/main/java/ru/ulstu/is/cbapp/dao/DrivingSchoolRepository.java @@ -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 { + Optional findById(Long id); +} diff --git a/src/main/java/ru/ulstu/is/cbapp/dao/StudentRepository.java b/src/main/java/ru/ulstu/is/cbapp/dao/StudentRepository.java new file mode 100644 index 0000000..a51acf4 --- /dev/null +++ b/src/main/java/ru/ulstu/is/cbapp/dao/StudentRepository.java @@ -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 { + Optional findById(Long id); +} diff --git a/src/main/java/ru/ulstu/is/cbapp/dto/CategoryDto.java b/src/main/java/ru/ulstu/is/cbapp/dto/CategoryDto.java new file mode 100644 index 0000000..1741193 --- /dev/null +++ b/src/main/java/ru/ulstu/is/cbapp/dto/CategoryDto.java @@ -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; + } +} diff --git a/src/main/java/ru/ulstu/is/cbapp/dto/DrivingSchoolDto.java b/src/main/java/ru/ulstu/is/cbapp/dto/DrivingSchoolDto.java new file mode 100644 index 0000000..d0cf490 --- /dev/null +++ b/src/main/java/ru/ulstu/is/cbapp/dto/DrivingSchoolDto.java @@ -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 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 getStudents() { + return students; + } +} diff --git a/src/main/java/ru/ulstu/is/cbapp/dto/StudentDto.java b/src/main/java/ru/ulstu/is/cbapp/dto/StudentDto.java new file mode 100644 index 0000000..66da05f --- /dev/null +++ b/src/main/java/ru/ulstu/is/cbapp/dto/StudentDto.java @@ -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 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 getCategories() { + return categories; + } +} diff --git a/src/main/java/ru/ulstu/is/cbapp/models/Category.java b/src/main/java/ru/ulstu/is/cbapp/models/Category.java index 3c29a1b..bd9a274 100644 --- a/src/main/java/ru/ulstu/is/cbapp/models/Category.java +++ b/src/main/java/ru/ulstu/is/cbapp/models/Category.java @@ -11,7 +11,6 @@ public class Category { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long Id; - private String name; //mappedBy - атрибут, указывающий, что классом-владельцем отношений является другой класс diff --git a/src/main/java/ru/ulstu/is/cbapp/service/CategoryService.java b/src/main/java/ru/ulstu/is/cbapp/service/CategoryService.java index 60aa45a..65a23af 100644 --- a/src/main/java/ru/ulstu/is/cbapp/service/CategoryService.java +++ b/src/main/java/ru/ulstu/is/cbapp/service/CategoryService.java @@ -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 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(); } diff --git a/src/main/java/ru/ulstu/is/cbapp/service/DrivingSchoolService.java b/src/main/java/ru/ulstu/is/cbapp/service/DrivingSchoolService.java index 998f050..ad8fba0 100644 --- a/src/main/java/ru/ulstu/is/cbapp/service/DrivingSchoolService.java +++ b/src/main/java/ru/ulstu/is/cbapp/service/DrivingSchoolService.java @@ -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 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 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); } + }