|
| 1 | +using Asp.Versioning; |
| 2 | +using Project.Core.Entities.Business; |
| 3 | +using Project.Core.Interfaces.IServices; |
| 4 | +using Microsoft.AspNetCore.Authentication.JwtBearer; |
| 5 | +using Microsoft.AspNetCore.Authorization; |
| 6 | +using Microsoft.AspNetCore.Mvc; |
| 7 | + |
| 8 | +namespace Project.API.Controllers.V1 |
| 9 | +{ |
| 10 | + [ApiVersion("1.0")] |
| 11 | + [Route("api/v{version:apiVersion}/[controller]")] |
| 12 | + [ApiController] |
| 13 | + [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] |
| 14 | + public class ProductController : ControllerBase |
| 15 | + { |
| 16 | + private readonly ILogger<ProductController> _logger; |
| 17 | + private readonly IProductService _productService; |
| 18 | + |
| 19 | + public ProductController(ILogger<ProductController> logger, IProductService productService) |
| 20 | + { |
| 21 | + _logger = logger; |
| 22 | + _productService = productService; |
| 23 | + } |
| 24 | + |
| 25 | + |
| 26 | + [HttpGet("paginated")] |
| 27 | + [AllowAnonymous] |
| 28 | + public async Task<IActionResult> Get(int? pageNumber, int? pageSize, CancellationToken cancellationToken) |
| 29 | + { |
| 30 | + try |
| 31 | + { |
| 32 | + int pageSizeValue = pageSize ?? 10; |
| 33 | + int pageNumberValue = pageNumber ?? 1; |
| 34 | + |
| 35 | + //Get peginated data |
| 36 | + var products = await _productService.GetPaginatedData(pageNumberValue, pageSizeValue, cancellationToken); |
| 37 | + |
| 38 | + return Ok(products); |
| 39 | + } |
| 40 | + catch (Exception ex) |
| 41 | + { |
| 42 | + _logger.LogError(ex, "An error occurred while retrieving products"); |
| 43 | + return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + [HttpGet] |
| 49 | + [AllowAnonymous] |
| 50 | + public async Task<IActionResult> Get(CancellationToken cancellationToken) |
| 51 | + { |
| 52 | + try |
| 53 | + { |
| 54 | + var products = await _productService.GetAll(cancellationToken); |
| 55 | + return Ok(products); |
| 56 | + } |
| 57 | + catch (Exception ex) |
| 58 | + { |
| 59 | + _logger.LogError(ex, "An error occurred while retrieving products"); |
| 60 | + return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); |
| 61 | + } |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + [HttpGet("{id}")] |
| 67 | + [AllowAnonymous] |
| 68 | + public async Task<IActionResult> Get(int id, CancellationToken cancellationToken) |
| 69 | + { |
| 70 | + try |
| 71 | + { |
| 72 | + var data = await _productService.GetById(id, cancellationToken); |
| 73 | + return Ok(data); |
| 74 | + } |
| 75 | + catch (Exception ex) |
| 76 | + { |
| 77 | + if (ex.Message == "No data found") |
| 78 | + { |
| 79 | + return StatusCode(StatusCodes.Status404NotFound, ex.Message); |
| 80 | + } |
| 81 | + _logger.LogError(ex, $"An error occurred while retrieving the product"); |
| 82 | + return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + [HttpPost] |
| 88 | + public async Task<IActionResult> Create(ProductCreateViewModel model, CancellationToken cancellationToken) |
| 89 | + { |
| 90 | + if (ModelState.IsValid) |
| 91 | + { |
| 92 | + string message = ""; |
| 93 | + if (await _productService.IsExists("Name", model.Name, cancellationToken)) |
| 94 | + { |
| 95 | + message = $"The product name- '{model.Name}' already exists"; |
| 96 | + return StatusCode(StatusCodes.Status400BadRequest, message); |
| 97 | + } |
| 98 | + |
| 99 | + if (await _productService.IsExists("Code", model.Code, cancellationToken)) |
| 100 | + { |
| 101 | + message = $"The product code- '{model.Code}' already exists"; |
| 102 | + return StatusCode(StatusCodes.Status400BadRequest, message); |
| 103 | + } |
| 104 | + |
| 105 | + try |
| 106 | + { |
| 107 | + var data = await _productService.Create(model, cancellationToken); |
| 108 | + return Ok(data); |
| 109 | + } |
| 110 | + catch (Exception ex) |
| 111 | + { |
| 112 | + _logger.LogError(ex, $"An error occurred while adding the product"); |
| 113 | + message = $"An error occurred while adding the product- " + ex.Message; |
| 114 | + |
| 115 | + return StatusCode(StatusCodes.Status500InternalServerError, message); |
| 116 | + } |
| 117 | + } |
| 118 | + return StatusCode(StatusCodes.Status400BadRequest, "Please input all required data"); |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + [HttpPut] |
| 123 | + public async Task<IActionResult> Edit(ProductUpdateViewModel model, CancellationToken cancellationToken) |
| 124 | + { |
| 125 | + if (ModelState.IsValid) |
| 126 | + { |
| 127 | + string message = ""; |
| 128 | + if (await _productService.IsExistsForUpdate(model.Id, "Name", model.Name, cancellationToken)) |
| 129 | + { |
| 130 | + message = "The product name- '{model.Name}' already exists"; |
| 131 | + return StatusCode(StatusCodes.Status400BadRequest, message); |
| 132 | + } |
| 133 | + |
| 134 | + if (await _productService.IsExistsForUpdate(model.Id, "Code", model.Code, cancellationToken)) |
| 135 | + { |
| 136 | + message = $"The product code- '{model.Code}' already exists"; |
| 137 | + return StatusCode(StatusCodes.Status400BadRequest, message); |
| 138 | + } |
| 139 | + |
| 140 | + try |
| 141 | + { |
| 142 | + await _productService.Update(model, cancellationToken); |
| 143 | + return Ok(); |
| 144 | + } |
| 145 | + catch (Exception ex) |
| 146 | + { |
| 147 | + _logger.LogError(ex, $"An error occurred while updating the product"); |
| 148 | + message = $"An error occurred while updating the product- " + ex.Message; |
| 149 | + |
| 150 | + return StatusCode(StatusCodes.Status500InternalServerError, message); |
| 151 | + } |
| 152 | + } |
| 153 | + return StatusCode(StatusCodes.Status400BadRequest, "Please input all required data"); |
| 154 | + } |
| 155 | + |
| 156 | + |
| 157 | + [HttpDelete("{id}")] |
| 158 | + public async Task<IActionResult> Delete(int id, CancellationToken cancellationToken) |
| 159 | + { |
| 160 | + try |
| 161 | + { |
| 162 | + await _productService.Delete(id, cancellationToken); |
| 163 | + return Ok(); |
| 164 | + } |
| 165 | + catch (Exception ex) |
| 166 | + { |
| 167 | + _logger.LogError(ex, "An error occurred while deleting the product"); |
| 168 | + return StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while deleting the product- " + ex.Message); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + [HttpGet("PriceCheck/{productId}")] |
| 173 | + [AllowAnonymous] |
| 174 | + public async Task<IActionResult> PriceCheck(int productId, CancellationToken cancellationToken) |
| 175 | + { |
| 176 | + try |
| 177 | + { |
| 178 | + var price = await _productService.PriceCheck(productId, cancellationToken); |
| 179 | + return Ok(price); |
| 180 | + } |
| 181 | + catch (Exception ex) |
| 182 | + { |
| 183 | + _logger.LogError(ex, "An error occurred while checking product price"); |
| 184 | + return StatusCode(StatusCodes.Status500InternalServerError, $"An error occurred while checking product price- {ex.Message}"); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + } |
| 189 | +} |
0 commit comments