Поправил создание кафедрального файла

This commit is contained in:
ProstoDenya01
2026-03-31 13:20:31 +03:00
parent 2be2534a1e
commit cd6cc6f5f7
8 changed files with 147 additions and 21 deletions

View File

@@ -4,6 +4,7 @@ import com.magistr.app.dto.CreateScheduleDataRequest;
import com.magistr.app.dto.ScheduleResponse;
import com.magistr.app.model.*;
import com.magistr.app.repository.*;
import com.magistr.app.utils.SemesterTypeValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
@@ -147,8 +148,91 @@ public class ScheduleDataController {
// Доделать проверки получаемых полей!!!
@PostMapping("/create")
public ResponseEntity<?> createScheduleData(@RequestBody CreateScheduleDataRequest request) {
logger.info("Получен запрос на создание записи данных для расписаний");
logger.info("Получен запрос на создание записи данных для расписаний: departmentId={}, semester={}, groupId={}, subjectsId={}, lessonTypeId={}, numberOfHours={}, division={}, teacherId={}, semesterType={}, period={}",
request.getDepartmentId(), request.getSemester(), request.getGroupId(), request.getSubjectsId(), request.getLessonTypeId(), request.getNumberOfHours(), request.getDivision(), request.getTeacherId(), request.getSemesterType(), request.getPeriod());
try {
if (request.getDepartmentId() == null || request.getDepartmentId() == 0) {
String errorMessage = "ID кафедры обязателен";
logger.info("Ошибка валидации: {}", errorMessage);
return ResponseEntity.badRequest().body(Map.of("message", errorMessage));
} else if(!scheduleDataRepository.existsById(request.getDepartmentId())) {
String errorMessage = "Кафедра не найдена";
logger.info("Кафедра не найдена");
return ResponseEntity.badRequest().body(Map.of("message", errorMessage));
}
if (request.getSemester() == null || request.getSemester() == 0) {
String errorMessage = "Семестр обязателен";
logger.info("Ошибка валидации: {}", errorMessage);
return ResponseEntity.badRequest().body(Map.of("message", errorMessage));
} else if(request.getSemester() > 12) {
String errorMessage = "Семестр должен быть меньше или равен 12";
logger.info("Семестр должен быть меньше или равен 12");
return ResponseEntity.badRequest().body(Map.of("message", errorMessage));
}
if (request.getGroupId() == null || request.getGroupId() == 0) {
String errorMessage = "ID группы обязателен";
logger.info("Ошибка валидации: {}", errorMessage);
return ResponseEntity.badRequest().body(Map.of("message", errorMessage));
}
if (request.getSubjectsId() == null || request.getSubjectsId() == 0) {
String errorMessage = "ID дисциплины обязателен";
logger.info("Ошибка валидации: {}", errorMessage);
return ResponseEntity.badRequest().body(Map.of("message", errorMessage));
}
if (request.getLessonTypeId() == null || request.getLessonTypeId() == 0) {
String errorMessage = "ID типа занятия обязателен";
logger.info("Ошибка валидации: {}", errorMessage);
return ResponseEntity.badRequest().body(Map.of("message", errorMessage));
}
if (request.getNumberOfHours() == null) {
request.setNumberOfHours(0L);
}
if (request.getTeacherId() == null || request.getTeacherId() == 0) {
String errorMessage = "ID преподавателя обязателен";
logger.info("Ошибка валидации: {}", errorMessage);
return ResponseEntity.badRequest().body(Map.of("message", errorMessage));
}
if (request.getSemesterType() == null) {
String errorMessage = "Семестр обязателен";
logger.info("Ошибка валидации: {}", errorMessage);
return ResponseEntity.badRequest().body(Map.of("message", errorMessage));
} else if (!SemesterTypeValidator.isValidTypeSemester(request.getSemesterType().toString())) {
String errorMessage = "Некорректный формат семестра. Допустимые форматы: " + SemesterTypeValidator.getValidTypes();
logger.info("Ошибка валидации: {}", errorMessage);
return ResponseEntity.badRequest().body(Map.of("message", errorMessage));
}
if (request.getPeriod() == null || request.getPeriod().isBlank()) {
String errorMessage = "Период обязателен";
logger.info("Ошибка валидации: {}", errorMessage);
return ResponseEntity.badRequest().body(Map.of("message", errorMessage));
}
boolean existsRecord = scheduleDataRepository.existsByDepartmentIdAndSemesterAndGroupIdAndSubjectsIdAndLessonTypeIdAndNumberOfHoursAndDivisionAndTeacherIdAndSemesterTypeAndPeriod(
request.getDepartmentId(),
request.getSemester(),
request.getGroupId(),
request.getSubjectsId(),
request.getLessonTypeId(),
request.getNumberOfHours(),
request.getDivision(),
request.getTeacherId(),
request.getSemesterType(),
request.getPeriod()
);
if(existsRecord) {
return ResponseEntity.status(HttpStatus.CONFLICT)
.body(Map.of("message", "Такая запись уже существует"));
}
ScheduleData scheduleData = new ScheduleData();
scheduleData.setDepartmentId(request.getDepartmentId());
scheduleData.setSemester(request.getSemester());
@@ -179,6 +263,9 @@ public class ScheduleDataController {
logger.info("Запись успешно создана с ID: {}", savedSchedule.getId());
return ResponseEntity.ok(response);
} catch (org.springframework.dao.DataIntegrityViolationException e) {
return ResponseEntity.status(HttpStatus.CONFLICT)
.body(Map.of("message", "Такая запись уже существует"));
} catch (Exception e) {
logger.error("Ошибка при создании записи: {}", e.getMessage(), e);

View File

@@ -10,7 +10,7 @@ public class CreateScheduleDataRequest {
private Long subjectsId;
private Long lessonTypeId;
private Long numberOfHours;
private Boolean isDivision;
private Boolean division;
private Long teacherId;
private SemesterType semesterType;
private String period;
@@ -72,11 +72,11 @@ public class CreateScheduleDataRequest {
}
public Boolean getDivision() {
return isDivision;
return division;
}
public void setDivision(Boolean division) {
isDivision = division;
this.division = division;
}
public Long getTeacherId() {

View File

@@ -17,14 +17,14 @@ public class ScheduleResponse {
private Long lessonTypeId;
private String lessonType;
private Long numberOfHours;
private Boolean isDivision;
private Boolean division;
private Long teacherId;
private String teacherName;
private String teacherJobTitle;
private SemesterType semesterType;
private String period;
public ScheduleResponse(Long id, Long departmentId, Long semester, Long groupId, Long subjectsId, Long lessonTypeId, String lessonType, Long numberOfHours, Boolean isDivision, Long teacherId, SemesterType semesterType, String period) {
public ScheduleResponse(Long id, Long departmentId, Long semester, Long groupId, Long subjectsId, Long lessonTypeId, String lessonType, Long numberOfHours, Boolean division, Long teacherId, SemesterType semesterType, String period) {
this.id = id;
this.departmentId = departmentId;
this.semester = semester;
@@ -32,13 +32,13 @@ public class ScheduleResponse {
this.subjectsId = subjectsId;
this.lessonTypeId = lessonTypeId;
this.numberOfHours = numberOfHours;
this.isDivision = isDivision;
this.division = division;
this.teacherId = teacherId;
this.semesterType = semesterType;
this.period = period;
}
public ScheduleResponse(Long id, Long departmentId, String specialityCode, Long semester, String groupName, Integer groupCourse, String subjectName, String lessonType, Long numberOfHours, Boolean isDivision, String teacherName, String teacherJobTitle, SemesterType semesterType, String period) {
public ScheduleResponse(Long id, Long departmentId, String specialityCode, Long semester, String groupName, Integer groupCourse, String subjectName, String lessonType, Long numberOfHours, Boolean division, String teacherName, String teacherJobTitle, SemesterType semesterType, String period) {
this.id = id;
this.departmentId = departmentId;
this.specialityCode = specialityCode;
@@ -48,7 +48,7 @@ public class ScheduleResponse {
this.subjectName = subjectName;
this.lessonType = lessonType;
this.numberOfHours = numberOfHours;
this.isDivision = isDivision;
this.division = division;
this.teacherName = teacherName;
this.teacherJobTitle = teacherJobTitle;
this.semesterType = semesterType;
@@ -104,7 +104,7 @@ public class ScheduleResponse {
}
public Boolean getDivision() {
return isDivision;
return division;
}
public Long getTeacherId() {

View File

@@ -29,7 +29,7 @@ public class ScheduleData {
private Long numberOfHours;
@Column(name="is_division", nullable = false)
private Boolean isDivision;
private Boolean division;
@Column(name="teacher_id", nullable = false)
private Long teacherId;
@@ -43,7 +43,7 @@ public class ScheduleData {
public ScheduleData() {}
public ScheduleData(Long id, Long departmentId, Long semester, Long groupId, Long subjectsId, Long lessonTypeId, Long numberOfHours, Boolean isDivision, Long teacherId, SemesterType semesterType, String period) {
public ScheduleData(Long id, Long departmentId, Long semester, Long groupId, Long subjectsId, Long lessonTypeId, Long numberOfHours, Boolean division, Long teacherId, SemesterType semesterType, String period) {
this.id = id;
this.departmentId = departmentId;
this.semester = semester;
@@ -51,7 +51,7 @@ public class ScheduleData {
this.subjectsId = subjectsId;
this.lessonTypeId = lessonTypeId;
this.numberOfHours = numberOfHours;
this.isDivision = isDivision;
this.division = division;
this.teacherId = teacherId;
this.semesterType = semesterType;
this.period = period;
@@ -114,11 +114,11 @@ public class ScheduleData {
}
public Boolean getDivision() {
return isDivision;
return division;
}
public void setDivision(Boolean division) {
isDivision = division;
this.division = division;
}
public Long getTeacherId() {

View File

@@ -9,4 +9,17 @@ import java.util.List;
public interface ScheduleDataRepository extends JpaRepository<ScheduleData, Long> {
List<ScheduleData> findByDepartmentIdAndSemesterTypeAndPeriod(Long departmentId, SemesterType semesterType, String period);
boolean existsByDepartmentIdAndSemesterAndGroupIdAndSubjectsIdAndLessonTypeIdAndNumberOfHoursAndDivisionAndTeacherIdAndSemesterTypeAndPeriod(
Long departmentId,
Long semester,
Long groupId,
Long subjectsId,
Long lessonTypeId,
Long numberOfHours,
Boolean division,
Long teacherId,
SemesterType semesterType,
String period
);
}

View File

@@ -0,0 +1,26 @@
package com.magistr.app.utils;
import com.magistr.app.model.SemesterType;
import java.util.Arrays;
public class SemesterTypeValidator {
public static boolean isValidTypeSemester(String semesterType) {
if (semesterType == null) {
return false;
}
try {
SemesterType.valueOf(semesterType);
return true;
} catch (IllegalArgumentException e) {
return false;
}
}
public static String getValidTypes() {
return String.join(", ", Arrays.stream(SemesterType.values())
.map(Enum::name)
.toArray(String[]::new));
}
}