| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using Microsoft.AspNetCore.Mvc; |
| | | 6 | | using Microsoft.Extensions.Logging; |
| | | 7 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 8 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 9 | | using DTO = SVETA.Api.Data.DTO.PromoOfferJournal; |
| | | 10 | | using Swashbuckle.AspNetCore.Annotations; |
| | | 11 | | using SVETA.Api.Data.DTO; |
| | | 12 | | using SVETA.Api.Data.DTO.PromoOfferJournal; |
| | | 13 | | |
| | | 14 | | namespace 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 | | |
| | 0 | 29 | | public PromoOfferJournalController(IPromoOfferService promoOfferService, IGoodService goodService, IDepartmentSe |
| | 0 | 30 | | { |
| | 0 | 31 | | _promoOfferService = promoOfferService; |
| | 0 | 32 | | _goodService = goodService; |
| | 0 | 33 | | _departmentService = departmentService; |
| | 0 | 34 | | _logger = logger; |
| | 0 | 35 | | } |
| | | 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) |
| | 0 | 45 | | { |
| | 0 | 46 | | page = page < 1 ? 1 : page; |
| | 0 | 47 | | limit = limit < 1 ? 10 : limit; |
| | 0 | 48 | | var result = await _promoOfferService.GetPromoOffers(page - 1, limit); |
| | | 49 | | |
| | 0 | 50 | | var totalCount = (await _promoOfferService.GetPromoOffers(0, null))?.Count(); |
| | 0 | 51 | | var totalFiltredCount = (await _promoOfferService.GetPromoOffers(0, null))?.Count(); |
| | | 52 | | |
| | 0 | 53 | | var response = new BaseResponseDTO<PromoOfferOutput>(_routeUrl, page, limit, totalFiltredCount ?? 0, totalCo |
| | 0 | 54 | | { |
| | 0 | 55 | | Data = result.Select(x => new DTO.PromoOfferOutput(x)).ToList() |
| | 0 | 56 | | }; |
| | 0 | 57 | | return Ok(response); |
| | 0 | 58 | | } |
| | | 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) |
| | 0 | 70 | | { |
| | 0 | 71 | | var result = await _promoOfferService.GetPromoOfferDetailed(id); |
| | 0 | 72 | | if(result == null) |
| | 0 | 73 | | { |
| | 0 | 74 | | return NotFound(); |
| | | 75 | | } |
| | 0 | 76 | | return Ok(new DTO.PromoOfferDetailedOutput(result)); |
| | 0 | 77 | | } |
| | | 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) |
| | 0 | 89 | | { |
| | 0 | 90 | | if (!ModelState.IsValid) |
| | 0 | 91 | | { |
| | 0 | 92 | | return BadRequestResult(""); |
| | | 93 | | } |
| | | 94 | | |
| | 0 | 95 | | var promo = new PromoOffer() |
| | 0 | 96 | | { |
| | 0 | 97 | | DateBegin = data.DateBegin, |
| | 0 | 98 | | DateEnd = data.DateEnd, |
| | 0 | 99 | | Good = await _goodService.GetGood(data.GoodId) ?? throw new ArgumentException("Good not found"), |
| | 0 | 100 | | MaxQuantity = data.MaxQuantity, |
| | 0 | 101 | | MinQuantity = data.MinQuantity, |
| | 0 | 102 | | Price = data.Price, |
| | 0 | 103 | | SupplierDepartment = await _departmentService.GetDepartment(data.DepartmentId) ?? throw new ArgumentExce |
| | 0 | 104 | | }; |
| | 0 | 105 | | await _promoOfferService.Create(promo); |
| | | 106 | | |
| | 0 | 107 | | return Ok(new DTO.PromoOfferDetailedOutput(promo)); |
| | 0 | 108 | | } |
| | | 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) |
| | 0 | 119 | | { |
| | 0 | 120 | | await _promoOfferService.Delete(id); |
| | 0 | 121 | | return Ok(); |
| | 0 | 122 | | } |
| | | 123 | | } |
| | | 124 | | } |