206 lines
		
	
	
		
			7.2 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			206 lines
		
	
	
		
			7.2 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using Microsoft.AspNetCore.Mvc;
 | 
						|
using GrossesMitainesAPI.Models;
 | 
						|
using System.Linq;
 | 
						|
using GrossesMitainesAPI.Data;
 | 
						|
using Microsoft.Extensions.Logging;
 | 
						|
using Microsoft.AspNetCore.Authorization;
 | 
						|
using Microsoft.EntityFrameworkCore;
 | 
						|
using Microsoft.AspNetCore.Cors;
 | 
						|
 | 
						|
namespace GrossesMitainesAPI.Controllers;
 | 
						|
 | 
						|
[EnableCors("_myAllowSpecificOrigins")]
 | 
						|
[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;
 | 
						|
    }
 | 
						|
 | 
						|
    [EnableCors("_myAllowSpecificOrigins")]
 | 
						|
    [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;
 | 
						|
    }
 | 
						|
 | 
						|
    [EnableCors("_myAllowSpecificOrigins")]
 | 
						|
    [HttpPost(Name = "Product")]
 | 
						|
    public void Post(string title, string category, string description, decimal? price, decimal? promoprice, uint? quantity, string? status, string imagename) {
 | 
						|
        Product prod = new() {
 | 
						|
            Title = title,
 | 
						|
            Category = category,
 | 
						|
            Description = description,
 | 
						|
            Price = price.HasValue ? (decimal)price : 0.01M,
 | 
						|
            PromoPrice = promoprice.HasValue ? (decimal)promoprice : 0.01M,
 | 
						|
            Quantity = quantity.HasValue ? (uint)quantity : 0,
 | 
						|
            ImageName = imagename
 | 
						|
        };
 | 
						|
 | 
						|
        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 (prod.Price <= prod.PromoPrice)
 | 
						|
            prod.PromoPrice = prod.Price - 0.01M;
 | 
						|
 | 
						|
        try {
 | 
						|
            _context.Products.Add(prod);
 | 
						|
            _context.SaveChanges();
 | 
						|
        } catch (Exception e) {
 | 
						|
            _logger.LogError(8, e.Message);
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    [EnableCors("_myAllowSpecificOrigins")]
 | 
						|
    [HttpPut(Name = "Product")]
 | 
						|
    public void Put(int id, string title, string category, string description, decimal? price, decimal? promoprice, uint? quantity, string? status, string imagename) {
 | 
						|
        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);
 | 
						|
            }
 | 
						|
    }
 | 
						|
 | 
						|
    [EnableCors("_myAllowSpecificOrigins")]
 | 
						|
    [HttpPatch(Name = "Product")]
 | 
						|
    public void Patch(int id, string title, string category, string description, decimal? price, decimal? promoprice, uint? quantity, string? status, 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 (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);
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    [EnableCors("_myAllowSpecificOrigins")]
 | 
						|
    [HttpDelete(Name = "Product")]
 | 
						|
    public void DeleteProduct(int id) {
 | 
						|
        try {
 | 
						|
            _context.Products.Remove(_context.Products.Where(x => x.Id == id).First());
 | 
						|
            _context.SaveChanges();
 | 
						|
        } catch (Exception e) {
 | 
						|
            _logger.LogError(8, e.Message);
 | 
						|
        }
 | 
						|
    }
 | 
						|
} |