Continuage d'API

This commit is contained in:
MarcEricMartel 2022-10-09 12:07:35 -07:00
parent f5351340d8
commit 1f7c97949d
7 changed files with 117 additions and 19 deletions

View File

@ -8,17 +8,40 @@ namespace GrossesMitainesAPI.Controllers;
[ApiController, Route("api/[controller]")] [ApiController, Route("api/[controller]")]
public class InventoryController : Controller { public class InventoryController : Controller {
private readonly ILogger<InventoryController> _logger;
private readonly InventoryContext _context; private readonly InventoryContext _context;
public InventoryController(InventoryContext context) { public InventoryController(ILogger<InventoryController> logger, InventoryContext context) {
_context = context; _context = context;
_logger = logger;
} }
[HttpGet(Name ="Inventory")] [HttpGet(Name ="Inventory")] // Pour faire des calls async par paquet de 5 (pour du loading en scrollant)
public IEnumerable<Product> Get(int? lastId) { public IEnumerable<Product> Get(int? lastId) {
if (!lastId.HasValue) if (!lastId.HasValue)
lastId = 1; lastId = 1;
return _context.Products.Where(x => x.Id >= lastId && x.Id < lastId + 5).ToList(); return _context.Products.Where(x => x.Id >= lastId && x.Id < lastId + 5).ToList();
} }
[HttpDelete(Name = "Inventory")]
public void Delete(int? id) {
if (!id.HasValue) {
_logger.LogError(8, "Delete sans Id.");
return;
}
try {
Product prod = _context.Products.First(x => x.Id == id);
if (prod.Quantity > 0)
prod.Quantity--;
else {
_logger.LogError(8, "Delete de produit en backorder.");
return;
}
_context.Products.Update(prod);
} catch (Exception e) {
_logger.LogError(8, e.Message);
}
}
} }

View File

