Changement dans la BD et les modèles

This commit is contained in:
MarcEricMartel 2022-10-09 10:18:39 -07:00
parent 2b7583b045
commit 418f79985e
12 changed files with 189 additions and 142 deletions

View File

@ -29,7 +29,13 @@ public class ProductController : Controller {
}
[HttpPost(Name = "Product")]
public void Post(Product prod) {
public void Post(string title, string description, decimal price, string imagename) {
Product prod = new() {
Title = title,
Description = description,
Price = price,
ImageName = imagename
};
try {
_context.Products.Add(prod);
} catch (Exception e) {
@ -38,8 +44,49 @@ public class ProductController : Controller {
}
[HttpPut(Name = "Product")]
public void Put(Product prod) {
public void Put(int id, string title, string description, decimal? price, string imagename) {
try {
Product prod = _context.Products.Where(x => x.Id == id).First();
if (title != null || title != "")
prod.Title = title;
if (description != null || description != "")
prod.Description = description;
if (price.HasValue || price > 0)
prod.Price = (decimal)price;
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 description, decimal? price, string imagename) {
try {
Product prod = _context.Products.Where(x => x.Id == id).First();
if (title != null)
prod.Title = title;
else prod.Title = "";
if (description != null)
prod.Description = description;
else prod.Description = "";
if (price.HasValue || price > 0)
prod.Price = (decimal)price;
else prod.Price = 0.01M;
if (imagename != null)
prod.ImageName = imagename;
else prod.ImageName = "";
_context.Products.Update(prod);
} catch (Exception e) {
_logger.LogError(8, e.Message);

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,82 @@
// <auto-generated />
using GrossesMitainesAPI.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace GrossesMitainesAPI.Migrations
{
[DbContext(typeof(InventoryContext))]
[Migration("20221009165926_Initial-Db")]
partial class InitialDb
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.9")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);
modelBuilder.Entity("GrossesMitainesAPI.Models.Product", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("Description")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("ImageName")
.HasColumnType("nvarchar(max)");
b.Property<decimal>("Price")
.HasColumnType("decimal(18,2)");
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("nvarchar(255)");
b.HasKey("Id");
b.ToTable("Products");
b.HasData(
new
{
Id = 1,
Description = "Pour faire votre propre bonhomme de 1837, comme dans le bon vieux temps.",
ImageName = "ceintureflechee",
Price = 85.86m,
Title = "Ceinture flèchée"
},
new
{
Id = 2,
Description = "Parce que ça sent la coupe!",
ImageName = "pantouflesCH",
Price = 15.64m,
Title = "Pantoufles du Canadien en Phentex"
},
new
{
Id = 3,
Description = "On ne lui ferait pas mal, en tout cas!!",
ImageName = "jeanlucmongrain",
Price = 1453.12m,
Title = "Jean-Luc Mongrain"
});
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,49 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace GrossesMitainesAPI.Migrations
{
public partial class InitialDb : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Products",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Title = table.Column<string>(type: "nvarchar(255)", maxLength: 255, nullable: false),
Description = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
ImageName = table.Column<string>(type: "nvarchar(max)", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Products", x => x.Id);
});
migrationBuilder.InsertData(
table: "Products",
columns: new[] { "Id", "Description", "ImageName", "Price", "Title" },
values: new object[] { 1, "Pour faire votre propre bonhomme de 1837, comme dans le bon vieux temps.", "ceintureflechee", 85.86m, "Ceinture flèchée" });
migrationBuilder.InsertData(
table: "Products",
columns: new[] { "Id", "Description", "ImageName", "Price", "Title" },
values: new object[] { 2, "Parce que ça sent la coupe!", "pantouflesCH", 15.64m, "Pantoufles du Canadien en Phentex" });
migrationBuilder.InsertData(
table: "Products",
columns: new[] { "Id", "Description", "ImageName", "Price", "Title" },
values: new object[] { 3, "On ne lui ferait pas mal, en tout cas!!", "jeanlucmongrain", 1453.12m, "Jean-Luc Mongrain" });
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Products");
}
}
}

File diff suppressed because one or more lines are too long

View File

@ -11,5 +11,5 @@ public class Product {
public string Description { get; set; } = "Lorem Ipsum.";
[Range(0.01, (double)decimal.MaxValue)] // Range qui prend pas les decimals!
public decimal Price { get; set; } = 0;
public string? ImageData { get; set; } // Base64 en string pour le html.
public string? ImageName { get; set; } // Base pour sortir les images ({ImageName}.png , {ImageName}_thumbnail.png, etc...)
}

View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB