MireaBackend/Endpoint/Controllers/V1/ScheduleController.cs
Polianin Nikita 0d3461b769
All checks were successful
.NET Test Pipeline / build-and-test (pull_request) Successful in 1m52s
feat: add specific weeks
2024-05-19 13:53:25 +03:00

312 lines
12 KiB
C#

using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Mirea.Api.DataAccess.Application.Cqrs.Schedule.Queries.GetScheduleList;
using Mirea.Api.Dto.Requests;
using Mirea.Api.Dto.Responses;
using Mirea.Api.Dto.Responses.Schedule;
using Mirea.Api.Endpoint.Common.Attributes;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Mirea.Api.Endpoint.Controllers.V1;
public class ScheduleController(IMediator mediator) : BaseControllerV1
{
/// <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)
{
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
}));
}
/// <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>
[HttpGet("{id:int}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[BadRequestResponse]
[NotFoundResponse]
public async Task<ActionResult<GroupScheduleResponse>> GetByGroup(int id,
[FromQuery] bool? isEven = null,
[FromQuery] int[]? disciplines = null,
[FromQuery] int[]? professors = null,
[FromQuery] int[]? lectureHalls = null)
{
var result = (await mediator.Send(new GetScheduleListQuery()
{
IsEven = isEven,
DisciplineIds = disciplines,
GroupIds = [id],
LectureHallIds = lectureHalls,
ProfessorIds = professors
})).Schedules;
if (result.Count == 0) NoContent();
return Ok(new GroupScheduleResponse()
{
Group = result[0].Group,
GroupId = result[0].GroupId,
Schedules = result.Select(g => new GroupScheduleInfo()
{
DayOfWeek = g.DayOfWeek,
PairNumber = g.PairNumber,
IsEven = g.IsEven,
Discipline = g.Discipline,
DisciplineId = g.DisciplineId,
IsExcludedWeeks = g.IsExcludedWeeks,
Weeks = g.Weeks,
TypeOfOccupations = g.TypeOfOccupations,
LectureHalls = g.LectureHalls,
LectureHallsId = g.LectureHallsId,
Professors = g.Professors,
ProfessorsId = g.ProfessorsId,
Campus = g.Campus,
CampusId = g.CampusId,
LinkToMeet = g.LinkToMeet
})
});
}
/// <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>
[HttpGet("{id:int}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[BadRequestResponse]
[NotFoundResponse]
public async Task<ActionResult<ProfessorScheduleResponse>> GetByProfessor(int id,
[FromQuery] bool? isEven = null,
[FromQuery] int[]? disciplines = null,
[FromQuery] int[]? groups = null,
[FromQuery] int[]? lectureHalls = null)
{
var result = (await mediator.Send(new GetScheduleListQuery()
{
IsEven = isEven,
DisciplineIds = disciplines,
GroupIds = groups,
LectureHallIds = lectureHalls,
ProfessorIds = [id]
})).Schedules;
if (result.Count == 0) NoContent();
return Ok(new ProfessorScheduleResponse()
{
Professor = result.Select(professor =>
professor.Professors.FirstOrDefault(x => !string.IsNullOrEmpty(x))
).First()!,
ProfessorId = result.Select(professor =>
professor.ProfessorsId.FirstOrDefault(x => x != null)
).First()!.Value,
Schedules = result.Select(p => new ProfessorScheduleInfo()
{
DayOfWeek = p.DayOfWeek,
PairNumber = p.PairNumber,
IsEven = p.IsEven,
Discipline = p.Discipline,
DisciplineId = p.DisciplineId,
IsExcludedWeeks = p.IsExcludedWeeks,
Weeks = p.Weeks,
TypeOfOccupations = p.TypeOfOccupations,
Group = p.Group,
GroupId = p.GroupId,
LectureHalls = p.LectureHalls,
LectureHallsId = p.LectureHallsId,
Campus = p.Campus,
CampusId = p.CampusId,
LinkToMeet = p.LinkToMeet
})
});
}
/// <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>
[HttpGet("{id:int}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[BadRequestResponse]
[NotFoundResponse]
public async Task<ActionResult<LectureHallScheduleResponse>> GetByLectureHall(int id,
[FromQuery] bool? isEven = null,
[FromQuery] int[]? disciplines = null,
[FromQuery] int[]? groups = null,
[FromQuery] int[]? professors = null)
{
var result = (await mediator.Send(new GetScheduleListQuery()
{
IsEven = isEven,
DisciplineIds = disciplines,
GroupIds = groups,
LectureHallIds = [id],
ProfessorIds = professors
})).Schedules;
if (result.Count == 0) NoContent();
return Ok(new LectureHallScheduleResponse()
{
LectureHalls = result.Select(lectureHall =>
lectureHall.LectureHalls.FirstOrDefault(x => !string.IsNullOrEmpty(x))
).First()!,
LectureHallsId = result.Select(lectureHall =>
lectureHall.LectureHallsId.FirstOrDefault(x => x != null)
).First()!.Value,
Schedules = result.Select(l => new LectureHallScheduleInfo()
{
DayOfWeek = l.DayOfWeek,
PairNumber = l.PairNumber,
IsEven = l.IsEven,
Discipline = l.Discipline,
DisciplineId = l.DisciplineId,
IsExcludedWeeks = l.IsExcludedWeeks,
Weeks = l.Weeks,
TypeOfOccupations = l.TypeOfOccupations,
Group = l.Group,
GroupId = l.GroupId,
Professors = l.Professors,
ProfessorsId = l.ProfessorsId,
Campus = l.Campus,
CampusId = l.CampusId,
LinkToMeet = l.LinkToMeet
})
});
}
/// <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>
[HttpGet("{id:int}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[BadRequestResponse]
[NotFoundResponse]
public async Task<ActionResult<DisciplineScheduleResponse>> GetByDiscipline(int id,
[FromQuery] bool? isEven = null,
[FromQuery] int[]? groups = null,
[FromQuery] int[]? professors = null,
[FromQuery] int[]? lectureHalls = null)
{
var result = (await mediator.Send(new GetScheduleListQuery()
{
IsEven = isEven,
DisciplineIds = [id],
GroupIds = groups,
LectureHallIds = lectureHalls,
ProfessorIds = professors
})).Schedules;
if (result.Count == 0) NoContent();
return Ok(new DisciplineScheduleResponse()
{
Discipline = result[0].Discipline,
DisciplineId = result[0].DisciplineId,
Schedules = result.Select(d => new DisciplineScheduleInfo()
{
DayOfWeek = d.DayOfWeek,
PairNumber = d.PairNumber,
IsEven = d.IsEven,
TypeOfOccupation = d.TypeOfOccupations,
Group = d.Group,
GroupId = d.GroupId,
LectureHalls = d.LectureHalls,
LectureHallsId = d.LectureHallsId,
Professors = d.Professors,
ProfessorsId = d.ProfessorsId,
Campus = d.Campus,
CampusId = d.CampusId,
LinkToMeet = d.LinkToMeet
})
});
}
}