This commit is contained in:
MarcEricMartel 2022-10-16 08:25:21 -07:00
parent 0cc291b2a8
commit dbc1c78f07
9 changed files with 111 additions and 56 deletions

View File

@ -17,12 +17,12 @@ public class InventoryController : Controller {
}
[HttpGet(Name = "Inventory")] // Pour faire des calls async par paquet de AMOUNT (5) (pour du loading en scrollant)
public IEnumerable<Product> Get(int? lastId, string order, string filter) {
const uint AMOUNT = 5;
public IEnumerable<Product> Get(int? lastId, string? order, string? filterPrice, string? filterState) {
const int AMOUNT = 5;
var ret = _context.Products.AsQueryable();
switch (filter) {
switch (filterPrice) {
case "PriceUnder20":
ret = ret.Where(x => x.Price < 20);
break;
@ -35,17 +35,24 @@ public class InventoryController : Controller {
case "PriceOver100":
ret = ret.Where(x => x.Price >= 100);
break;
default: break;
}
switch (filterState) {
case "isAvailable":
ret = ret.Where(x => x.Quantity > 0);
ret = ret.Where(x => x.Status == Product.States.Available);
break;
case "isUnavailable":
ret = ret.Where(x => x.Status == Product.States.Unavailable);
break;
case "isBackOrder":
ret = ret.Where(x => x.Quantity == 0 || !x.isDiscontinued);
ret = ret.Where(x => x.Status == Product.States.BackOrder);
break; ;
case "isClearance":
ret = ret.Where(x => x.Quantity > 0 && x.isDiscontinued);
ret = ret.Where(x => x.Status == Product.States.Clearance);
break;
case "isDiscontinued":
ret = ret.Where(x => x.Quantity == 0 && x.isDiscontinued);
ret = ret.Where(x => x.Status == Product.States.Discontinued);
break;
default: break;
}
@ -73,14 +80,12 @@ public class InventoryController : Controller {
}
if (order is not null && order.Contains("Desc")) {
if (!lastId.HasValue)
lastId = _context.Products.Max(x => x.Id);
ret = ret.Where(x => x.Id < lastId && x.Id > lastId - AMOUNT);
} else {
lastId = _context.Products.Max(x => x.Id) + 1;
} else
if (!lastId.HasValue)
lastId = 1;
ret = ret.Where(x => x.Id > lastId && x.Id < lastId + AMOUNT);
}
return ret.ToList();
lastId = _context.Products.Min(x => x.Id) - 1;
return ret.Where(x => x.Id > lastId).Take(AMOUNT).ToList();
}
// Inventory/Delete => Décrémenter un produit.
@ -93,13 +98,19 @@ public class InventoryController : Controller {
try {
Product prod = _context.Products.First(x => x.Id == id);
if (prod.Quantity > 0)
prod.Quantity--;
if (prod.Quantity > 0) {
prod.Quantity = prod.Quantity - 1;
if (prod.Quantity == 0)
prod.Status = prod.Status == Product.States.Clearance?
Product.States.Discontinued:
Product.States.Unavailable;
}
else {
_logger.LogError(8, "Delete de produit en backorder.");
_logger.LogError(8, "Vente de produit pas en stock.");
return;
}
_context.Products.Update(prod);
_context.SaveChanges();
} catch (Exception e) {
_logger.LogError(8, e.Message);
}

View File

@ -37,18 +37,18 @@ public class ProductController : Controller {
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);
_context.SaveChanges();
} 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) {
public void Put(int id, string title, string category, string description, decimal? price, uint? quantity, string? status, string imagename) {
try {
Product prod = _context.Products.Where(x => x.Id == id).First();
@ -67,20 +67,37 @@ public class ProductController : Controller {
if (quantity.HasValue)
prod.Quantity = (uint)quantity;
if (disc.HasValue)
prod.isDiscontinued = (bool)disc;
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;
}
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, uint? quantity, bool? disc, string imagename) {
public void Patch(int id, string title, string category, string description, decimal? price, uint? quantity, string? status, string imagename) {
try {
Product prod = _context.Products.Where(x => x.Id == id).First();
@ -104,15 +121,33 @@ public class ProductController : Controller {
prod.Quantity = (uint)quantity;
else prod.Quantity = 0;
if (disc.HasValue)
prod.isDiscontinued = (bool)disc;
else prod.isDiscontinued = false;
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;
}
if (imagename != null)
prod.ImageName = imagename;
else prod.ImageName = "";
_context.Products.Update(prod);
_context.SaveChanges();
} catch (Exception e) {
_logger.LogError(8, e.Message);
}

View File

@ -3,6 +3,7 @@ using GrossesMitainesAPI.Models;
using System.Linq;
using GrossesMitainesAPI.Data;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization;
namespace GrossesMitainesAPI.Controllers;

View File

@ -16,9 +16,9 @@ public class InventoryContext : DbContext {
Title = $"Ceinture flèchée",
Category = $"Linge",
Description = $"Pour faire votre propre bonhomme de 1837, comme dans le bon vieux temps.",
Status = Product.States.Promotion,
Price = 85.86M,
Quantity = 1,
isDiscontinued = false,
ImageName = $"ceintureflechee"
});
modelBuilder.Entity<Product>().HasData(new Product {
@ -26,9 +26,9 @@ public class InventoryContext : DbContext {
Title = $"Pantoufles du Canadien en Phentex",
Category = $"Linge",
Description = $"Parce que ça sent la coupe!",
Status = Product.States.Available,
Price = 15.64M,
Quantity = 54,
isDiscontinued = false,
ImageName = $"pantouflesCH"
});
modelBuilder.Entity<Product>().HasData(new Product {
@ -36,9 +36,9 @@ public class InventoryContext : DbContext {
Title = $"Jean-Luc Mongrain",
Category = $"Homme",
Description = $"On ne lui ferait pas mal, en tout cas!!",
Status = Product.States.Clearance,
Price = 1453.12M,
Quantity = 1,
isDiscontinued = true,
ImageName = $"jeanlucmongrain"
});
}

View File

@ -11,7 +11,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace GrossesMitainesAPI.Migrations
{
[DbContext(typeof(InventoryContext))]
[Migration("20221009190720_Initial-Db")]
[Migration("20221016150710_Initial-Db")]
partial class InitialDb
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -48,14 +48,14 @@ namespace GrossesMitainesAPI.Migrations
b.Property<long>("Quantity")
.HasColumnType("bigint");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("nvarchar(255)");
b.Property<bool>("isDiscontinued")
.HasColumnType("bit");
b.HasKey("Id");
b.ToTable("Products");
@ -69,8 +69,8 @@ namespace GrossesMitainesAPI.Migrations
ImageName = "ceintureflechee",
Price = 85.86m,
Quantity = 1L,
Title = "Ceinture flèchée",
isDiscontinued = false
Status = 4,
Title = "Ceinture flèchée"
},
new
{
@ -80,8 +80,8 @@ namespace GrossesMitainesAPI.Migrations
ImageName = "pantouflesCH",
Price = 15.64m,
Quantity = 54L,
Title = "Pantoufles du Canadien en Phentex",
isDiscontinued = false
Status = 0,
Title = "Pantoufles du Canadien en Phentex"
},
new
{
@ -91,8 +91,8 @@ namespace GrossesMitainesAPI.Migrations
ImageName = "jeanlucmongrain",
Price = 1453.12m,
Quantity = 1L,
Title = "Jean-Luc Mongrain",
isDiscontinued = true
Status = 3,
Title = "Jean-Luc Mongrain"
});
});
#pragma warning restore 612, 618

View File

@ -19,7 +19,7 @@ namespace GrossesMitainesAPI.Migrations
Description = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
Quantity = table.Column<long>(type: "bigint", nullable: false),
isDiscontinued = table.Column<bool>(type: "bit", nullable: false),
Status = table.Column<int>(type: "int", nullable: false),
ImageName = table.Column<string>(type: "nvarchar(max)", nullable: true)
},
constraints: table =>
@ -29,18 +29,18 @@ namespace GrossesMitainesAPI.Migrations
migrationBuilder.InsertData(
table: "Products",
columns: new[] { "Id", "Category", "Description", "ImageName", "Price", "Quantity", "Title", "isDiscontinued" },
values: new object[] { 1, "Linge", "Pour faire votre propre bonhomme de 1837, comme dans le bon vieux temps.", "ceintureflechee", 85.86m, 1L, "Ceinture flèchée", false });
columns: new[] { "Id", "Category", "Description", "ImageName", "Price", "Quantity", "Status", "Title" },
values: new object[] { 1, "Linge", "Pour faire votre propre bonhomme de 1837, comme dans le bon vieux temps.", "ceintureflechee", 85.86m, 1L, 4, "Ceinture flèchée" });
migrationBuilder.InsertData(
table: "Products",
columns: new[] { "Id", "Category", "Description", "ImageName", "Price", "Quantity", "Title", "isDiscontinued" },
values: new object[] { 2, "Linge", "Parce que ça sent la coupe!", "pantouflesCH", 15.64m, 54L, "Pantoufles du Canadien en Phentex", false });
columns: new[] { "Id", "Category", "Description", "ImageName", "Price", "Quantity", "Status", "Title" },
values: new object[] { 2, "Linge", "Parce que ça sent la coupe!", "pantouflesCH", 15.64m, 54L, 0, "Pantoufles du Canadien en Phentex" });
migrationBuilder.InsertData(
table: "Products",
columns: new[] { "Id", "Category", "Description", "ImageName", "Price", "Quantity", "Title", "isDiscontinued" },
values: new object[] { 3, "Homme", "On ne lui ferait pas mal, en tout cas!!", "jeanlucmongrain", 1453.12m, 1L, "Jean-Luc Mongrain", true });
columns: new[] { "Id", "Category", "Description", "ImageName", "Price", "Quantity", "Status", "Title" },
values: new object[] { 3, "Homme", "On ne lui ferait pas mal, en tout cas!!", "jeanlucmongrain", 1453.12m, 1L, 3, "Jean-Luc Mongrain" });
}
protected override void Down(MigrationBuilder migrationBuilder)

