react-version #1

Merged
memartel_loc merged 290 commits from react-version into main 2023-11-04 09:48:15 -04:00
Showing only changes of commit 6d5c97a4e5 - Show all commits

View File

@ -4,6 +4,7 @@ using System.Linq;
using GrossesMitainesAPI.Data; using GrossesMitainesAPI.Data;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
namespace GrossesMitainesAPI.Controllers; namespace GrossesMitainesAPI.Controllers;
@ -30,7 +31,7 @@ public class ProductController : Controller {
} }
[HttpPost(Name = "Product")] [HttpPost(Name = "Product")]
public void Post(string title, string category, string description, decimal? price, decimal? promoprice, uint? quantity, bool? disc, string imagename) { public void Post(string title, string category, string description, decimal? price, decimal? promoprice, uint? quantity, string? status, string imagename) {
Product prod = new() { Product prod = new() {
Title = title, Title = title,
Category = category, Category = category,
@ -41,6 +42,28 @@ public class ProductController : Controller {
ImageName = imagename 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) if (prod.Price <= prod.PromoPrice)
prod.PromoPrice = prod.Price - 0.01M; prod.PromoPrice = prod.Price - 0.01M;
@ -54,9 +77,11 @@ public class ProductController : Controller {
[HttpPut(Name = "Product")] [HttpPut(Name = "Product")]
public void Put(int id, string title, string category, string description, decimal? price, decimal? promoprice, uint? quantity, string? status, string imagename) { public void Put(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).FirstOrDefault();
Product prod = _context.Products.Where(x => x.Id == id).First();
if (prod == new Product())
Post(title, category, description, price, promoprice, quantity, status, imagename);
else try {
if (title != null || title != "") if (title != null || title != "")
prod.Title = title; prod.Title = title;
@ -112,25 +137,23 @@ public class ProductController : Controller {
try { try {
Product prod = _context.Products.Where(x => x.Id == id).First(); Product prod = _context.Products.Where(x => x.Id == id).First();
if (title != null) if (title != null || title != "")
prod.Title = title; prod.Title = title;
else prod.Title = "";
if (category != null) if (category != null || category != "")
prod.Category = category; prod.Category = category;
else prod.Category = "";
if (description != null) if (description != null || description != "")
prod.Description = description; prod.Description = description;
else prod.Description = "";
if (promoprice.HasValue || promoprice < prod.Price) if (price.HasValue || price > 0)
prod.Price = (decimal)price;
if (promoprice.HasValue || promoprice > prod.Price)
prod.PromoPrice = (decimal)promoprice; prod.PromoPrice = (decimal)promoprice;
else prod.PromoPrice = prod.Price - 0.01M;
if (quantity.HasValue) if (quantity.HasValue)
prod.Quantity = (uint)quantity; prod.Quantity = (uint)quantity;
else prod.Quantity = 0;
switch (status) { switch (status) {
case "isAvailable": case "isAvailable":
@ -151,14 +174,11 @@ public class ProductController : Controller {
case "isDiscontinued": case "isDiscontinued":
prod.Status = Product.States.Discontinued; prod.Status = Product.States.Discontinued;
break; break;
default: default: break;
prod.Status = prod.Quantity > 0 ? Product.States.Available : Product.States.Unavailable;
break;
} }
if (imagename != null) if (imagename != null || imagename != "")
prod.ImageName = imagename; prod.ImageName = imagename;
else prod.ImageName = "";
_context.Products.Update(prod); _context.Products.Update(prod);
_context.SaveChanges(); _context.SaveChanges();
@ -167,4 +187,3 @@ public class ProductController : Controller {
} }
} }
} }