2024-09-18 06:00:07 +03:00
|
|
|
|
using Asp.Versioning;
|
|
|
|
|
using MediatR;
|
2024-02-16 23:57:13 +03:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Mirea.Api.DataAccess.Application.Cqrs.Group.Queries.GetGroupDetails;
|
|
|
|
|
using Mirea.Api.DataAccess.Application.Cqrs.Group.Queries.GetGroupList;
|
|
|
|
|
using Mirea.Api.Dto.Responses;
|
|
|
|
|
using Mirea.Api.Endpoint.Common.Attributes;
|
2024-02-17 01:49:37 +03:00
|
|
|
|
using System;
|
2024-02-16 23:57:13 +03:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2024-05-28 07:09:40 +03:00
|
|
|
|
namespace Mirea.Api.Endpoint.Controllers.V1;
|
|
|
|
|
|
|
|
|
|
[ApiVersion("1.0")]
|
2024-08-24 02:27:05 +03:00
|
|
|
|
[CacheMaxAge(true)]
|
2024-05-28 07:09:40 +03:00
|
|
|
|
public class GroupController(IMediator mediator) : BaseController
|
2024-02-16 23:57:13 +03:00
|
|
|
|
{
|
2024-05-28 07:09:40 +03:00
|
|
|
|
private static int GetCourseNumber(string groupName)
|
2024-02-16 23:57:13 +03:00
|
|
|
|
{
|
2024-05-28 07:09:40 +03:00
|
|
|
|
var current = DateTime.Now;
|
|
|
|
|
if (!int.TryParse(groupName[2..], out var yearOfGroup)
|
|
|
|
|
&& !int.TryParse(groupName.Split('-')[^1][..2], out yearOfGroup))
|
|
|
|
|
return -1;
|
2024-02-17 01:49:37 +03:00
|
|
|
|
|
2024-05-28 07:09:40 +03:00
|
|
|
|
// Convert a two-digit year to a four-digit one
|
|
|
|
|
yearOfGroup += current.Year / 100 * 100;
|
2024-02-17 01:49:37 +03:00
|
|
|
|
|
2024-08-27 21:35:35 +03:00
|
|
|
|
return current.Year - yearOfGroup + (current.Month < 8 ? 0 : 1);
|
2024-05-28 07:09:40 +03:00
|
|
|
|
}
|
2024-02-17 01:49:37 +03:00
|
|
|
|
|
2024-05-28 07:09:40 +03:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieves a list of groups.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="page">The page number for pagination (optional).</param>
|
|
|
|
|
/// <param name="pageSize">The page size for pagination (optional).</param>
|
|
|
|
|
/// <returns>A list of groups.</returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[BadRequestResponse]
|
|
|
|
|
public async Task<ActionResult<List<GroupResponse>>> Get([FromQuery] int? page, [FromQuery] int? pageSize)
|
|
|
|
|
{
|
|
|
|
|
var result = await mediator.Send(new GetGroupListQuery()
|
2024-02-16 23:57:13 +03:00
|
|
|
|
{
|
2024-05-28 07:09:40 +03:00
|
|
|
|
Page = page,
|
|
|
|
|
PageSize = pageSize
|
|
|
|
|
});
|
2024-02-16 23:57:13 +03:00
|
|
|
|
|
2024-05-28 07:09:40 +03:00
|
|
|
|
return Ok(result.Groups
|
|
|
|
|
.Select(g => new GroupResponse()
|
2024-02-16 23:57:13 +03:00
|
|
|
|
{
|
2024-05-28 07:09:40 +03:00
|
|
|
|
Id = g.Id,
|
|
|
|
|
Name = g.Name,
|
|
|
|
|
FacultyId = g.FacultyId,
|
|
|
|
|
CourseNumber = GetCourseNumber(g.Name)
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-02-16 23:57:13 +03:00
|
|
|
|
|
2024-05-28 07:09:40 +03:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieves detailed information about a specific group.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id">The ID of the group to retrieve.</param>
|
|
|
|
|
/// <returns>Detailed information about the group.</returns>
|
|
|
|
|
[HttpGet("{id:int}")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[BadRequestResponse]
|
|
|
|
|
[NotFoundResponse]
|
|
|
|
|
public async Task<ActionResult<GroupDetailsResponse>> GetDetails(int id)
|
|
|
|
|
{
|
|
|
|
|
var result = await mediator.Send(new GetGroupInfoQuery()
|
|
|
|
|
{
|
|
|
|
|
Id = id
|
|
|
|
|
});
|
2024-02-19 12:02:37 +03:00
|
|
|
|
|
2024-05-28 07:09:40 +03:00
|
|
|
|
return Ok(new GroupDetailsResponse()
|
2024-02-19 12:02:37 +03:00
|
|
|
|
{
|
2024-05-28 07:09:40 +03:00
|
|
|
|
Id = result.Id,
|
|
|
|
|
Name = result.Name,
|
|
|
|
|
FacultyId = result.FacultyId,
|
|
|
|
|
FacultyName = result.Faculty,
|
|
|
|
|
CourseNumber = GetCourseNumber(result.Name)
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-02-19 12:02:37 +03:00
|
|
|
|
|
2024-05-28 07:09:40 +03:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieves a list of groups by faculty ID.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id">The ID of the faculty.</param>
|
|
|
|
|
/// <returns>A list of groups belonging to the specified faculty.</returns>
|
|
|
|
|
[HttpGet("GetByFaculty/{id:int}")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[BadRequestResponse]
|
|
|
|
|
[NotFoundResponse]
|
|
|
|
|
public async Task<ActionResult<List<GroupResponse>>> GetByFaculty(int id)
|
|
|
|
|
{
|
|
|
|
|
var result = await mediator.Send(new GetGroupListQuery());
|
|
|
|
|
|
|
|
|
|
return Ok(result.Groups
|
|
|
|
|
.Where(g => g.FacultyId == id)
|
|
|
|
|
.Select(g => new GroupResponse()
|
|
|
|
|
{
|
|
|
|
|
Id = g.Id,
|
|
|
|
|
Name = g.Name,
|
|
|
|
|
CourseNumber = GetCourseNumber(g.Name),
|
|
|
|
|
FacultyId = g.FacultyId
|
|
|
|
|
}));
|
2024-02-16 23:57:13 +03:00
|
|
|
|
}
|
2024-05-28 07:09:40 +03:00
|
|
|
|
}
|