diff --git a/backend/src/main/java/com/magistr/app/controller/GroupController.java b/backend/src/main/java/com/magistr/app/controller/GroupController.java index 0365aea..183ed51 100755 --- a/backend/src/main/java/com/magistr/app/controller/GroupController.java +++ b/backend/src/main/java/com/magistr/app/controller/GroupController.java @@ -6,6 +6,9 @@ import com.magistr.app.model.EducationForm; import com.magistr.app.model.StudentGroup; import com.magistr.app.repository.EducationFormRepository; import com.magistr.app.repository.GroupRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -17,6 +20,8 @@ import java.util.Optional; @RequestMapping("/api/groups") public class GroupController { + private static final Logger logger = LoggerFactory.getLogger(GroupController.class); + private final GroupRepository groupRepository; private final EducationFormRepository educationFormRepository; @@ -28,56 +33,105 @@ public class GroupController { @GetMapping public List getAllGroups() { - return groupRepository.findAll().stream() - .map(g -> new GroupResponse( + logger.info("Получен запрос на получение всех групп"); + + try { + List groups = groupRepository.findAll(); + + List response = groups.stream() + .map(g -> new GroupResponse( g.getId(), g.getName(), g.getGroupSize(), g.getEducationForm().getId(), - g.getEducationForm().getName())) - .toList(); + g.getEducationForm().getName(), + g.getDepartmentId(), + g.getCourse() + )) + .toList(); + logger.info("Получено {} групп", response.size()); + return response; + } catch (Exception e) { + logger.error("Ошибка при получении списка групп: {}", e.getMessage(), e); + throw e; + } } @PostMapping public ResponseEntity createGroup(@RequestBody CreateGroupRequest request) { - if (request.getName() == null || request.getName().isBlank()) { - return ResponseEntity.badRequest().body(Map.of("message", "Название группы обязательно")); - } - if (groupRepository.findByName(request.getName().trim()).isPresent()) { - return ResponseEntity.badRequest().body(Map.of("message", "Группа с таким названием уже существует")); - } - if (request.getGroupSize() == null) { - return ResponseEntity.badRequest().body(Map.of("message", "Численность группы обязательна")); - } - if (request.getEducationFormId() == null) { - return ResponseEntity.badRequest().body(Map.of("message", "Форма обучения обязательна")); - } + logger.info("Получен запрос на создание новой группы: name = {}, groupSize = {}, educationFormId = {}, departmentId = {}, course = {}", + request.getName(), request.getGroupSize(), request.getEducationFormId(), request.getDepartmentId(), request.getCourse()); + try { + if (request.getName() == null || request.getName().isBlank()) { + String errorMessage = "Название группы обязательно"; + logger.error("Ошибка валидации: {}", errorMessage); + return ResponseEntity.badRequest().body(Map.of("message", errorMessage)); + } + if (groupRepository.findByName(request.getName().trim()).isPresent()) { + String errorMessage = "Группа с таким названием уже существует"; + logger.error("Ошибка валидации: {}", errorMessage); + return ResponseEntity.badRequest().body(Map.of("message", errorMessage)); + } + if (request.getGroupSize() == null) { + String errorMessage = "Численность группы обязательна"; + logger.error("Ошибка валидации: {}", errorMessage); + return ResponseEntity.badRequest().body(Map.of("message", errorMessage)); + } + if (request.getEducationFormId() == null) { + String errorMessage = "Форма обучения обязательна"; + logger.error("Ошибка валидации: {}", errorMessage); + return ResponseEntity.badRequest().body(Map.of("message", errorMessage)); + } + if (request.getDepartmentId() == null || request.getDepartmentId() == 0) { + String errorMessage = "ID кафедры обязателен"; + logger.error("Ошибка валидации: {}", errorMessage); + return ResponseEntity.badRequest().body(Map.of("message", errorMessage)); + } + if (request.getCourse() == null || request.getCourse() == 0) { + String errorMessage = "Курс обязателен"; + logger.error("Ошибка валидации: {}", errorMessage); + return ResponseEntity.badRequest().body(Map.of("message", errorMessage)); + } - Optional efOpt = educationFormRepository.findById(request.getEducationFormId()); - if (efOpt.isEmpty()) { - return ResponseEntity.badRequest().body(Map.of("message", "Форма обучения не найдена")); + Optional efOpt = educationFormRepository.findById(request.getEducationFormId()); + if (efOpt.isEmpty()) { + return ResponseEntity.badRequest().body(Map.of("message", "Форма обучения не найдена")); + } + + StudentGroup group = new StudentGroup(); + group.setName(request.getName().trim()); + group.setGroupSize(request.getGroupSize()); + group.setEducationForm(efOpt.get()); + group.setDepartmentId(request.getDepartmentId()); + group.setCourse(request.getCourse()); + groupRepository.save(group); + + logger.info("Группа успешно создана с ID - {}", group.getId()); + + return ResponseEntity.ok(new GroupResponse( + group.getId(), + group.getName(), + group.getGroupSize(), + group.getEducationForm().getId(), + group.getEducationForm().getName(), + group.getDepartmentId(), + group.getCourse())); + } catch (Exception e ) { + logger.error("Ошибка при создании группы: {}", e.getMessage(), e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body(Map.of("message", "Произошла ошибка при создании группы: " + e.getMessage())); } - - StudentGroup group = new StudentGroup(); - group.setName(request.getName().trim()); - group.setGroupSize(request.getGroupSize()); - group.setEducationForm(efOpt.get()); - groupRepository.save(group); - - return ResponseEntity.ok(new GroupResponse( - group.getId(), - group.getName(), - group.getGroupSize(), - group.getEducationForm().getId(), - group.getEducationForm().getName())); } @DeleteMapping("/{id}") public ResponseEntity deleteGroup(@PathVariable Long id) { + logger.info("Получен запрос на удаление группы с ID - {}", id); if (!groupRepository.existsById(id)) { + logger.info("Группа с ID - {} не найдена", id); return ResponseEntity.notFound().build(); } groupRepository.deleteById(id); + logger.info("Группа с ID - {} успешно удалена", id); return ResponseEntity.ok(Map.of("message", "Группа удалена")); } } diff --git a/backend/src/main/java/com/magistr/app/dto/CreateGroupRequest.java b/backend/src/main/java/com/magistr/app/dto/CreateGroupRequest.java index dfbe5a0..5007602 100755 --- a/backend/src/main/java/com/magistr/app/dto/CreateGroupRequest.java +++ b/backend/src/main/java/com/magistr/app/dto/CreateGroupRequest.java @@ -5,6 +5,8 @@ public class CreateGroupRequest { private String name; private Long groupSize; private Long educationFormId; + private Long departmentId; + private Integer course; public String getName() { return name; @@ -29,4 +31,20 @@ public class CreateGroupRequest { public void setEducationFormId(Long educationFormId) { this.educationFormId = educationFormId; } + + public Long getDepartmentId() { + return departmentId; + } + + public void setDepartmentId(Long departmentId) { + this.departmentId = departmentId; + } + + public Integer getCourse() { + return course; + } + + public void setCourse(Integer course) { + this.course = course; + } } diff --git a/backend/src/main/java/com/magistr/app/dto/GroupResponse.java b/backend/src/main/java/com/magistr/app/dto/GroupResponse.java index 7dd7c58..9d0216b 100755 --- a/backend/src/main/java/com/magistr/app/dto/GroupResponse.java +++ b/backend/src/main/java/com/magistr/app/dto/GroupResponse.java @@ -7,13 +7,17 @@ public class GroupResponse { private Long groupSize; private Long educationFormId; private String educationFormName; + private Long departmentId; + private Integer course; - public GroupResponse(Long id, String name, Long groupSize, Long educationFormId, String educationFormName) { + public GroupResponse(Long id, String name, Long groupSize, Long educationFormId, String educationFormName, Long departmentId, Integer course) { this.id = id; this.name = name; this.groupSize = groupSize; this.educationFormId = educationFormId; this.educationFormName = educationFormName; + this.departmentId = departmentId; + this.course = course; } public Long getId() { @@ -35,4 +39,12 @@ public class GroupResponse { public String getEducationFormName() { return educationFormName; } + + public Long getDepartmentId() { + return departmentId; + } + + public Integer getCourse() { + return course; + } } diff --git a/backend/src/main/java/com/magistr/app/model/StudentGroup.java b/backend/src/main/java/com/magistr/app/model/StudentGroup.java index 9deee86..ed1ca2b 100755 --- a/backend/src/main/java/com/magistr/app/model/StudentGroup.java +++ b/backend/src/main/java/com/magistr/app/model/StudentGroup.java @@ -20,6 +20,12 @@ public class StudentGroup { @JoinColumn(name = "education_form_id", nullable = false) private EducationForm educationForm; + @Column(name = "department_id", nullable = false) + private Long departmentId; + + @Column(name = "course", nullable = false) + private Integer course; + public StudentGroup() { } @@ -54,4 +60,20 @@ public class StudentGroup { public void setEducationForm(EducationForm educationForm) { this.educationForm = educationForm; } + + public Long getDepartmentId() { + return departmentId; + } + + public void setDepartmentId(Long departmentId) { + this.departmentId = departmentId; + } + + public Integer getCourse() { + return course; + } + + public void setCourse(Integer course) { + this.course = course; + } }