137 lines
4.9 KiB
C#
137 lines
4.9 KiB
C#
|
|
using AutoMapper;
|
|||
|
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
|
using Microsoft.EntityFrameworkCore;
|
|||
|
|
using PARR.API.Contracts.V1;
|
|||
|
|
using PARR.API.Contracts.V1.Requests.Queries;
|
|||
|
|
using PARR.API.Contracts.V1.Responses;
|
|||
|
|
using PARR.API.Contracts.V1.Responses.Base;
|
|||
|
|
using PARR.API.Controllers.V1.Base;
|
|||
|
|
using PARR.API.Extensions;
|
|||
|
|
using PARR.DAL.Contracts;
|
|||
|
|
using PARR.DAL.DomainModels;
|
|||
|
|
using PARR.DAL.Models;
|
|||
|
|
using PARR.DAL.Services.Interfaces;
|
|||
|
|
|
|||
|
|
namespace PARR.API.Controllers.V1
|
|||
|
|
{
|
|||
|
|
public class TemplateController : BaseApiController
|
|||
|
|
{
|
|||
|
|
private readonly IMapper mapper;
|
|||
|
|
private readonly ITemplateService templateService;
|
|||
|
|
|
|||
|
|
public TemplateController(
|
|||
|
|
IMapper mapper,
|
|||
|
|
ITemplateService templateService
|
|||
|
|
)
|
|||
|
|
{
|
|||
|
|
this.mapper = mapper;
|
|||
|
|
this.templateService = templateService;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Получить список всех шаблонов постранично
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="paginationQuery"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
[HttpGet(ApiRoutes.Template.GetAll)]
|
|||
|
|
public async Task<IActionResult> GetAll([FromQuery] PaginationQuery paginationQuery, [FromQuery] TemplateQuery filter)
|
|||
|
|
{
|
|||
|
|
var paginationFilter = mapper.Map<PaginationFilter>(paginationQuery);
|
|||
|
|
|
|||
|
|
IQueryable<Template> query = templateService.Get()
|
|||
|
|
.OrderBy(t => t.Name);
|
|||
|
|
|
|||
|
|
if (filter.StatusCode.HasValue)
|
|||
|
|
{
|
|||
|
|
switch (filter.StatusCode.Value)
|
|||
|
|
{
|
|||
|
|
case (int)StatusTemplateEnum.Ok:
|
|||
|
|
query = query.Where(t => t.StatusCode == (int)StatusTemplateEnum.Ok);
|
|||
|
|
break;
|
|||
|
|
case (int)StatusTemplateEnum.Creating:
|
|||
|
|
query = query.Where(t => t.StatusCode == (int)StatusTemplateEnum.Creating);
|
|||
|
|
break;
|
|||
|
|
case (int)StatusTemplateEnum.Updating:
|
|||
|
|
query = query.Where(t => t.StatusCode == (int)StatusTemplateEnum.Updating);
|
|||
|
|
break;
|
|||
|
|
default:
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var templates = await templateService.GetPage(query, paginationFilter).ToListAsync();
|
|||
|
|
|
|||
|
|
if (!templates.Any())
|
|||
|
|
return NoContent();
|
|||
|
|
|
|||
|
|
var templateResponse = mapper.Map<List<TemplateResponse>>(templates);
|
|||
|
|
var paginationResponse = new PagedResponse<TemplateResponse>(templateResponse, true).GetPaginatedProps(paginationFilter, query);
|
|||
|
|
|
|||
|
|
return Ok(paginationResponse);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Получить шаблон по id
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="id"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
[HttpGet(ApiRoutes.Template.Get)]
|
|||
|
|
public async Task<IActionResult> GetById([FromRoute] Guid id)
|
|||
|
|
{
|
|||
|
|
var template = await templateService.GetAsync(id);
|
|||
|
|
|
|||
|
|
if (template == null)
|
|||
|
|
return NotFound();
|
|||
|
|
|
|||
|
|
var response = mapper.Map<TemplateResponse>(template);
|
|||
|
|
|
|||
|
|
return Ok(new Response<TemplateResponse>(response, true));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Получить один шаблон по заданному статусу
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="statusCode"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
[HttpGet(ApiRoutes.Template.GetByStatusCode)]
|
|||
|
|
public async Task<IActionResult> GetByStatus([FromRoute] int statusCode)
|
|||
|
|
{
|
|||
|
|
var template = await templateService.Get().FirstOrDefaultAsync(t => t.StatusCode == statusCode);
|
|||
|
|
|
|||
|
|
if (template == null)
|
|||
|
|
return NotFound();
|
|||
|
|
|
|||
|
|
var response = mapper.Map<TemplateResponse>(template);
|
|||
|
|
|
|||
|
|
return Ok(new Response<TemplateResponse>(response, true));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Установить статус ОК для шаблона с id
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="id"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
[HttpPut(ApiRoutes.Template.SetOkStatus)]
|
|||
|
|
public async Task<IActionResult> SetOkStatus([FromRoute] Guid id)
|
|||
|
|
{
|
|||
|
|
var template = await templateService.GetAsync(id);
|
|||
|
|
|
|||
|
|
if (template == null)
|
|||
|
|
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Не найден шаблон с id: {id}" } }));
|
|||
|
|
|
|||
|
|
template.StatusCode = (int)StatusTemplateEnum.Ok;
|
|||
|
|
|
|||
|
|
if (!await templateService.CommitAsync())
|
|||
|
|
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Ошибка при изменении статуса у шаблона с id: {id}" } }));
|
|||
|
|
|
|||
|
|
var response = mapper.Map<TemplateResponse>(template);
|
|||
|
|
|
|||
|
|
return Ok(new Response<TemplateResponse>(response, true));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|