feat: дисциплины формы правила — по календарному графику выбранной группы
В конструкторе правил список дисциплин больше не общий: сначала выбирается группа, по её привязке находится календарный график на учебный год семестра версии, и в списке остаются только дисциплины графика за этот семестр. - эндпоинт GET /api/admin/schedule-rules/subject-options (groupId, semesterId): график по student_group_calendar_assignments, номер семестра группы — из года начала обучения и типа семестра; предупреждения на русском при отсутствии графика или дисциплин на семестр - форма правила: селект «Группа» над дисциплиной, подсказка «График: … · семестр N», выбранная группа сразу отмечается в «Группах» - редактирование: фильтр по первой группе правила, текущая дисциплина всегда остаётся в списке; выпустившаяся группа добавляется в опции - тесты: 4 backend (9/9 в классе), frontend 244/244; docs: API, BUSINESS_LOGIC, FRONTEND
This commit is contained in:
1 parent
df59e1a924
commit
d285db8f58
11 files changed
+515
-12
No files matched your search
@@ -1,11 +1,23 @@
|
||||
package com.magistr.app.controller;
|
||||
|
||||
import com.magistr.app.config.auth.RequireRoles;
|
||||
import com.magistr.app.dto.RuleSubjectOptionsDto;
|
||||
import com.magistr.app.dto.ScheduleRuleDto;
|
||||
import com.magistr.app.dto.PageResponse;
|
||||
import com.magistr.app.model.AcademicCalendarSubject;
|
||||
import com.magistr.app.model.AcademicYear;
|
||||
import com.magistr.app.model.Role;
|
||||
import com.magistr.app.model.Semester;
|
||||
import com.magistr.app.model.SemesterType;
|
||||
import com.magistr.app.model.StudentGroup;
|
||||
import com.magistr.app.model.StudentGroupCalendarAssignment;
|
||||
import com.magistr.app.repository.AcademicCalendarSubjectRepository;
|
||||
import com.magistr.app.repository.GroupRepository;
|
||||
import com.magistr.app.repository.SemesterRepository;
|
||||
import com.magistr.app.repository.StudentGroupCalendarAssignmentRepository;
|
||||
import com.magistr.app.service.ScheduleRuleConflictException;
|
||||
import com.magistr.app.service.ScheduleRuleService;
|
||||
import com.magistr.app.utils.CourseAndSemesterCalculator;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
@@ -21,6 +33,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/admin/schedule-rules")
|
||||
@@ -28,9 +41,21 @@ import java.util.Map;
|
||||
public class ScheduleRuleAdminController {
|
||||
|
||||
private final ScheduleRuleService scheduleRuleService;
|
||||
private final GroupRepository groupRepository;
|
||||
private final SemesterRepository semesterRepository;
|
||||
private final StudentGroupCalendarAssignmentRepository calendarAssignmentRepository;
|
||||
private final AcademicCalendarSubjectRepository calendarSubjectRepository;
|
||||
|
||||
public ScheduleRuleAdminController(ScheduleRuleService scheduleRuleService) {
|
||||
public ScheduleRuleAdminController(ScheduleRuleService scheduleRuleService,
|
||||
GroupRepository groupRepository,
|
||||
SemesterRepository semesterRepository,
|
||||
StudentGroupCalendarAssignmentRepository calendarAssignmentRepository,
|
||||
AcademicCalendarSubjectRepository calendarSubjectRepository) {
|
||||
this.scheduleRuleService = scheduleRuleService;
|
||||
this.groupRepository = groupRepository;
|
||||
this.semesterRepository = semesterRepository;
|
||||
this.calendarAssignmentRepository = calendarAssignmentRepository;
|
||||
this.calendarSubjectRepository = calendarSubjectRepository;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@@ -54,6 +79,77 @@ public class ScheduleRuleAdminController {
|
||||
return ResponseEntity.ok(scheduleRuleService.getById(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Дисциплины для формы правила: сужаются до календарного графика выбранной
|
||||
* группы и семестра версии расписания. Год и семестр задаются версией
|
||||
* выше по вкладке, график ищется по привязке группы из раздела «Группы»,
|
||||
* номер семестра группы — по году начала обучения и типу семестра.
|
||||
*/
|
||||
@GetMapping("/subject-options")
|
||||
public ResponseEntity<RuleSubjectOptionsDto> getSubjectOptions(@RequestParam Long groupId,
|
||||
@RequestParam Long semesterId) {
|
||||
StudentGroup group = groupRepository.findById(groupId)
|
||||
.orElseThrow(() -> new NoSuchElementException("Группа не найдена"));
|
||||
Semester semester = semesterRepository.findById(semesterId)
|
||||
.orElseThrow(() -> new NoSuchElementException("Семестр не найден"));
|
||||
AcademicYear academicYear = semester.getAcademicYear();
|
||||
|
||||
StudentGroupCalendarAssignment assignment = calendarAssignmentRepository
|
||||
.findForSchedule(group.getId(), academicYear.getId())
|
||||
.orElse(null);
|
||||
if (assignment == null) {
|
||||
return ResponseEntity.ok(new RuleSubjectOptionsDto(
|
||||
groupId,
|
||||
semesterId,
|
||||
null,
|
||||
null,
|
||||
"У группы нет календарного графика на этот учебный год",
|
||||
List.of()
|
||||
));
|
||||
}
|
||||
|
||||
int course = CourseAndSemesterCalculator.getActualCourse(
|
||||
group.getYearStartStudy(),
|
||||
academicYear.getStartDate()
|
||||
);
|
||||
if (course < 1) {
|
||||
return ResponseEntity.ok(new RuleSubjectOptionsDto(
|
||||
groupId,
|
||||
semesterId,
|
||||
null,
|
||||
assignment.getAcademicCalendar().getTitle(),
|
||||
"Не удалось определить курс группы на этот учебный год",
|
||||
List.of()
|
||||
));
|
||||
}
|
||||
int semesterNumber = semester.getSemesterType() == SemesterType.autumn
|
||||
? course * 2 - 1
|
||||
: course * 2;
|
||||
|
||||
List<AcademicCalendarSubject> calendarSubjects = calendarSubjectRepository
|
||||
.findByCalendarIdAndSemesterNumberWithSubject(
|
||||
assignment.getAcademicCalendar().getId(),
|
||||
semesterNumber
|
||||
);
|
||||
String warning = calendarSubjects.isEmpty()
|
||||
? "В календарном графике нет дисциплин на " + semesterNumber + "-й семестр"
|
||||
: null;
|
||||
return ResponseEntity.ok(new RuleSubjectOptionsDto(
|
||||
groupId,
|
||||
semesterId,
|
||||
semesterNumber,
|
||||
assignment.getAcademicCalendar().getTitle(),
|
||||
warning,
|
||||
calendarSubjects.stream()
|
||||
.map(calendarSubject -> new RuleSubjectOptionsDto.SubjectOptionDto(
|
||||
calendarSubject.getSubject().getId(),
|
||||
calendarSubject.getSubject().getName(),
|
||||
calendarSubject.getSubject().getCode()
|
||||
))
|
||||
.toList()
|
||||
));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<?> create(@RequestBody ScheduleRuleDto request) {
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.magistr.app.dto;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Дисциплины для формы правила расписания: сужаются до календарного графика,
|
||||
* к которому привязана выбранная группа, и семестра выбранной версии расписания.
|
||||
*/
|
||||
public record RuleSubjectOptionsDto(
|
||||
Long groupId,
|
||||
Long semesterId,
|
||||
Integer semesterNumber,
|
||||
String calendarTitle,
|
||||
String warning,
|
||||
List<SubjectOptionDto> subjects
|
||||
) {
|
||||
|
||||
public record SubjectOptionDto(
|
||||
Long id,
|
||||
String name,
|
||||
String code
|
||||
) {
|
||||
}
|
||||
}
|
||||
+14
@@ -55,6 +55,20 @@ public interface AcademicCalendarSubjectRepository extends JpaRepository<Academi
|
||||
""")
|
||||
List<AcademicCalendarSubject> findBySubjectDepartmentId(@Param("departmentId") Long departmentId);
|
||||
|
||||
// Дисциплины одного семестра календарного графика — для конструктора правил:
|
||||
// список дисциплин формы сужается до графика, к которому привязана группа.
|
||||
@Query("""
|
||||
select calendarSubject
|
||||
from AcademicCalendarSubject calendarSubject
|
||||
join fetch calendarSubject.subject subject
|
||||
where calendarSubject.academicCalendar.id = :calendarId
|
||||
and calendarSubject.semesterNumber = :semesterNumber
|
||||
order by lower(subject.name) asc
|
||||
""")
|
||||
List<AcademicCalendarSubject> findByCalendarIdAndSemesterNumberWithSubject(
|
||||
@Param("calendarId") Long calendarId,
|
||||
@Param("semesterNumber") Integer semesterNumber);
|
||||
|
||||
void deleteByAcademicCalendar_Id(Long calendarId);
|
||||
|
||||
boolean existsByAcademicCalendarIdAndSemesterNumberGreaterThan(Long calendarId, Integer semesterNumber);
|
||||
|
||||
+145
-1
@@ -1,6 +1,18 @@
|
||||
package com.magistr.app.controller;
|
||||
|
||||
import com.magistr.app.dto.ScheduleRuleDto;
|
||||
import com.magistr.app.model.AcademicCalendar;
|
||||
import com.magistr.app.model.AcademicCalendarSubject;
|
||||
import com.magistr.app.model.AcademicYear;
|
||||
import com.magistr.app.model.Semester;
|
||||
import com.magistr.app.model.SemesterType;
|
||||
import com.magistr.app.model.StudentGroup;
|
||||
import com.magistr.app.model.StudentGroupCalendarAssignment;
|
||||
import com.magistr.app.model.Subject;
|
||||
import com.magistr.app.repository.AcademicCalendarSubjectRepository;
|
||||
import com.magistr.app.repository.GroupRepository;
|
||||
import com.magistr.app.repository.SemesterRepository;
|
||||
import com.magistr.app.repository.StudentGroupCalendarAssignmentRepository;
|
||||
import com.magistr.app.service.ScheduleRuleConflictException;
|
||||
import com.magistr.app.service.ScheduleRuleService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -8,9 +20,12 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -23,12 +38,26 @@ import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standal
|
||||
class ScheduleRuleAdminControllerTest {
|
||||
|
||||
private ScheduleRuleService service;
|
||||
private GroupRepository groupRepository;
|
||||
private SemesterRepository semesterRepository;
|
||||
private StudentGroupCalendarAssignmentRepository calendarAssignmentRepository;
|
||||
private AcademicCalendarSubjectRepository calendarSubjectRepository;
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = mock(ScheduleRuleService.class);
|
||||
mockMvc = standaloneSetup(new ScheduleRuleAdminController(service))
|
||||
groupRepository = mock(GroupRepository.class);
|
||||
semesterRepository = mock(SemesterRepository.class);
|
||||
calendarAssignmentRepository = mock(StudentGroupCalendarAssignmentRepository.class);
|
||||
calendarSubjectRepository = mock(AcademicCalendarSubjectRepository.class);
|
||||
mockMvc = standaloneSetup(new ScheduleRuleAdminController(
|
||||
service,
|
||||
groupRepository,
|
||||
semesterRepository,
|
||||
calendarAssignmentRepository,
|
||||
calendarSubjectRepository
|
||||
))
|
||||
.setControllerAdvice(new GlobalExceptionHandler())
|
||||
.build();
|
||||
}
|
||||
@@ -116,6 +145,121 @@ class ScheduleRuleAdminControllerTest {
|
||||
.andExpect(jsonPath("$.message").value("Правило расписания не найдено"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@org.junit.jupiter.api.DisplayName("Дисциплины формы правила сужаются до графика группы и семестра версии")
|
||||
void returnsSubjectOptionsForGroupAndSemester() throws Exception {
|
||||
when(groupRepository.findById(5L)).thenReturn(Optional.of(group(5L, 2023)));
|
||||
when(semesterRepository.findById(11L)).thenReturn(Optional.of(semester(11L, 2L, SemesterType.autumn)));
|
||||
when(calendarAssignmentRepository.findForSchedule(5L, 2L))
|
||||
.thenReturn(Optional.of(assignment(calendar(3L, "ИБ 10.03.01 очно"))));
|
||||
when(calendarSubjectRepository.findByCalendarIdAndSemesterNumberWithSubject(3L, 7))
|
||||
.thenReturn(List.of(calendarSubject(31L, "Криптография", "Б3.Б.01")));
|
||||
|
||||
mockMvc.perform(get("/api/admin/schedule-rules/subject-options")
|
||||
.param("groupId", "5").param("semesterId", "11"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.groupId").value(5))
|
||||
.andExpect(jsonPath("$.semesterId").value(11))
|
||||
.andExpect(jsonPath("$.semesterNumber").value(7))
|
||||
.andExpect(jsonPath("$.calendarTitle").value("ИБ 10.03.01 очно"))
|
||||
.andExpect(jsonPath("$.warning", nullValue()))
|
||||
.andExpect(jsonPath("$.subjects[0].id").value(31))
|
||||
.andExpect(jsonPath("$.subjects[0].name").value("Криптография"))
|
||||
.andExpect(jsonPath("$.subjects[0].code").value("Б3.Б.01"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@org.junit.jupiter.api.DisplayName("Без привязки группы к графику возвращается подсказка и пустой список")
|
||||
void returnsWarningWhenGroupHasNoCalendarAssignment() throws Exception {
|
||||
when(groupRepository.findById(5L)).thenReturn(Optional.of(group(5L, 2023)));
|
||||
when(semesterRepository.findById(11L)).thenReturn(Optional.of(semester(11L, 2L, SemesterType.autumn)));
|
||||
when(calendarAssignmentRepository.findForSchedule(5L, 2L)).thenReturn(Optional.empty());
|
||||
|
||||
mockMvc.perform(get("/api/admin/schedule-rules/subject-options")
|
||||
.param("groupId", "5").param("semesterId", "11"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.semesterNumber", nullValue()))
|
||||
.andExpect(jsonPath("$.calendarTitle", nullValue()))
|
||||
.andExpect(jsonPath("$.warning").value("У группы нет календарного графика на этот учебный год"))
|
||||
.andExpect(jsonPath("$.subjects").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@org.junit.jupiter.api.DisplayName("Пустой семестр графика возвращает подсказку с номером семестра")
|
||||
void returnsWarningWhenCalendarHasNoSubjectsForSemester() throws Exception {
|
||||
when(groupRepository.findById(6L)).thenReturn(Optional.of(group(6L, 2024)));
|
||||
when(semesterRepository.findById(12L)).thenReturn(Optional.of(semester(12L, 2L, SemesterType.spring)));
|
||||
when(calendarAssignmentRepository.findForSchedule(6L, 2L))
|
||||
.thenReturn(Optional.of(assignment(calendar(4L, "ИБ 09.03.01 очно"))));
|
||||
when(calendarSubjectRepository.findByCalendarIdAndSemesterNumberWithSubject(4L, 6))
|
||||
.thenReturn(List.of());
|
||||
|
||||
mockMvc.perform(get("/api/admin/schedule-rules/subject-options")
|
||||
.param("groupId", "6").param("semesterId", "12"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.semesterNumber").value(6))
|
||||
.andExpect(jsonPath("$.calendarTitle").value("ИБ 09.03.01 очно"))
|
||||
.andExpect(jsonPath("$.warning").value("В календарном графике нет дисциплин на 6-й семестр"))
|
||||
.andExpect(jsonPath("$.subjects").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@org.junit.jupiter.api.DisplayName("Несуществующая группа отдаёт 404 с русским сообщением")
|
||||
void returnsNotFoundWhenGroupDoesNotExist() throws Exception {
|
||||
when(groupRepository.findById(404L)).thenReturn(Optional.empty());
|
||||
|
||||
mockMvc.perform(get("/api/admin/schedule-rules/subject-options")
|
||||
.param("groupId", "404").param("semesterId", "11"))
|
||||
.andExpect(status().isNotFound())
|
||||
.andExpect(jsonPath("$.status").value(404))
|
||||
.andExpect(jsonPath("$.error").value("Не найдено"))
|
||||
.andExpect(jsonPath("$.message").value("Группа не найдена"));
|
||||
}
|
||||
|
||||
private StudentGroup group(Long id, Integer yearStartStudy) {
|
||||
StudentGroup group = new StudentGroup();
|
||||
group.setId(id);
|
||||
group.setName("Группа " + id);
|
||||
group.setYearStartStudy(yearStartStudy);
|
||||
return group;
|
||||
}
|
||||
|
||||
private Semester semester(Long id, Long academicYearId, SemesterType semesterType) {
|
||||
AcademicYear academicYear = new AcademicYear();
|
||||
academicYear.setId(academicYearId);
|
||||
academicYear.setTitle("2026/2027");
|
||||
academicYear.setStartDate(LocalDate.of(2026, 9, 1));
|
||||
academicYear.setEndDate(LocalDate.of(2027, 8, 31));
|
||||
Semester semester = new Semester();
|
||||
semester.setId(id);
|
||||
semester.setAcademicYear(academicYear);
|
||||
semester.setSemesterType(semesterType);
|
||||
return semester;
|
||||
}
|
||||
|
||||
private StudentGroupCalendarAssignment assignment(AcademicCalendar calendar) {
|
||||
StudentGroupCalendarAssignment assignment = new StudentGroupCalendarAssignment();
|
||||
assignment.setAcademicCalendar(calendar);
|
||||
return assignment;
|
||||
}
|
||||
|
||||
private AcademicCalendar calendar(Long id, String title) {
|
||||
AcademicCalendar calendar = new AcademicCalendar();
|
||||
calendar.setId(id);
|
||||
calendar.setTitle(title);
|
||||
return calendar;
|
||||
}
|
||||
|
||||
private AcademicCalendarSubject calendarSubject(Long subjectId, String name, String code) {
|
||||
Subject subject = new Subject();
|
||||
subject.setId(subjectId);
|
||||
subject.setName(name);
|
||||
subject.setCode(code);
|
||||
AcademicCalendarSubject calendarSubject = new AcademicCalendarSubject();
|
||||
calendarSubject.setSubject(subject);
|
||||
return calendarSubject;
|
||||
}
|
||||
|
||||
private ScheduleRuleDto dto(Long id) {
|
||||
return new ScheduleRuleDto(
|
||||
id,
|
||||
|
||||
Reference in new issue
Block a user