From 03b6560bc404a63b0bec364db448b1c254543383 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Sat, 1 Feb 2025 17:08:00 +0300 Subject: [PATCH] feat: add lesson type controller --- ApiDto/Responses/FacultyResponse.cs | 2 +- ApiDto/Responses/LessonTypeResponse.cs | 21 +++++++++ .../Controllers/V1/LessonTypeController.cs | 43 +++++++++++++++++++ .../GetTypeOfOccupationListQuery.cs | 9 ++++ .../GetTypeOfOccupationListQueryHandler.cs | 31 +++++++++++++ .../TypeOfOccupationListVm.cs | 14 ++++++ .../TypeOfOccupationLookupDto.cs | 17 ++++++++ 7 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 ApiDto/Responses/LessonTypeResponse.cs create mode 100644 Endpoint/Controllers/V1/LessonTypeController.cs create mode 100644 SqlData/Application/Cqrs/TypeOfOccupation/Queries/GetTypeOfOccupationList/GetTypeOfOccupationListQuery.cs create mode 100644 SqlData/Application/Cqrs/TypeOfOccupation/Queries/GetTypeOfOccupationList/GetTypeOfOccupationListQueryHandler.cs create mode 100644 SqlData/Application/Cqrs/TypeOfOccupation/Queries/GetTypeOfOccupationList/TypeOfOccupationListVm.cs create mode 100644 SqlData/Application/Cqrs/TypeOfOccupation/Queries/GetTypeOfOccupationList/TypeOfOccupationLookupDto.cs diff --git a/ApiDto/Responses/FacultyResponse.cs b/ApiDto/Responses/FacultyResponse.cs index 1e172c2..8eafaba 100644 --- a/ApiDto/Responses/FacultyResponse.cs +++ b/ApiDto/Responses/FacultyResponse.cs @@ -3,7 +3,7 @@ namespace Mirea.Api.Dto.Responses; /// -/// Represents basic information about a faculty. +/// Represents information about a faculty. /// public class FacultyResponse { diff --git a/ApiDto/Responses/LessonTypeResponse.cs b/ApiDto/Responses/LessonTypeResponse.cs new file mode 100644 index 0000000..9b3bf2d --- /dev/null +++ b/ApiDto/Responses/LessonTypeResponse.cs @@ -0,0 +1,21 @@ +using System.ComponentModel.DataAnnotations; + +namespace Mirea.Api.Dto.Responses; + +/// +/// Represents information about a lesson type. +/// +public class LessonTypeResponse +{ + /// + /// Gets or sets the unique identifier of the lesson type. + /// + [Required] + public int Id { get; set; } + + /// + /// Gets or sets the name of the lesson type. + /// + [Required] + public required string Name { get; set; } +} \ No newline at end of file diff --git a/Endpoint/Controllers/V1/LessonTypeController.cs b/Endpoint/Controllers/V1/LessonTypeController.cs new file mode 100644 index 0000000..1a9c2e6 --- /dev/null +++ b/Endpoint/Controllers/V1/LessonTypeController.cs @@ -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 +{ + /// + /// Gets a paginated list of type of occupation. + /// + /// Page number. Start from 0. + /// Number of items per page. + /// Paginated list of type of occupation. + [HttpGet] + [BadRequestResponse] + public async Task>> 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 + }) + ); + } +} \ No newline at end of file diff --git a/SqlData/Application/Cqrs/TypeOfOccupation/Queries/GetTypeOfOccupationList/GetTypeOfOccupationListQuery.cs b/SqlData/Application/Cqrs/TypeOfOccupation/Queries/GetTypeOfOccupationList/GetTypeOfOccupationListQuery.cs new file mode 100644 index 0000000..a6dcd96 --- /dev/null +++ b/SqlData/Application/Cqrs/TypeOfOccupation/Queries/GetTypeOfOccupationList/GetTypeOfOccupationListQuery.cs @@ -0,0 +1,9 @@ +using MediatR; + +namespace Mirea.Api.DataAccess.Application.Cqrs.TypeOfOccupation.Queries.GetTypeOfOccupationList; + +public class GetTypeOfOccupationListQuery : IRequest +{ + public int? Page { get; set; } + public int? PageSize { get; set; } +} \ No newline at end of file diff --git a/SqlData/Application/Cqrs/TypeOfOccupation/Queries/GetTypeOfOccupationList/GetTypeOfOccupationListQueryHandler.cs b/SqlData/Application/Cqrs/TypeOfOccupation/Queries/GetTypeOfOccupationList/GetTypeOfOccupationListQueryHandler.cs new file mode 100644 index 0000000..7e8fbc2 --- /dev/null +++ b/SqlData/Application/Cqrs/TypeOfOccupation/Queries/GetTypeOfOccupationList/GetTypeOfOccupationListQueryHandler.cs @@ -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 +{ + public async Task 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 + }; + } +} \ No newline at end of file diff --git a/SqlData/Application/Cqrs/TypeOfOccupation/Queries/GetTypeOfOccupationList/TypeOfOccupationListVm.cs b/SqlData/Application/Cqrs/TypeOfOccupation/Queries/GetTypeOfOccupationList/TypeOfOccupationListVm.cs new file mode 100644 index 0000000..ea2f92e --- /dev/null +++ b/SqlData/Application/Cqrs/TypeOfOccupation/Queries/GetTypeOfOccupationList/TypeOfOccupationListVm.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; + +namespace Mirea.Api.DataAccess.Application.Cqrs.TypeOfOccupation.Queries.GetTypeOfOccupationList; + +/// +/// Represents a view model containing multiple type of occupations. +/// +public class TypeOfOccupationListVm +{ + /// + /// The list of type of occupations. + /// + public IEnumerable TypeOfOccupations { get; set; } = []; +} \ No newline at end of file diff --git a/SqlData/Application/Cqrs/TypeOfOccupation/Queries/GetTypeOfOccupationList/TypeOfOccupationLookupDto.cs b/SqlData/Application/Cqrs/TypeOfOccupation/Queries/GetTypeOfOccupationList/TypeOfOccupationLookupDto.cs new file mode 100644 index 0000000..740826f --- /dev/null +++ b/SqlData/Application/Cqrs/TypeOfOccupation/Queries/GetTypeOfOccupationList/TypeOfOccupationLookupDto.cs @@ -0,0 +1,17 @@ +namespace Mirea.Api.DataAccess.Application.Cqrs.TypeOfOccupation.Queries.GetTypeOfOccupationList; + +/// +/// Represents type of occupations. +/// +public class TypeOfOccupationLookupDto +{ + /// + /// The unique identifier for the occupation. + /// + public int Id { get; set; } + + /// + /// The name of the occupation. + /// + public required string Name { get; set; } +} \ No newline at end of file