GGMM/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs

189 lines
6.6 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-17 17:33:45 -04:00
using Microsoft.EntityFrameworkCore;
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-17 17:33:45 -04:00
public void Post(string title, string category, string description, decimal? price, decimal? promoprice, uint? quantity, string? status, string imagename) {
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-17 17:33:45 -04:00
Price = price.HasValue ? (decimal)price : 0.01M,
2022-10-16 20:33:58 -04:00
PromoPrice = promoprice.HasValue ? (decimal)promoprice : 0.01M,
2022-10-09 15:07:35 -04:00
Quantity = quantity.HasValue ? (uint)quantity : 0,
2022-10-09 13:18:39 -04:00
ImageName = imagename
};
2022-10-16 20:33:58 -04:00
2022-10-17 17:33:45 -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 "isPromotion":
prod.Status = Product.States.Promotion;
break;
case "isDiscontinued":
prod.Status = Product.States.Discontinued;
break;
default: break;
}
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();
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 20:33:58 -04:00
public void Put(int id, string title, string category, string description, decimal? price, decimal? promoprice, uint? quantity, string? status, string imagename) {
2022-10-17 17:33:45 -04:00
Product prod = _context.Products.Where(x => x.Id == id).FirstOrDefault();
if (prod == new Product())
Post(title, category, description, price, promoprice, quantity, status, imagename);
else try {
if (title != null || title != "")
prod.Title = title;
if (category != null || category != "")
prod.Category = category;
if (description != null || description != "")
prod.Description = description;
if (price.HasValue || price > 0)
prod.Price = (decimal)price;
if (promoprice.HasValue || promoprice > prod.Price)
prod.PromoPrice = (decimal)promoprice;
if (quantity.HasValue)
prod.Quantity = (uint)quantity;
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 "isPromotion":
prod.Status = Product.States.Promotion;
break;
case "isDiscontinued":
prod.Status = Product.States.Discontinued;
break;
default: break;
}
if (imagename != null || imagename != "")
prod.ImageName = imagename;
_context.Products.Update(prod);
_context.SaveChanges();
} catch (Exception e) {
_logger.LogError(8, e.Message);
}
}
[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();
2022-10-09 13:18:39 -04:00
} catch (Exception e) {
_logger.LogError(8, e.Message);
}
}
2022-10-17 17:33:45 -04:00
}