MireaBackend/Endpoint/Controllers/V1/ScheduleController.cs

199 lines
8.2 KiB
C#
Raw Normal View History

2024-02-19 11:20:49 +03:00
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
2024-02-19 11:20:49 +03:00
using Mirea.Api.DataAccess.Application.Cqrs.Schedule.Queries.GetScheduleList;
using Mirea.Api.Dto.Common;
2024-02-19 11:20:49 +03:00
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;
2024-02-19 11:20:49 +03:00
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Mirea.Api.Endpoint.Controllers.V1;
2024-05-28 07:09:40 +03:00
[ApiVersion("1.0")]
public class ScheduleController(IMediator mediator, IOptionsSnapshot<GeneralConfig> config) : BaseController
2024-02-19 11:20:49 +03:00
{
[HttpGet("StartTerm")]
public ActionResult<DateOnly> GetStartTerm() => config.Value.ScheduleSettings!.StartTerm;
[HttpGet("PairPeriod")]
public ActionResult<Dictionary<int, PairPeriodTime>> GetPairPeriod() => config.Value.ScheduleSettings!.PairPeriod.ConvertToDto();
2024-02-19 11:20:49 +03:00
/// <summary>
/// Retrieves schedules based on various filters.
/// </summary>
/// <param name="request">The request object containing filter criteria.</param>
/// <returns>A list of schedules matching the filter criteria.</returns>
[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[BadRequestResponse]
[NotFoundResponse]
public async Task<ActionResult<List<ScheduleResponse>>> Get([FromBody] ScheduleRequest request)
{
2024-05-19 12:30:17 +03:00
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))
2024-02-19 11:20:49 +03:00
{
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
2024-02-19 11:20:49 +03:00
{
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
2024-02-19 11:20:49 +03:00
{
DayOfWeek = s.DayOfWeek,
PairNumber = s.PairNumber,
IsEven = s.IsEven,
Discipline = s.Discipline,
DisciplineId = s.DisciplineId,
2024-05-19 13:53:25 +03:00
IsExcludedWeeks = s.IsExcludedWeeks,
Weeks = s.Weeks,
2024-05-19 12:30:17 +03:00
TypeOfOccupations = s.TypeOfOccupations,
2024-02-19 11:20:49 +03:00
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
}));
}
/// <summary>
/// Retrieves schedules for a specific group based on various filters.
/// </summary>
/// <param name="id">The ID of the group.</param>
/// <param name="isEven">A value indicating whether to retrieve schedules for even weeks.</param>
/// <param name="disciplines">An array of discipline IDs.</param>
/// <param name="professors">An array of professor IDs.</param>
/// <param name="lectureHalls">An array of lecture hall IDs.</param>
/// <returns>A response containing schedules for the specified group.</returns>
2024-05-28 07:09:40 +03:00
[HttpGet("GetByGroup/{id:int}")]
2024-02-19 11:20:49 +03:00
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[BadRequestResponse]
[NotFoundResponse]
public async Task<ActionResult<List<ScheduleResponse>>> GetByGroup(int id,
2024-02-19 11:20:49 +03:00
[FromQuery] bool? isEven = null,
[FromQuery] int[]? disciplines = null,
[FromQuery] int[]? professors = null,
[FromQuery] int[]? lectureHalls = null) =>
await Get(new ScheduleRequest
2024-02-19 11:20:49 +03:00
{
Disciplines = disciplines,
2024-02-19 11:20:49 +03:00
IsEven = isEven,
Groups = [id],
Professors = professors,
LectureHalls = lectureHalls
2024-02-19 11:20:49 +03:00
});
/// <summary>
/// Retrieves schedules for a specific professor based on various filters.
/// </summary>
/// <param name="id">The ID of the professor.</param>
/// <param name="isEven">A value indicating whether to retrieve schedules for even weeks.</param>
/// <param name="disciplines">An array of discipline IDs.</param>
/// <param name="groups">An array of group IDs.</param>
/// <param name="lectureHalls">An array of lecture hall IDs.</param>
/// <returns>A response containing schedules for the specified professor.</returns>
2024-05-28 07:09:40 +03:00
[HttpGet("GetByProfessor/{id:int}")]
2024-02-19 11:20:49 +03:00
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[BadRequestResponse]
[NotFoundResponse]
public async Task<ActionResult<List<ScheduleResponse>>> GetByProfessor(int id,
2024-02-19 11:20:49 +03:00
[FromQuery] bool? isEven = null,
[FromQuery] int[]? disciplines = null,
[FromQuery] int[]? groups = null,
[FromQuery] int[]? lectureHalls = null) =>
await Get(new ScheduleRequest
2024-02-19 11:20:49 +03:00
{
Disciplines = disciplines,
2024-02-19 11:20:49 +03:00
IsEven = isEven,
Groups = groups,
Professors = [id],
LectureHalls = lectureHalls
2024-02-19 11:20:49 +03:00
});
/// <summary>
/// Retrieves schedules for a specific lecture hall based on various filters.
/// </summary>
/// <param name="id">The ID of the lecture hall.</param>
/// <param name="isEven">A value indicating whether to retrieve schedules for even weeks.</param>
/// <param name="disciplines">An array of discipline IDs.</param>
/// <param name="professors">An array of professor IDs.</param>
/// <param name="groups">An array of group IDs.</param>
/// <returns>A response containing schedules for the specified lecture hall.</returns>
2024-05-28 07:09:40 +03:00
[HttpGet("GetByLectureHall/{id:int}")]
2024-02-19 11:20:49 +03:00
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[BadRequestResponse]
[NotFoundResponse]
public async Task<ActionResult<List<ScheduleResponse>>> GetByLectureHall(int id,
2024-02-19 11:20:49 +03:00
[FromQuery] bool? isEven = null,
[FromQuery] int[]? disciplines = null,
[FromQuery] int[]? groups = null,
[FromQuery] int[]? professors = null) =>
await Get(new ScheduleRequest
2024-02-19 11:20:49 +03:00
{
Disciplines = disciplines,
2024-02-19 11:20:49 +03:00
IsEven = isEven,
Groups = groups,
Professors = professors,
LectureHalls = [id]
2024-02-19 11:20:49 +03:00
});
/// <summary>
/// Retrieves schedules for a specific discipline based on various filters.
/// </summary>
/// <param name="id">The ID of the discipline.</param>
/// <param name="isEven">A value indicating whether to retrieve schedules for even weeks.</param>
/// <param name="groups">An array of group IDs.</param>
/// <param name="professors">An array of professor IDs.</param>
/// <param name="lectureHalls">An array of lecture hall IDs.</param>
/// <returns>A response containing schedules for the specified discipline.</returns>
2024-05-28 07:09:40 +03:00
[HttpGet("GetByDiscipline/{id:int}")]
2024-02-19 11:20:49 +03:00
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[BadRequestResponse]
[NotFoundResponse]
public async Task<ActionResult<List<ScheduleResponse>>> GetByDiscipline(int id,
2024-02-19 11:20:49 +03:00
[FromQuery] bool? isEven = null,
[FromQuery] int[]? groups = null,
[FromQuery] int[]? professors = null,
[FromQuery] int[]? lectureHalls = null) =>
await Get(new ScheduleRequest
2024-02-19 11:20:49 +03:00
{
Disciplines = [id],
2024-02-19 11:20:49 +03:00
IsEven = isEven,
Groups = groups,
Professors = professors,
LectureHalls = lectureHalls
2024-02-19 11:20:49 +03:00
});
}