@ -29,11 +29,14 @@ public class ProductController : Controller {
} }
[HttpPost(Name = "Product")] [HttpPost(Name = "Product")]
public void Post(string title, string description, decimal price, string imagename) { public void Post(int id, string title, string category, string description, decimal? price, uint? quantity, bool? disc, string imagename) {
Product prod = new() { Product prod = new() {
Title = title, Title = title,
Category = category,
Description = description, Description = description,
Price = price, Price = price.HasValue? (decimal)price: 0.01M,
Quantity = quantity.HasValue ? (uint)quantity : 0,
isDiscontinued = disc.HasValue? (bool)disc: false,
ImageName = imagename ImageName = imagename
}; };
try { try {
@ -44,19 +47,28 @@ public class ProductController : Controller {
} }
[HttpPut(Name = "Product")] [HttpPut(Name = "Product")]
public void Put(int id, string title, string description, decimal? price, string imagename) { public void Put(int id, string title, string category, string description, decimal? price, uint? quantity, bool? disc, string imagename) {
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 || title != "") if (title != null || title != "")
prod.Title = title; prod.Title = title;
if (category != null || category != "")
prod.Category = category;
if (description != null || description != "") if (description != null || description != "")
prod.Description = description; prod.Description = description;
if (price.HasValue || price > 0) if (price.HasValue || price > 0)
prod.Price = (decimal)price; prod.Price = (decimal)price;
if (quantity.HasValue)
prod.Quantity = (uint)quantity;
if (disc.HasValue)
prod.isDiscontinued = (bool)disc;
if (imagename != null || imagename != "") if (imagename != null || imagename != "")
prod.ImageName = imagename; prod.ImageName = imagename;
@ -67,7 +79,7 @@ public class ProductController : Controller {
} }
[HttpPatch(Name = "Product")] [HttpPatch(Name = "Product")]
public void Patch(int id, string title, string description, decimal? price, string imagename) { public void Patch(int id, string title, string category, string description, decimal? price, uint? quantity, bool? disc, string imagename) {
try { try {
Product prod = _context.Products.Where(x => x.Id == id).First(); Product prod = _context.Products.Where(x => x.Id == id).First();
@ -75,6 +87,10 @@ public class ProductController : Controller {
prod.Title = title; prod.Title = title;
else prod.Title = ""; else prod.Title = "";
if (category != null)
prod.Category = category;
else prod.Category = "";
if (description != null) if (description != null)
prod.Description = description; prod.Description = description;
else prod.Description = ""; else prod.Description = "";
@ -83,6 +99,14 @@ public class ProductController : Controller {
prod.Price = (decimal)price; prod.Price = (decimal)price;
else prod.Price = 0.01M; 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) if (imagename != null)
prod.ImageName = imagename; prod.ImageName = imagename;
else prod.ImageName = ""; else prod.ImageName = "";

View File

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

View File

@ -11,7 +11,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace GrossesMitainesAPI.Migrations namespace GrossesMitainesAPI.Migrations
{ {
[DbContext(typeof(InventoryContext))] [DbContext(typeof(InventoryContext))]
[Migration("20221009165926_Initial-Db")] [Migration("20221009190720_Initial-Db")]
partial class InitialDb partial class InitialDb
{ {
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -31,6 +31,10 @@ namespace GrossesMitainesAPI.Migrations
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1); SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("Category")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Description") b.Property<string>("Description")
.IsRequired() .IsRequired()
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");
@ -41,11 +45,17 @@ namespace GrossesMitainesAPI.Migrations
b.Property<decimal>("Price") b.Property<decimal>("Price")
.HasColumnType("decimal(18,2)"); .HasColumnType("decimal(18,2)");
b.Property<long>("Quantity")
.HasColumnType("bigint");
b.Property<string>("Title") b.Property<string>("Title")
.IsRequired() .IsRequired()
.HasMaxLength(255) .HasMaxLength(255)
.HasColumnType("nvarchar(255)"); .HasColumnType("nvarchar(255)");
b.Property<bool>("isDiscontinued")
.HasColumnType("bit");
b.HasKey("Id"); b.HasKey("Id");
b.ToTable("Products"); b.ToTable("Products");
@ -54,26 +64,35 @@ namespace GrossesMitainesAPI.Migrations
new new
{ {
Id = 1, Id = 1,
Category = "Linge",
Description = "Pour faire votre propre bonhomme de 1837, comme dans le bon vieux temps.", Description = "Pour faire votre propre bonhomme de 1837, comme dans le bon vieux temps.",
ImageName = "ceintureflechee", ImageName = "ceintureflechee",
Price = 85.86m, Price = 85.86m,
Title = "Ceinture flèchée" Quantity = 1L,
Title = "Ceinture flèchée",
isDiscontinued = false
}, },
new new
{ {
Id = 2, Id = 2,
Category = "Linge",
Description = "Parce que ça sent la coupe!", Description = "Parce que ça sent la coupe!",
ImageName = "pantouflesCH", ImageName = "pantouflesCH",
Price = 15.64m, Price = 15.64m,
Title = "Pantoufles du Canadien en Phentex" Quantity = 54L,
Title = "Pantoufles du Canadien en Phentex",
isDiscontinued = false
}, },
new new
{ {
Id = 3, Id = 3,
Category = "Homme",
Description = "On ne lui ferait pas mal, en tout cas!!", Description = "On ne lui ferait pas mal, en tout cas!!",
ImageName = "jeanlucmongrain", ImageName = "jeanlucmongrain",
Price = 1453.12m, Price = 1453.12m,
Title = "Jean-Luc Mongrain" Quantity = 1L,
Title = "Jean-Luc Mongrain",
isDiscontinued = true
}); });
}); });
#pragma warning restore 612, 618 #pragma warning restore 612, 618

View File

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

View File

@ -29,6 +29,10 @@ namespace GrossesMitainesAPI.Migrations
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1); SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("Category")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Description") b.Property<string>("Description")
.IsRequired() .IsRequired()
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");
@ -39,11 +43,17 @@ namespace GrossesMitainesAPI.Migrations
b.Property<decimal>("Price") b.Property<decimal>("Price")
.HasColumnType("decimal(18,2)"); .HasColumnType("decimal(18,2)");
b.Property<long>("Quantity")
.HasColumnType("bigint");
b.Property<string>("Title") b.Property<string>("Title")
.IsRequired() .IsRequired()
.HasMaxLength(255) .HasMaxLength(255)
.HasColumnType("nvarchar(255)"); .HasColumnType("nvarchar(255)");
b.Property<bool>("isDiscontinued")
.HasColumnType("bit");
b.HasKey("Id"); b.HasKey("Id");
b.ToTable("Products"); b.ToTable("Products");
@ -52,26 +62,35 @@ namespace GrossesMitainesAPI.Migrations
new new
{ {
Id = 1, Id = 1,
Category = "Linge",
Description = "Pour faire votre propre bonhomme de 1837, comme dans le bon vieux temps.", Description = "Pour faire votre propre bonhomme de 1837, comme dans le bon vieux temps.",
ImageName = "ceintureflechee", ImageName = "ceintureflechee",
Price = 85.86m, Price = 85.86m,
Title = "Ceinture flèchée" Quantity = 1L,
Title = "Ceinture flèchée",
isDiscontinued = false
}, },
new new
{ {
Id = 2, Id = 2,
Category = "Linge",
Description = "Parce que ça sent la coupe!", Description = "Parce que ça sent la coupe!",
ImageName = "pantouflesCH", ImageName = "pantouflesCH",
Price = 15.64m, Price = 15.64m,
Title = "Pantoufles du Canadien en Phentex" Quantity = 54L,
Title = "Pantoufles du Canadien en Phentex",
isDiscontinued = false
}, },
new new
{ {
Id = 3, Id = 3,
Category = "Homme",
Description = "On ne lui ferait pas mal, en tout cas!!", Description = "On ne lui ferait pas mal, en tout cas!!",
ImageName = "jeanlucmongrain", ImageName = "jeanlucmongrain",
Price = 1453.12m, Price = 1453.12m,
Title = "Jean-Luc Mongrain" Quantity = 1L,
Title = "Jean-Luc Mongrain",
isDiscontinued = true
}); });
}); });
#pragma warning restore 612, 618 #pragma warning restore 612, 618

View File

@ -17,5 +17,6 @@ public class Product {
[Required, Range(0.01, (double)decimal.MaxValue)] // Range qui prend pas les decimals! [Required, Range(0.01, (double)decimal.MaxValue)] // Range qui prend pas les decimals!
public decimal Price { get; set; } = 0; public decimal Price { get; set; } = 0;
public uint Quantity { get; set; } = 0; public uint Quantity { get; set; } = 0;
public bool isDiscontinued { get; set; } = false;
public string? ImageName { get; set; } // Base pour sortir les images ({ImageName}.jpg , {ImageName}_thumbnail.jpg, etc...) public string? ImageName { get; set; } // Base pour sortir les images ({ImageName}.jpg , {ImageName}_thumbnail.jpg, etc...)
} }