| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using Microsoft.AspNetCore.Http; |
| | | 6 | | using Microsoft.AspNetCore.Mvc; |
| | | 7 | | using Microsoft.Extensions.Logging; |
| | | 8 | | using SVETA.Api.Data.DTO; |
| | | 9 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 10 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 11 | | |
| | | 12 | | namespace SVETA.Api.Controllers |
| | | 13 | | { |
| | | 14 | | [Route("api/v1/PromoBids")] |
| | | 15 | | [ApiController] |
| | | 16 | | public class PromoBidsController : SvetaController |
| | | 17 | | { |
| | | 18 | | const string _routeUrl = "api/v1/PromoBids"; |
| | | 19 | | private readonly IPromoBidService _service; |
| | | 20 | | private readonly ILogger<PromoBidsController> _logger; |
| | | 21 | | |
| | 0 | 22 | | public PromoBidsController(IPromoBidService service, ILogger<PromoBidsController> logger) : base(logger) |
| | 0 | 23 | | { |
| | 0 | 24 | | _service = service; |
| | 0 | 25 | | _logger = logger; |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Action GetPromoBids |
| | | 30 | | /// </summary> |
| | | 31 | | /// <returns>List of PromoBids</returns> |
| | | 32 | | [HttpGet()] |
| | | 33 | | public async Task<IActionResult> GetPromoBids(int page = 1, int limit = 10) |
| | 0 | 34 | | { |
| | 0 | 35 | | page = page < 1 ? 1 : page; |
| | 0 | 36 | | limit = limit < 1 ? 10 : limit; |
| | 0 | 37 | | var data = await _service.GetPromoBids(page - 1, limit); |
| | | 38 | | |
| | 0 | 39 | | var totalCount = (await _service.GetPromoBids(0, null))?.Count(); |
| | 0 | 40 | | var totalFiltredCount = (await _service.GetPromoBids(0, null))?.Count(); |
| | | 41 | | |
| | 0 | 42 | | var response = new BaseResponseDTO<PromoBid>(_routeUrl, page, limit, totalFiltredCount ?? 0, totalCount ?? 0 |
| | 0 | 43 | | { |
| | 0 | 44 | | Data = data |
| | 0 | 45 | | }; |
| | 0 | 46 | | return Ok(response); |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Action GetPromoBid |
| | | 51 | | /// </summary> |
| | | 52 | | /// <param name="id"></param> |
| | | 53 | | /// <returns>Single PromoBid</returns> |
| | | 54 | | [HttpGet("GetPromoBid/{id}")] |
| | | 55 | | public async Task<IActionResult> GetPromoBid(long id) |
| | 0 | 56 | | { |
| | 0 | 57 | | return Ok(await _service.GetPromoBid(id)); |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Action PostPromoBid |
| | | 62 | | /// </summary> |
| | | 63 | | /// <param name="data"></param> |
| | | 64 | | [HttpPost("PostPromoBid")] |
| | | 65 | | public async Task<IActionResult> PostPromoBid([FromBody] PromoBid data) |
| | 0 | 66 | | { |
| | | 67 | | try |
| | 0 | 68 | | { |
| | 0 | 69 | | if (!ModelState.IsValid) |
| | 0 | 70 | | { |
| | 0 | 71 | | return BadRequest(); |
| | | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | await _service.Create(data); |
| | 0 | 75 | | return Created("GetPromoBid", data); |
| | | 76 | | } |
| | 0 | 77 | | catch (Exception e) |
| | 0 | 78 | | { |
| | 0 | 79 | | return ServerError(e); |
| | | 80 | | } |
| | 0 | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Action PutPromoBid |
| | | 85 | | /// </summary> |
| | | 86 | | /// <param name="data"></param> |
| | | 87 | | [HttpPut("PutPromoBid")] |
| | | 88 | | public async Task<IActionResult> PutPromoBid([FromBody] PromoBid data) |
| | 0 | 89 | | { |
| | | 90 | | try |
| | 0 | 91 | | { |
| | 0 | 92 | | if (await _service.PromoBidExists(data.Id) == false) |
| | 0 | 93 | | { |
| | 0 | 94 | | return NotFound(); |
| | | 95 | | } |
| | | 96 | | |
| | 0 | 97 | | await _service.Update(data); |
| | 0 | 98 | | return NoContent(); |
| | | 99 | | } |
| | 0 | 100 | | catch (Exception e) |
| | 0 | 101 | | { |
| | 0 | 102 | | return ServerError(e); |
| | | 103 | | } |
| | 0 | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Action DeletePromoBid |
| | | 108 | | /// </summary> |
| | | 109 | | /// <param name="id"></param> |
| | | 110 | | [HttpDelete("DeletePromoBid")] |
| | | 111 | | public async Task<IActionResult> DeletePromoBid(long id) |
| | 0 | 112 | | { |
| | 0 | 113 | | await _service.Delete(id); |
| | 0 | 114 | | return Ok(); |
| | 0 | 115 | | } |
| | | 116 | | } |
| | | 117 | | } |