GGMM/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs

157 lines
5.2 KiB
C#
Raw Normal View History

2022-10-08 15:04:29 -04:00
using Microsoft.AspNetCore.Mvc;
using GrossesMitainesAPI.Models;
using System.Linq;
using GrossesMitainesAPI.Data;
using Microsoft.Extensions.Logging;
2022-10-16 10:45:18 -04:00
using Microsoft.AspNetCore.Authorization;
2022-10-08 15:04:29 -04:00
namespace GrossesMitainesAPI.Controllers;
[ApiController, Route("api/[controller]")]
public class ProductController : Controller {
private readonly ILogger<ProductController> _logger;
private readonly InventoryContext _context;
public ProductController(ILogger<ProductController> logger, InventoryContext context) {
_logger = logger;
_context = context;
}
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) {
_logger.LogError(8, e.Message);
prod = new Product();
}
return prod;
}
[HttpPost(Name = "Product")]
2022-10-09 15:07:35 -04:00
public void Post(int id, string title, string category, string description, decimal? price, uint? quantity, bool? disc, string imagename) {
2022-10-09 13:18:39 -04:00
Product prod = new() {
Title = title,
2022-10-09 15:07:35 -04:00
Category = category,
2022-10-09 13:18:39 -04:00
Description = description,
2022-10-09 15:07:35 -04:00
Price = price.HasValue? (decimal)price: 0.01M,
Quantity = quantity.HasValue ? (uint)quantity : 0,
2022-10-09 13:18:39 -04:00
ImageName = imagename
};
2022-10-08 15:04:29 -04:00
try {
_context.Products.Add(prod);
2022-10-16 11:25:21 -04:00
_context.SaveChanges();
2022-10-08 15:04:29 -04:00
} catch (Exception e) {
_logger.LogError(8, e.Message);
}
}
2022-10-08 16:05:23 -04:00
[HttpPut(Name = "Product")]
2022-10-16 11:25:21 -04:00
public void Put(int id, string title, string category, string description, decimal? price, 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-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;
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();
2022-10-09 13:18:39 -04:00
} catch (Exception e) {
_logger.LogError(8, e.Message);
}
}
[HttpPatch(Name = "Product")]
2022-10-16 11:25:21 -04:00
public void Patch(int id, string title, string category, string description, decimal? price, uint? quantity, string? status, string imagename) {
2022-10-09 13:18:39 -04:00
try {
Product prod = _context.Products.Where(x => x.Id == id).First();
if (title != null)
prod.Title = title;
else prod.Title = "";
2022-10-09 15:07:35 -04:00
if (category != null)
prod.Category = category;
else prod.Category = "";
2022-10-09 13:18:39 -04:00
if (description != null)
prod.Description = description;
else prod.Description = "";
if (price.HasValue || price > 0)
prod.Price = (decimal)price;
else prod.Price = 0.01M;
2022-10-09 15:07:35 -04:00
if (quantity.HasValue)
prod.Quantity = (uint)quantity;
else prod.Quantity = 0;
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;
case "isDiscontinued":
prod.Status = Product.States.Discontinued;
break;
default:
prod.Status = prod.Quantity > 0 ? Product.States.Available : Product.States.Unavailable;
break;
}
2022-10-09 15:07:35 -04:00
2022-10-09 13:18:39 -04:00
if (imagename != null)
prod.ImageName = imagename;
else prod.ImageName = "";
2022-10-08 16:05:23 -04:00
_context.Products.Update(prod);
2022-10-16 11:25:21 -04:00
_context.SaveChanges();
2022-10-08 16:05:23 -04:00
} catch (Exception e) {
_logger.LogError(8, e.Message);
}
}
2022-10-08 15:04:29 -04:00
}