feat: add lesson type controller

This commit is contained in:
2025-02-01 17:08:00 +03:00
parent 5bcb7bfbc1
commit 03b6560bc4
7 changed files with 136 additions and 1 deletions

View File

@ -3,7 +3,7 @@
namespace Mirea.Api.Dto.Responses;
/// <summary>
/// Represents basic information about a faculty.
/// Represents information about a faculty.
/// </summary>
public class FacultyResponse
{

View File

@ -0,0 +1,21 @@
using System.ComponentModel.DataAnnotations;
namespace Mirea.Api.Dto.Responses;
/// <summary>
/// Represents information about a lesson type.
/// </summary>
public class LessonTypeResponse
{
/// <summary>
/// Gets or sets the unique identifier of the lesson type.
/// </summary>
[Required]
public int Id { get; set; }
/// <summary>
/// Gets or sets the name of the lesson type.
/// </summary>
[Required]
public required string Name { get; set; }
}

View File

@ -0,0 +1,43 @@
using Asp.Versioning;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Mirea.Api.DataAccess.Application.Cqrs.TypeOfOccupation.Queries.GetTypeOfOccupationList;
using Mirea.Api.Dto.Responses;
using Mirea.Api.Endpoint.Common.Attributes;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace Mirea.Api.Endpoint.Controllers.V1;
[ApiVersion("1.0")]
[CacheMaxAge(true)]
public class LessonTypeController(IMediator mediator) : BaseController
{
/// <summary>
/// Gets a paginated list of type of occupation.
/// </summary>
/// <param name="page">Page number. Start from 0.</param>
/// <param name="pageSize">Number of items per page.</param>
/// <returns>Paginated list of type of occupation.</returns>
[HttpGet]
[BadRequestResponse]
public async Task<ActionResult<List<LessonTypeResponse>>> Get([FromQuery][Range(0, int.MaxValue)] int? page,
[FromQuery][Range(1, int.MaxValue)] int? pageSize)
{
var result = await mediator.Send(new GetTypeOfOccupationListQuery()
{
Page = page,
PageSize = pageSize
});
return Ok(result.TypeOfOccupations
.Select(f => new LessonTypeResponse()
{
Id = f.Id,
Name = f.Name
})
);
}
}

View File

@ -0,0 +1,9 @@
using MediatR;
namespace Mirea.Api.DataAccess.Application.Cqrs.TypeOfOccupation.Queries.GetTypeOfOccupationList;
public class GetTypeOfOccupationListQuery : IRequest<TypeOfOccupationListVm>
{
public int? Page { get; set; }
public int? PageSize { get; set; }
}

View File

@ -0,0 +1,31 @@
using MediatR;
using Microsoft.EntityFrameworkCore;
using Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Mirea.Api.DataAccess.Application.Cqrs.TypeOfOccupation.Queries.GetTypeOfOccupationList;
public class GetTypeOfOccupationListQueryHandler(ITypeOfOccupationDbContext dbContext) : IRequestHandler<GetTypeOfOccupationListQuery, TypeOfOccupationListVm>
{
public async Task<TypeOfOccupationListVm> Handle(GetTypeOfOccupationListQuery request, CancellationToken cancellationToken)
{
var dtos = dbContext.TypeOfOccupations.OrderBy(t => t.Id)
.Select(t => new TypeOfOccupationLookupDto()
{
Id = t.Id,
Name = t.ShortName
});
if (request is { PageSize: not null, Page: not null })
dtos = dtos.Skip(request.Page.Value * request.PageSize.Value).Take(request.PageSize.Value);
var result = await dtos.ToListAsync(cancellationToken);
return new TypeOfOccupationListVm
{
TypeOfOccupations = result
};
}
}

View File

@ -0,0 +1,14 @@
using System.Collections.Generic;
namespace Mirea.Api.DataAccess.Application.Cqrs.TypeOfOccupation.Queries.GetTypeOfOccupationList;
/// <summary>
/// Represents a view model containing multiple type of occupations.
/// </summary>
public class TypeOfOccupationListVm
{
/// <summary>
/// The list of type of occupations.
/// </summary>
public IEnumerable<TypeOfOccupationLookupDto> TypeOfOccupations { get; set; } = [];
}

View File

@ -0,0 +1,17 @@
namespace Mirea.Api.DataAccess.Application.Cqrs.TypeOfOccupation.Queries.GetTypeOfOccupationList;
/// <summary>
/// Represents type of occupations.
/// </summary>
public class TypeOfOccupationLookupDto
{
/// <summary>
/// The unique identifier for the occupation.
/// </summary>
public int Id { get; set; }
/// <summary>
/// The name of the occupation.
/// </summary>
public required string Name { get; set; }
}