< Summary

Class:SVETA.Api.Controllers.PromoOfferJournalController
Assembly:SVETA.Api
File(s):/opt/dev/sveta_api_build/SVETA.Api/Controllers/PromoOfferJournalController.cs
Covered lines:0
Uncovered lines:47
Coverable lines:47
Total lines:124
Line coverage:0% (0 of 47)
Covered branches:0
Total branches:18
Branch coverage:0% (0 of 18)

Metrics

MethodLine coverage Branch coverage
.ctor(...)0%100%
GetPromoOffers()0%0%
GetPromoOffer()0%0%
PostPromoOffer()0%0%
DeletePromoOffer()0%100%

File(s)

/opt/dev/sveta_api_build/SVETA.Api/Controllers/PromoOfferJournalController.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Threading.Tasks;
 5using Microsoft.AspNetCore.Mvc;
 6using Microsoft.Extensions.Logging;
 7using WinSolutions.Sveta.Server.Data.DataModel.Entities;
 8using WinSolutions.Sveta.Server.Services.Interfaces;
 9using DTO = SVETA.Api.Data.DTO.PromoOfferJournal;
 10using Swashbuckle.AspNetCore.Annotations;
 11using SVETA.Api.Data.DTO;
 12using SVETA.Api.Data.DTO.PromoOfferJournal;
 13
 14namespace SVETA.Api.Controllers
 15{
 16    /// <summary>
 17    /// Контроллер для журнала промо (пункт "Журнал промо" в тз)
 18    /// </summary>
 19    [Route("api/v1/PromoOfferJournal")]
 20    [ApiController]
 21    public class PromoOfferJournalController : SvetaController
 22    {
 23        const string _routeUrl = "api/v1/PromoOfferJournal";
 24        readonly IPromoOfferService _promoOfferService;
 25        readonly IGoodService _goodService;
 26        readonly IDepartmentService _departmentService;
 27        readonly ILogger<PromoOfferJournalController> _logger;
 28
 029        public PromoOfferJournalController(IPromoOfferService promoOfferService, IGoodService goodService, IDepartmentSe
 030        {
 031            _promoOfferService = promoOfferService;
 032            _goodService = goodService;
 033            _departmentService = departmentService;
 034            _logger = logger;
 035        }
 36
 37        /// <summary>
 38        /// Возвращает список промо
 39        /// </summary>
 40        /// <returns>List of PromoOffers</returns>
 41        [HttpGet()]
 42        [SwaggerResponse(200, "Успешно", typeof(IEnumerable<DTO.PromoOfferOutput>))]
 43        [SwaggerResponse(500, "Ошибка на стороне сервера", typeof(ErrorDTO))]
 44        public async Task<IActionResult> GetPromoOffers(int page = 1, int limit = 10)
 045        {
 046            page = page < 1 ? 1 : page;
 047            limit = limit < 1 ? 10 : limit;
 048            var result = await _promoOfferService.GetPromoOffers(page - 1, limit);
 49
 050            var totalCount = (await _promoOfferService.GetPromoOffers(0, null))?.Count();
 051            var totalFiltredCount = (await _promoOfferService.GetPromoOffers(0, null))?.Count();
 52
 053            var response = new BaseResponseDTO<PromoOfferOutput>(_routeUrl, page, limit, totalFiltredCount ?? 0, totalCo
 054            {
 055                Data = result.Select(x => new DTO.PromoOfferOutput(x)).ToList()
 056            };
 057            return Ok(response);
 058        }
 59
 60        /// <summary>
 61        /// Возаращает промо по его Id
 62        /// </summary>
 63        /// <param name="id">Id промо</param>
 64        /// <returns>Single PromoOffer</returns>
 65        [HttpGet("{id}")]
 66        [SwaggerResponse(200, "Успешно", typeof(DTO.PromoOfferDetailedOutput))]
 67        [SwaggerResponse(404, "Нет записей", typeof(ErrorDTO))]
 68        [SwaggerResponse(500, "Ошибка на стороне сервера", typeof(ErrorDTO))]
 69        public async Task<IActionResult> GetPromoOffer(long id)
 070        {
 071            var result = await _promoOfferService.GetPromoOfferDetailed(id);
 072            if(result == null)
 073            {
 074                return NotFound();
 75            }
 076            return Ok(new DTO.PromoOfferDetailedOutput(result));
 077        }
 78
 79        /// <summary>
 80        /// Создает промо
 81        /// </summary>
 82        /// <param name="data"></param>
 83        [HttpPost()]
 84        [SwaggerResponse(200, "Успешно", typeof(DTO.PromoOfferDetailedOutput))]
 85        [SwaggerResponse(404, "Нет записей", typeof(ErrorDTO))]
 86        [SwaggerResponse(400, "Некорректные параметры", typeof(ErrorDTO))]
 87        [SwaggerResponse(500, "Ошибка на стороне сервера", typeof(ErrorDTO))]
 88        public async Task<IActionResult> PostPromoOffer([FromBody] DTO.PromoOfferInput data)
 089        {
 090            if (!ModelState.IsValid)
 091            {
 092                return BadRequestResult("");
 93            }
 94
 095            var promo = new PromoOffer()
 096            {
 097                DateBegin = data.DateBegin,
 098                DateEnd = data.DateEnd,
 099                Good = await _goodService.GetGood(data.GoodId) ?? throw new ArgumentException("Good not found"),
 0100                MaxQuantity = data.MaxQuantity,
 0101                MinQuantity = data.MinQuantity,
 0102                Price = data.Price,
 0103                SupplierDepartment = await _departmentService.GetDepartment(data.DepartmentId) ?? throw new ArgumentExce
 0104            };
 0105            await _promoOfferService.Create(promo);
 106
 0107            return Ok(new DTO.PromoOfferDetailedOutput(promo));
 0108        }
 109
 110        /// <summary>
 111        /// Удаляет промо
 112        /// </summary>
 113        /// <param name="id">Id промо</param>
 114        [HttpDelete("{id}")]
 115        [SwaggerResponse(200, "Успешно", typeof(EmptyResult))]
 116        [SwaggerResponse(404, "Нет записей", typeof(ErrorDTO))]
 117        [SwaggerResponse(500, "Ошибка на стороне сервера", typeof(ErrorDTO))]
 118        public async Task<IActionResult> DeletePromoOffer(long id)
 0119        {
 0120            await _promoOfferService.Delete(id);
 0121            return Ok();
 0122        }
 123    }
 124}