using MediatR; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using Mirea.Api.DataAccess.Application.Cqrs.Schedule.Queries.GetScheduleList; using Mirea.Api.Dto.Common; using Mirea.Api.Dto.Requests; using Mirea.Api.Dto.Responses; using Mirea.Api.Endpoint.Common.Attributes; using Mirea.Api.Endpoint.Common.Services; using Mirea.Api.Endpoint.Configuration.General; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Mirea.Api.Endpoint.Controllers.V1; [ApiVersion("1.0")] public class ScheduleController(IMediator mediator, IOptionsSnapshot config) : BaseController { [HttpGet("StartTerm")] public ActionResult GetStartTerm() => config.Value.ScheduleSettings!.StartTerm; [HttpGet("PairPeriod")] public ActionResult> GetPairPeriod() => config.Value.ScheduleSettings!.PairPeriod.ConvertToDto(); /// /// Retrieves schedules based on various filters. /// /// The request object containing filter criteria. /// A list of schedules matching the filter criteria. [HttpPost] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] [BadRequestResponse] [NotFoundResponse] public async Task>> Get([FromBody] ScheduleRequest request) { if ((request.Groups == null || request.Groups.Length == 0) && (request.Disciplines == null || request.Disciplines.Length == 0) && (request.Professors == null || request.Professors.Length == 0) && (request.LectureHalls == null || request.LectureHalls.Length == 0)) { return BadRequest(new ErrorResponse() { Error = "At least one of the arguments must be selected." + (request.IsEven.HasValue ? $" \"{nameof(request.IsEven)}\" is not a strong argument" : string.Empty), Code = StatusCodes.Status400BadRequest }); } var result = (await mediator.Send(new GetScheduleListQuery { IsEven = request.IsEven, DisciplineIds = request.Disciplines, GroupIds = request.Groups, LectureHallIds = request.LectureHalls, ProfessorIds = request.Professors })).Schedules; if (result.Count == 0) NoContent(); return Ok(result.Select(s => new ScheduleResponse { DayOfWeek = s.DayOfWeek, PairNumber = s.PairNumber, IsEven = s.IsEven, Discipline = s.Discipline, DisciplineId = s.DisciplineId, IsExcludedWeeks = s.IsExcludedWeeks, Weeks = s.Weeks, TypeOfOccupations = s.TypeOfOccupations, Group = s.Group, GroupId = s.GroupId, LectureHalls = s.LectureHalls, LectureHallsId = s.LectureHallsId, Professors = s.Professors, ProfessorsId = s.ProfessorsId, Campus = s.Campus, CampusId = s.CampusId, LinkToMeet = s.LinkToMeet })); } /// /// Retrieves schedules for a specific group based on various filters. /// /// The ID of the group. /// A value indicating whether to retrieve schedules for even weeks. /// An array of discipline IDs. /// An array of professor IDs. /// An array of lecture hall IDs. /// A response containing schedules for the specified group. [HttpGet("GetByGroup/{id:int}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] [BadRequestResponse] [NotFoundResponse] public async Task>> GetByGroup(int id, [FromQuery] bool? isEven = null, [FromQuery] int[]? disciplines = null, [FromQuery] int[]? professors = null, [FromQuery] int[]? lectureHalls = null) => await Get(new ScheduleRequest { Disciplines = disciplines, IsEven = isEven, Groups = [id], Professors = professors, LectureHalls = lectureHalls }); /// /// Retrieves schedules for a specific professor based on various filters. /// /// The ID of the professor. /// A value indicating whether to retrieve schedules for even weeks. /// An array of discipline IDs. /// An array of group IDs. /// An array of lecture hall IDs. /// A response containing schedules for the specified professor. [HttpGet("GetByProfessor/{id:int}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] [BadRequestResponse] [NotFoundResponse] public async Task>> GetByProfessor(int id, [FromQuery] bool? isEven = null, [FromQuery] int[]? disciplines = null, [FromQuery] int[]? groups = null, [FromQuery] int[]? lectureHalls = null) => await Get(new ScheduleRequest { Disciplines = disciplines, IsEven = isEven, Groups = groups, Professors = [id], LectureHalls = lectureHalls }); /// /// Retrieves schedules for a specific lecture hall based on various filters. /// /// The ID of the lecture hall. /// A value indicating whether to retrieve schedules for even weeks. /// An array of discipline IDs. /// An array of professor IDs. /// An array of group IDs. /// A response containing schedules for the specified lecture hall. [HttpGet("GetByLectureHall/{id:int}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] [BadRequestResponse] [NotFoundResponse] public async Task>> GetByLectureHall(int id, [FromQuery] bool? isEven = null, [FromQuery] int[]? disciplines = null, [FromQuery] int[]? groups = null, [FromQuery] int[]? professors = null) => await Get(new ScheduleRequest { Disciplines = disciplines, IsEven = isEven, Groups = groups, Professors = professors, LectureHalls = [id] }); /// /// Retrieves schedules for a specific discipline based on various filters. /// /// The ID of the discipline. /// A value indicating whether to retrieve schedules for even weeks. /// An array of group IDs. /// An array of professor IDs. /// An array of lecture hall IDs. /// A response containing schedules for the specified discipline. [HttpGet("GetByDiscipline/{id:int}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] [BadRequestResponse] [NotFoundResponse] public async Task>> GetByDiscipline(int id, [FromQuery] bool? isEven = null, [FromQuery] int[]? groups = null, [FromQuery] int[]? professors = null, [FromQuery] int[]? lectureHalls = null) => await Get(new ScheduleRequest { Disciplines = [id], IsEven = isEven, Groups = groups, Professors = professors, LectureHalls = lectureHalls }); }