From 798d61c7eac839b9b9f1b290eb66e07e9b4da94e Mon Sep 17 00:00:00 2001 From: ProstoDenya01 Date: Wed, 25 Mar 2026 22:41:53 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D1=8C=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=B2=D0=B0=D1=82=D1=8C?= =?UTF-8?q?,=20=D0=BF=D1=80=D0=BE=D1=81=D0=BC=D0=B0=D1=82=D1=80=D0=B8?= =?UTF-8?q?=D0=B2=D0=B0=D1=82=D1=8C=20=D0=B8=20=D1=83=D0=B4=D0=B0=D0=BB?= =?UTF-8?q?=D1=8F=D1=82=D1=8C=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D0=B5=20?= =?UTF-8?q?=D0=B4=D0=B8=D1=81=D1=86=D0=B8=D0=BF=D0=BB=D0=B8=D0=BD=20=D0=B8?= =?UTF-8?q?=20=D0=BA=D0=B0=D1=84=D0=B5=D0=B4=D1=80.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/controller/DepartmentController.java | 106 ++++++++++++++++++ .../app/controller/SpecialityController.java | 106 ++++++++++++++++++ .../app/dto/CreateDepartmentRequest.java | 25 +++++ .../app/dto/CreateSpecialityRequest.java | 25 +++++ .../magistr/app/dto/DepartmentResponse.java | 38 +++++++ .../magistr/app/dto/SpecialityResponse.java | 38 +++++++ .../com/magistr/app/model/Department.java | 50 +++++++++ .../com/magistr/app/model/Speciality.java | 50 +++++++++ .../app/repository/DepartmentRepository.java | 13 +++ .../app/repository/SpecialtiesRepository.java | 13 +++ 10 files changed, 464 insertions(+) create mode 100644 backend/src/main/java/com/magistr/app/controller/DepartmentController.java create mode 100644 backend/src/main/java/com/magistr/app/controller/SpecialityController.java create mode 100644 backend/src/main/java/com/magistr/app/dto/CreateDepartmentRequest.java create mode 100644 backend/src/main/java/com/magistr/app/dto/CreateSpecialityRequest.java create mode 100644 backend/src/main/java/com/magistr/app/dto/DepartmentResponse.java create mode 100644 backend/src/main/java/com/magistr/app/dto/SpecialityResponse.java create mode 100644 backend/src/main/java/com/magistr/app/model/Department.java create mode 100644 backend/src/main/java/com/magistr/app/model/Speciality.java create mode 100644 backend/src/main/java/com/magistr/app/repository/DepartmentRepository.java create mode 100644 backend/src/main/java/com/magistr/app/repository/SpecialtiesRepository.java diff --git a/backend/src/main/java/com/magistr/app/controller/DepartmentController.java b/backend/src/main/java/com/magistr/app/controller/DepartmentController.java new file mode 100644 index 0000000..8adfc3b --- /dev/null +++ b/backend/src/main/java/com/magistr/app/controller/DepartmentController.java @@ -0,0 +1,106 @@ +package com.magistr.app.controller; + +import com.magistr.app.dto.CreateDepartmentRequest; +import com.magistr.app.dto.DepartmentResponse; +import com.magistr.app.model.Department; +import com.magistr.app.repository.DepartmentRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Map; + +@RestController +@RequestMapping("/api/departments") +public class DepartmentController { + + private static final Logger logger = LoggerFactory.getLogger(DepartmentController.class); + + private final DepartmentRepository departmentRepository; + + public DepartmentController(DepartmentRepository departmentRepository) { + this.departmentRepository = departmentRepository; + } + + @GetMapping + public List getAllDepartments() { + logger.info("Получен запрос на получение списка кафедр"); + try { + List departments = departmentRepository.findAll(); + List response = departments.stream() + .map( d -> new Department( + d.getId(), + d.getDepartmentName(), + d.getDepartmentCode() + )) + .toList(); + logger.info("Получено {} кафедр", response.size()); + return response; + } catch (Exception e) { + logger.error("Ошибка при получении списка кафедр: {}", e.getMessage(), e); + throw e; + } + } + + @PostMapping + public ResponseEntity createDepartment(@RequestBody CreateDepartmentRequest request) { + logger.info("Получен запрос на создание кафедры: name = {}, code = {}", request.getDepartmentName(), request.getDepartmentCode()); + + try { + if (request.getDepartmentName() == null || request.getDepartmentName().isBlank()){ + String errorMessage = "Название кафедры обязательно"; + logger.error("Ошибка валидации: {}", errorMessage); + return ResponseEntity.badRequest().body(Map.of("message", errorMessage)); + } + if (departmentRepository.findByDepartmentName(request.getDepartmentName().trim()).isPresent()) { + String errorMessage = "Кафедра с таким названием уже существует"; + logger.error("Ошибка валидации: {}", errorMessage); + return ResponseEntity.badRequest().body(Map.of("message", errorMessage)); + } + if (request.getDepartmentCode() == null || request.getDepartmentCode() == 0) { + String errorMessage = "Код кафедры обязателен"; + logger.error("Ошибка валидации: {}", errorMessage); + return ResponseEntity.badRequest().body(Map.of("message", errorMessage)); + } + if (departmentRepository.findByDepartmentCode(request.getDepartmentCode()).isPresent()) { + String errorMessage = "Кафедра с таким кодом уже существует"; + logger.error("Ошибка валидации: {}", errorMessage); + return ResponseEntity.badRequest().body(Map.of("message", errorMessage)); + } + + Department department = new Department(); + department.setDepartmentName(request.getDepartmentName()); + department.setDepartmentCode(request.getDepartmentCode()); + departmentRepository.save(department); + + logger.info("Кафедра успешно создана с ID: {}", department.getId()); + + return ResponseEntity.ok( + new DepartmentResponse( + department.getId(), + department.getDepartmentName(), + department.getDepartmentCode() + ) + ); + } catch (Exception e) { + logger.error("Ошибка при создании кафедры: {}", e.getMessage(), e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body(Map.of("message", "Произошла ошибка при создании кафедры " + e.getMessage())); + } + } + + @DeleteMapping("/id") + public ResponseEntity deleteDepartment(@PathVariable Long id) { + logger.info("Получен запрос на удаление кафедры с ID: {}", id); + if (!departmentRepository.existsById(id)) { + logger.info("Кафедра с ID - {} не найдена", id); + return ResponseEntity.notFound().build(); + } + departmentRepository.deleteById(id); + logger.info("Кафедра с ID - {} успешно удалена", id); + return ResponseEntity.ok(Map.of("message", "Кафедра удалена")); + } +} diff --git a/backend/src/main/java/com/magistr/app/controller/SpecialityController.java b/backend/src/main/java/com/magistr/app/controller/SpecialityController.java new file mode 100644 index 0000000..92db85a --- /dev/null +++ b/backend/src/main/java/com/magistr/app/controller/SpecialityController.java @@ -0,0 +1,106 @@ +package com.magistr.app.controller; + +import com.magistr.app.dto.CreateSpecialityRequest; +import com.magistr.app.dto.SpecialityResponse; +import com.magistr.app.model.Speciality; +import com.magistr.app.repository.SpecialtiesRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Map; + +@RestController +@RequestMapping("/api/specialties") +public class SpecialityController { + + private static final Logger logger = LoggerFactory.getLogger(SpecialityController.class); + + private final SpecialtiesRepository specialtiesRepository; + + public SpecialityController(SpecialtiesRepository specialtiesRepository) { + this.specialtiesRepository = specialtiesRepository; + } + + @GetMapping + public List getAllSpecialties() { + logger.info("Получен запрос на получение списка специальностей"); + try { + List specialities = specialtiesRepository.findAll(); + List response = specialities.stream() + .map( s -> new Speciality( + s.getId(), + s.getSpecialityName(), + s.getSpecialityCode() + )) + .toList(); + logger.info("Получено {} специальностей", response.size()); + return response; + } catch (Exception e) { + logger.error("Ошибка при получении списка специальностей: {}", e.getMessage(), e); + throw e; + } + } + + @PostMapping + public ResponseEntity createSpeciality(@RequestBody CreateSpecialityRequest request) { + logger.info("Получен запрос на создание специальности: name = {}, code = {}", request.getSpecialityName(), request.getSpecialityCode()); + + try { + if (request.getSpecialityName() == null || request.getSpecialityName().isBlank()) { + String errorMessage = "Название специальности обязательно"; + logger.error("Ошибка валидации: {}", errorMessage); + return ResponseEntity.badRequest().body(Map.of("message", errorMessage)); + } + if (specialtiesRepository.findBySpecialityName(request.getSpecialityName().trim()).isPresent()) { + String errorMessage = "Специальность с таким названием уже существует"; + logger.error("Ошибка валидации: {}", errorMessage); + return ResponseEntity.badRequest().body(Map.of("message", errorMessage)); + } + if (request.getSpecialityCode() == null || request.getSpecialityCode().isBlank()) { + String errorMessage = "Код специальности обязателен"; + logger.error("Ошибка валидации: {}", errorMessage); + return ResponseEntity.badRequest().body(Map.of("message", errorMessage)); + } + if (specialtiesRepository.findBySpecialityCode(request.getSpecialityCode().trim()).isPresent()) { + String errorMessage = "Специальность с таким кодом уже существует"; + logger.error("Ошибка валидации: {}", errorMessage); + return ResponseEntity.badRequest().body(Map.of("message", errorMessage)); + } + + Speciality speciality = new Speciality(); + speciality.setSpecialityName(request.getSpecialityName()); + speciality.setSpecialityCode(request.getSpecialityCode()); + specialtiesRepository.save(speciality); + + logger.info("Специальность успешно создана с ID: {}", speciality.getId()); + + return ResponseEntity.ok( + new SpecialityResponse( + speciality.getId(), + speciality.getSpecialityName(), + speciality.getSpecialityCode() + ) + ); + } catch (Exception e) { + logger.error("Ошибка при создании специальности: {}", e.getMessage(), e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body(Map.of("message", "Произошла ошибка при создании специальности " + e.getMessage())); + } + } + + @DeleteMapping("/id") + public ResponseEntity deleteSpeciality(@PathVariable Long id) { + logger.info("Получен запрос на удаление специальности с ID: {}", id); + if (!specialtiesRepository.existsById(id)) { + logger.info("Специальность с ID - {} не найдена", id); + return ResponseEntity.notFound().build(); + } + specialtiesRepository.deleteById(id); + logger.info("Специальность с ID - {} успешно удалена", id); + return ResponseEntity.ok(Map.of("message", "Специальнсть удалена")); + } +} diff --git a/backend/src/main/java/com/magistr/app/dto/CreateDepartmentRequest.java b/backend/src/main/java/com/magistr/app/dto/CreateDepartmentRequest.java new file mode 100644 index 0000000..caa572e --- /dev/null +++ b/backend/src/main/java/com/magistr/app/dto/CreateDepartmentRequest.java @@ -0,0 +1,25 @@ +package com.magistr.app.dto; + +public class CreateDepartmentRequest { + + private String departmentName; + private Long departmentCode; + + public CreateDepartmentRequest() {} + + public String getDepartmentName() { + return departmentName; + } + + public void setDepartmentName(String departmentName) { + this.departmentName = departmentName; + } + + public Long getDepartmentCode() { + return departmentCode; + } + + public void setDepartmentCode(Long departmentCode) { + this.departmentCode = departmentCode; + } +} diff --git a/backend/src/main/java/com/magistr/app/dto/CreateSpecialityRequest.java b/backend/src/main/java/com/magistr/app/dto/CreateSpecialityRequest.java new file mode 100644 index 0000000..9b8aede --- /dev/null +++ b/backend/src/main/java/com/magistr/app/dto/CreateSpecialityRequest.java @@ -0,0 +1,25 @@ +package com.magistr.app.dto; + +public class CreateSpecialityRequest { + + private String specialityName; + private String specialityCode; + + public CreateSpecialityRequest() {} + + public String getSpecialityName() { + return specialityName; + } + + public void setSpecialityName(String specialityName) { + this.specialityName = specialityName; + } + + public String getSpecialityCode() { + return specialityCode; + } + + public void setSpecialityCode(String specialityCode) { + this.specialityCode = specialityCode; + } +} diff --git a/backend/src/main/java/com/magistr/app/dto/DepartmentResponse.java b/backend/src/main/java/com/magistr/app/dto/DepartmentResponse.java new file mode 100644 index 0000000..32df4a7 --- /dev/null +++ b/backend/src/main/java/com/magistr/app/dto/DepartmentResponse.java @@ -0,0 +1,38 @@ +package com.magistr.app.dto; + +public class DepartmentResponse { + + private Long id; + private String departmentName; + private Long departmentCode; + + public DepartmentResponse(Long id, String departmentName, Long departmentCode) { + this.id = id; + this.departmentName = departmentName; + this.departmentCode = departmentCode; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getDepartmentName() { + return departmentName; + } + + public void setDepartmentName(String departmentName) { + this.departmentName = departmentName; + } + + public Long getDepartmentCode() { + return departmentCode; + } + + public void setDepartmentCode(Long departmentCode) { + this.departmentCode = departmentCode; + } +} diff --git a/backend/src/main/java/com/magistr/app/dto/SpecialityResponse.java b/backend/src/main/java/com/magistr/app/dto/SpecialityResponse.java new file mode 100644 index 0000000..49408d2 --- /dev/null +++ b/backend/src/main/java/com/magistr/app/dto/SpecialityResponse.java @@ -0,0 +1,38 @@ +package com.magistr.app.dto; + +public class SpecialityResponse { + + private Long id; + private String specialityName; + private String specialityCode; + + public SpecialityResponse(Long id, String specialityName, String specialityCode) { + this.id = id; + this.specialityName = specialityName; + this.specialityCode = specialityCode; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getSpecialityName() { + return specialityName; + } + + public void setSpecialityName(String specialityName) { + this.specialityName = specialityName; + } + + public String getSpecialityCode() { + return specialityCode; + } + + public void setSpecialityCode(String specialityCode) { + this.specialityCode = specialityCode; + } +} diff --git a/backend/src/main/java/com/magistr/app/model/Department.java b/backend/src/main/java/com/magistr/app/model/Department.java new file mode 100644 index 0000000..d14905e --- /dev/null +++ b/backend/src/main/java/com/magistr/app/model/Department.java @@ -0,0 +1,50 @@ +package com.magistr.app.model; + +import jakarta.persistence.*; + +@Entity +@Table(name="departments") +public class Department { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "name", nullable = false) + private String departmentName; + + @Column(name = "code", nullable = false) + private Long departmentCode; + + public Department() {} + + public Department(Long id, String departmentName, Long departmentCode) { + this.id = id; + this.departmentName = departmentName; + this.departmentCode = departmentCode; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getDepartmentName() { + return departmentName; + } + + public void setDepartmentName(String departmentName) { + this.departmentName = departmentName; + } + + public Long getDepartmentCode() { + return departmentCode; + } + + public void setDepartmentCode(Long departmentCode) { + this.departmentCode = departmentCode; + } +} diff --git a/backend/src/main/java/com/magistr/app/model/Speciality.java b/backend/src/main/java/com/magistr/app/model/Speciality.java new file mode 100644 index 0000000..327360b --- /dev/null +++ b/backend/src/main/java/com/magistr/app/model/Speciality.java @@ -0,0 +1,50 @@ +package com.magistr.app.model; + +import jakarta.persistence.*; + +@Entity +@Table(name="specialties") +public class Speciality { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name="name", nullable = false) + private String specialityName; + + @Column(name="specialty_code",nullable = false) + private String specialityCode; + + public Speciality() {} + + public Speciality(Long id, String specialityName, String specialityCode) { + this.id = id; + this.specialityName = specialityName; + this.specialityCode = specialityCode; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getSpecialityName() { + return specialityName; + } + + public void setSpecialityName(String specialityName) { + this.specialityName = specialityName; + } + + public String getSpecialityCode() { + return specialityCode; + } + + public void setSpecialityCode(String specialityCode) { + this.specialityCode = specialityCode; + } +} diff --git a/backend/src/main/java/com/magistr/app/repository/DepartmentRepository.java b/backend/src/main/java/com/magistr/app/repository/DepartmentRepository.java new file mode 100644 index 0000000..01fc400 --- /dev/null +++ b/backend/src/main/java/com/magistr/app/repository/DepartmentRepository.java @@ -0,0 +1,13 @@ +package com.magistr.app.repository; + +import com.magistr.app.model.Department; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface DepartmentRepository extends JpaRepository { + + Optional findByDepartmentName(String departmentName); + + Optional findByDepartmentCode(Long departmentCode); +} diff --git a/backend/src/main/java/com/magistr/app/repository/SpecialtiesRepository.java b/backend/src/main/java/com/magistr/app/repository/SpecialtiesRepository.java new file mode 100644 index 0000000..7c63217 --- /dev/null +++ b/backend/src/main/java/com/magistr/app/repository/SpecialtiesRepository.java @@ -0,0 +1,13 @@ +package com.magistr.app.repository; + +import com.magistr.app.model.Speciality; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface SpecialtiesRepository extends JpaRepository { + + Optional findBySpecialityName(String specialityName); + + Optional findBySpecialityCode(String specialityCode); +}