122 lines
3.8 KiB
C#
122 lines
3.8 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using GrossesMitainesAPI.Models;
|
|
using System.Linq;
|
|
using GrossesMitainesAPI.Data;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
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;
|
|
}
|
|
|
|
[HttpGet(Name = "Product"), AllowAnonymous]
|
|
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")]
|
|
public void Post(int id, string title, string category, string description, decimal? price, uint? quantity, bool? disc, string imagename) {
|
|
Product prod = new() {
|
|
Title = title,
|
|
Category = category,
|
|
Description = description,
|
|
Price = price.HasValue? (decimal)price: 0.01M,
|
|
Quantity = quantity.HasValue ? (uint)quantity : 0,
|
|
isDiscontinued = disc.HasValue? (bool)disc: false,
|
|
ImageName = imagename
|
|
};
|
|
try {
|
|
_context.Products.Add(prod);
|
|
} catch (Exception e) {
|
|
_logger.LogError(8, e.Message);
|
|
}
|
|
}
|
|
|
|
[HttpPut(Name = "Product")]
|
|
public void Put(int id, string title, string category, string description, decimal? price, uint? quantity, bool? disc, string imagename) {
|
|
try {
|
|
Product prod = _context.Products.Where(x => x.Id == id).First();
|
|
|
|
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 (quantity.HasValue)
|
|
prod.Quantity = (uint)quantity;
|
|
|
|
if (disc.HasValue)
|
|
prod.isDiscontinued = (bool)disc;
|
|
|
|
if (imagename != null || imagename != "")
|
|
prod.ImageName = imagename;
|
|
|
|
_context.Products.Update(prod);
|
|
} catch (Exception e) {
|
|
_logger.LogError(8, e.Message);
|
|
}
|
|
}
|
|
|
|
[HttpPatch(Name = "Product")]
|
|
public void Patch(int id, string title, string category, string description, decimal? price, uint? quantity, bool? disc, string imagename) {
|
|
try {
|
|
Product prod = _context.Products.Where(x => x.Id == id).First();
|
|
|
|
if (title != null)
|
|
prod.Title = title;
|
|
else prod.Title = "";
|
|
|
|
if (category != null)
|
|
prod.Category = category;
|
|
else prod.Category = "";
|
|
|
|
if (description != null)
|
|
prod.Description = description;
|
|
else prod.Description = "";
|
|
|
|
if (price.HasValue || price > 0)
|
|
prod.Price = (decimal)price;
|
|
else prod.Price = 0.01M;
|
|
|
|
if (quantity.HasValue)
|
|
prod.Quantity = (uint)quantity;
|
|
else prod.Quantity = 0;
|
|
|
|
if (disc.HasValue)
|
|
prod.isDiscontinued = (bool)disc;
|
|
else prod.isDiscontinued = false;
|
|
|
|
if (imagename != null)
|
|
prod.ImageName = imagename;
|
|
else prod.ImageName = "";
|
|
|
|
_context.Products.Update(prod);
|
|
} catch (Exception e) {
|
|
_logger.LogError(8, e.Message);
|
|
}
|
|
}
|
|
}
|
|
|