GGMM/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs

141 lines
4.3 KiB
C#
Raw Normal View History

using GrossesMitainesAPI.Models;
2022-10-08 15:04:29 -04:00
using System.Linq;
using GrossesMitainesAPI.Data;
using Microsoft.Extensions.Logging;
2022-10-16 10:45:18 -04:00
using Microsoft.AspNetCore.Authorization;
2022-10-17 17:33:45 -04:00
using Microsoft.EntityFrameworkCore;
2022-10-18 10:08:23 -04:00
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
2022-10-08 15:04:29 -04:00
namespace GrossesMitainesAPI.Controllers;
2022-10-18 11:32:06 -04:00
[EnableCors("_myAllowSpecificOrigins")]
2022-10-08 15:04:29 -04:00
[ApiController, Route("api/[controller]")]
public class ProductController : ControllerBase {
2022-10-08 15:04:29 -04:00
private readonly ILogger<ProductController> _logger;
private readonly InventoryContext _context;
public ProductController(ILogger<ProductController> logger, InventoryContext context) {
_logger = logger;
_context = context;
}
2022-10-18 10:08:23 -04:00
[EnableCors("_myAllowSpecificOrigins")]
2022-10-16 10:45:18 -04:00
[HttpGet(Name = "Product"), AllowAnonymous]
2022-10-08 15:04:29 -04:00
public Product Get(int id) {
Product prod;
try {
prod = _context.Products.Where(x => x.Id == id).First();
}
catch (Exception e) {
2022-10-08 15:04:29 -04:00
_logger.LogError(8, e.Message);
prod = new Product();
}
return prod;
}
2022-10-18 10:08:23 -04:00
[EnableCors("_myAllowSpecificOrigins")]
2022-10-08 15:04:29 -04:00
[HttpPost(Name = "Product")]
public ActionResult<Product> Post(Product prod) {
2022-10-17 17:33:45 -04:00
2022-10-16 20:33:58 -04:00
if (prod.Price <= prod.PromoPrice)
prod.PromoPrice = prod.Price - 0.01M;
2022-10-08 15:04:29 -04:00
try {
_context.Products.Add(prod);
2022-10-16 11:25:21 -04:00
_context.SaveChanges();
}
catch (Exception e) {
2022-10-08 15:04:29 -04:00
_logger.LogError(8, e.Message);
return BadRequest();
2022-10-08 15:04:29 -04:00
}
return prod;
2022-10-08 15:04:29 -04:00
}
2022-10-08 16:05:23 -04:00
2022-10-18 10:08:23 -04:00
[EnableCors("_myAllowSpecificOrigins")]
2022-10-08 16:05:23 -04:00
[HttpPut(Name = "Product")]
public ActionResult<Product> Put(Product prod) {
try {
_context.Products.Update(prod);
_context.SaveChanges();
}
catch (Exception e) {
_logger.LogError(8, e.Message);
return BadRequest();
}
return prod;
2022-10-17 17:33:45 -04:00
}
2022-10-18 10:08:23 -04:00
[EnableCors("_myAllowSpecificOrigins")]
2022-10-17 17:33:45 -04:00
[HttpPatch(Name = "Product")]
public void Patch(int id, string title, string category, string description, decimal? price, decimal? promoprice, uint? quantity, string? status, string imagename) {
2022-10-08 16:05:23 -04:00
try {
2022-10-09 13:18:39 -04:00
Product prod = _context.Products.Where(x => x.Id == id).First();
if (title != null || title != "")
prod.Title = title;
2022-10-09 15:07:35 -04:00
if (category != null || category != "")
prod.Category = category;
2022-10-09 13:18:39 -04:00
if (description != null || description != "")
prod.Description = description;
if (price.HasValue || price > 0)
prod.Price = (decimal)price;
2022-10-16 20:33:58 -04:00
if (promoprice.HasValue || promoprice > prod.Price)
prod.PromoPrice = (decimal)promoprice;
2022-10-09 15:07:35 -04:00
if (quantity.HasValue)
prod.Quantity = (uint)quantity;
2022-10-16 11:25:21 -04:00
switch (status) {
case "isAvailable":
prod.Status = Product.States.Available;
break;
case "isUnavailable":
prod.Status = Product.States.Unavailable;
break;
case "isBackOrder":
prod.Status = Product.States.BackOrder;
break; ;
case "isClearance":
prod.Status = Product.States.Clearance;
break;
2022-10-16 20:33:58 -04:00
case "isPromotion":
prod.Status = Product.States.Promotion;
break;
2022-10-16 11:25:21 -04:00
case "isDiscontinued":
prod.Status = Product.States.Discontinued;
break;
default: break;
}
2022-10-09 15:07:35 -04:00
2022-10-09 13:18:39 -04:00
if (imagename != null || imagename != "")
prod.ImageName = imagename;
_context.Products.Update(prod);
2022-10-16 11:25:21 -04:00
_context.SaveChanges();
}
catch (Exception e) {
2022-10-09 13:18:39 -04:00
_logger.LogError(8, e.Message);
}
}
2022-10-18 11:01:16 -04:00
2022-10-18 10:08:23 -04:00
[EnableCors("_myAllowSpecificOrigins")]
2022-10-18 11:01:16 -04:00
[HttpDelete(Name = "Product")]
public IActionResult DeleteProduct(int id) {
2022-10-18 11:01:16 -04:00
try {
_context.Products.Remove(_context.Products.Where(x => x.Id == id).First());
_context.SaveChanges();
}
catch (Exception e) {
2022-10-18 11:01:16 -04:00
_logger.LogError(8, e.Message);
}
return Ok();
2022-10-18 11:01:16 -04:00
}
2022-10-17 17:33:45 -04:00
}