View File

@ -46,14 +46,14 @@ namespace GrossesMitainesAPI.Migrations
b.Property<long>("Quantity")
.HasColumnType("bigint");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("nvarchar(255)");
b.Property<bool>("isDiscontinued")
.HasColumnType("bit");
b.HasKey("Id");
b.ToTable("Products");
@ -67,8 +67,8 @@ namespace GrossesMitainesAPI.Migrations
ImageName = "ceintureflechee",
Price = 85.86m,
Quantity = 1L,
Title = "Ceinture flèchée",
isDiscontinued = false
Status = 4,
Title = "Ceinture flèchée"
},
new
{
@ -78,8 +78,8 @@ namespace GrossesMitainesAPI.Migrations
ImageName = "pantouflesCH",
Price = 15.64m,
Quantity = 54L,
Title = "Pantoufles du Canadien en Phentex",
isDiscontinued = false
Status = 0,
Title = "Pantoufles du Canadien en Phentex"
},
new
{
@ -89,8 +89,8 @@ namespace GrossesMitainesAPI.Migrations
ImageName = "jeanlucmongrain",
Price = 1453.12m,
Quantity = 1L,
Title = "Jean-Luc Mongrain",
isDiscontinued = true
Status = 3,
Title = "Jean-Luc Mongrain"
});
});
#pragma warning restore 612, 618

View File

@ -6,6 +6,14 @@ namespace GrossesMitainesAPI.Models;
// catégories, description, quantité disponible, images, prix normal et
// autres informations pertinentes
public class Product {
public enum States {
Available,
BackOrder,
Unavailable,
Clearance,
Promotion,
Discontinued
}
[Key]
public int Id { get; set; }
[Required, MaxLength(255)]
@ -17,6 +25,6 @@ public class Product {
[Required, Range(0.01, (double)decimal.MaxValue)] // Range qui prend pas les decimals!
public decimal Price { get; set; } = 0;
public uint Quantity { get; set; } = 0;
public bool isDiscontinued { get; set; } = false;
public States Status { get; set; } = States.Available;
public string? ImageName { get; set; } // Base pour sortir les images ({ImageName}.jpg , {ImageName}_thumbnail.jpg, etc...)
}

View File

@ -7,6 +7,6 @@
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb; Database=GrossesMitainesDB; Trusted_Connection=True; MultipleActiveResultSets=true"
"DefaultConnection": "Server=(localdb)\\mssqllocaldb; Database=GrossesMitainesDB0; Trusted_Connection=True; MultipleActiveResultSets=true"
}
}