diff --git a/MySolution/InventoryManagement_MariaDB/Components/Accessary.cs b/MySolution/InventoryManagement_MariaDB/Components/Accessary.cs new file mode 100644 index 0000000..6bfca38 --- /dev/null +++ b/MySolution/InventoryManagement_MariaDB/Components/Accessary.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace InventoryManagement.Components +{ + internal class Accessary + { + public int Id { get; set; } + public int ProductId { get; set; } + public string Name { get; set; } + public string? Description { get; set; } + + public Product CompatibleProduct { get; set; } + } +} diff --git a/MySolution/InventoryManagement_MariaDB/Components/InventoryContext.cs b/MySolution/InventoryManagement_MariaDB/Components/InventoryContext.cs new file mode 100644 index 0000000..1cb150e --- /dev/null +++ b/MySolution/InventoryManagement_MariaDB/Components/InventoryContext.cs @@ -0,0 +1,21 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace InventoryManagement.Components +{ + internal class InventoryContext : DbContext + { + public DbSet Products { get; set; } + public DbSet Accessaries { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseMySQL(@"Server=peacecloud.synology.me;Port=3307;Database=Study;Uid=study;Pwd=Study1234!;"); + base.OnConfiguring(optionsBuilder); + } + } +} diff --git a/MySolution/InventoryManagement_MariaDB/Components/InventoryManager.cs b/MySolution/InventoryManagement_MariaDB/Components/InventoryManager.cs new file mode 100644 index 0000000..21e62e6 --- /dev/null +++ b/MySolution/InventoryManagement_MariaDB/Components/InventoryManager.cs @@ -0,0 +1,158 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace InventoryManagement.Components +{ + internal class InventoryManager + { + public void AddProduct(Product product) + { + using (var context = new InventoryContext()) + { + if (context.Products.Any(p => p.Name == product.Name)) + { + Console.WriteLine("Product with the same name already exists."); + return; + } + + context.Products.Add(product); + context.SaveChanges(); + Console.WriteLine($"{product.Name} added."); + } + } + + public void BuyProduct(string name, int quantity) + { + using (var context = new InventoryContext()) + { + var product = context.Products.FirstOrDefault(p => p.Name == name); + if (product == null) + { + Console.WriteLine($"Cannot find the product named {name}"); + return; + } + + product.Quantity += quantity; + context.SaveChanges(); + Console.WriteLine($"{product.Name}'s quantity: {product.Quantity}"); + } + } + + public void BuyProduct(int id, int quantity) + { + using (var context = new InventoryContext()) + { + var product = context.Products.Find(id); + if (product == null) + { + Console.WriteLine("Product not found."); + return; + } + + product.Quantity += quantity; + context.SaveChanges(); + Console.WriteLine($"{product.Name}'s quantity: {product.Quantity}"); + } + } + + public void SellProduct(string name, int quantity) + { + using (var context = new InventoryContext()) + { + var product = context.Products.FirstOrDefault(p => p.Name == name); + if (product == null) + { + Console.WriteLine($"Cannot find the product named {name}"); + return; + } + + if (product.Quantity >= quantity) + { + product.Quantity -= quantity; + context.SaveChanges(); + Console.WriteLine($"{quantity} units of {product.Name} sold."); + } + else + { + Console.WriteLine($"Insufficient quantity in stock (Current: {product.Quantity})"); + } + } + } + + public void SellProduct(int id, int quantity) + { + using (var context = new InventoryContext()) + { + var product = context.Products.Find(id); + if (product == null) + { + Console.WriteLine("Product not found."); + return; + } + + if (product.Quantity >= quantity) + { + product.Quantity -= quantity; + context.SaveChanges(); + Console.WriteLine($"{quantity} units of {product.Name} sold."); + } + else + { + Console.WriteLine($"Insufficient quantity in stock (Current: {product.Quantity})"); + } + } + } + + public void AddAccessary(int productId, string name, string description) + { + using (var context = new InventoryContext()) + { + var product = context.Products.Find(productId); + if (product == null) + { + Console.WriteLine("Product not found."); + return; + } + + context.Entry(product).Collection(e => e.CompatibleAccessaries).Load(); + + Accessary acc = new Accessary() + { + ProductId = productId, + Name = name, + Description = description, + }; + + product.CompatibleAccessaries.Add(acc); + context.SaveChanges(); + Console.WriteLine($"{acc.Name} added."); + } + } + + public void ShowInventory() + { + using (var context = new InventoryContext()) + { + Console.WriteLine("********** Current Inventory **********"); + Console.WriteLine("***** Products *****"); + foreach (var product in context.Products) + { + Console.WriteLine($"ID: {product.Id}, Name: {product.Name}, Quantity: {product.Quantity}"); + } + Console.WriteLine("********************"); + Console.WriteLine(); + Console.WriteLine("***** Accesarry *****"); + foreach (var accesarry in context.Accessaries.Include(e => e.CompatibleProduct)) + { + Console.WriteLine($"ID: {accesarry.Id}, Name: {accesarry.Name}, Compatible product: {accesarry.CompatibleProduct.Name}"); + } + Console.WriteLine("*********************"); + Console.WriteLine("****************************************"); + } + } + } +} diff --git a/MySolution/InventoryManagement_MariaDB/Components/Product.cs b/MySolution/InventoryManagement_MariaDB/Components/Product.cs new file mode 100644 index 0000000..811eaff --- /dev/null +++ b/MySolution/InventoryManagement_MariaDB/Components/Product.cs @@ -0,0 +1,21 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace InventoryManagement.Components +{ + [PrimaryKey(nameof(Id))] + [Index(nameof(Name), IsUnique = true)] + internal class Product + { + public int Id { get; } + public string Name { get; set; } + public int Quantity { get; set; } + + public ICollection CompatibleAccessaries { get; set; } + } +} diff --git a/MySolution/InventoryManagement_MariaDB/InventoryManagement_MariaDB.csproj b/MySolution/InventoryManagement_MariaDB/InventoryManagement_MariaDB.csproj new file mode 100644 index 0000000..d35751f --- /dev/null +++ b/MySolution/InventoryManagement_MariaDB/InventoryManagement_MariaDB.csproj @@ -0,0 +1,24 @@ + + + + Exe + net6.0 + enable + enable + True + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + diff --git a/MySolution/InventoryManagement_MariaDB/Migrations/20231019083443_initialize.Designer.cs b/MySolution/InventoryManagement_MariaDB/Migrations/20231019083443_initialize.Designer.cs new file mode 100644 index 0000000..f27c004 --- /dev/null +++ b/MySolution/InventoryManagement_MariaDB/Migrations/20231019083443_initialize.Designer.cs @@ -0,0 +1,86 @@ +// +using InventoryManagement.Components; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace InventoryManagement_MariaDB.Migrations +{ + [DbContext(typeof(InventoryContext))] + [Migration("20231019083443_initialize")] + partial class initialize + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.12") + .HasAnnotation("Relational:MaxIdentifierLength", 64); + + modelBuilder.Entity("InventoryManagement.Components.Accessary", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("Description") + .HasColumnType("longtext"); + + b.Property("Name") + .IsRequired() + .HasColumnType("longtext"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ProductId"); + + b.ToTable("Accessaries"); + }); + + modelBuilder.Entity("InventoryManagement.Components.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("varchar(255)"); + + b.Property("Quantity") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("Products"); + }); + + modelBuilder.Entity("InventoryManagement.Components.Accessary", b => + { + b.HasOne("InventoryManagement.Components.Product", "CompatibleProduct") + .WithMany("CompatibleAccessaries") + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("CompatibleProduct"); + }); + + modelBuilder.Entity("InventoryManagement.Components.Product", b => + { + b.Navigation("CompatibleAccessaries"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MySolution/InventoryManagement_MariaDB/Migrations/20231019083443_initialize.cs b/MySolution/InventoryManagement_MariaDB/Migrations/20231019083443_initialize.cs new file mode 100644 index 0000000..849b719 --- /dev/null +++ b/MySolution/InventoryManagement_MariaDB/Migrations/20231019083443_initialize.cs @@ -0,0 +1,76 @@ +using Microsoft.EntityFrameworkCore.Migrations; +using MySql.EntityFrameworkCore.Metadata; + +#nullable disable + +namespace InventoryManagement_MariaDB.Migrations +{ + /// + public partial class initialize : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AlterDatabase() + .Annotation("MySQL:Charset", "utf8mb4"); + + migrationBuilder.CreateTable( + name: "Products", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("MySQL:ValueGenerationStrategy", MySQLValueGenerationStrategy.IdentityColumn), + Name = table.Column(type: "varchar(255)", nullable: false), + Quantity = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Products", x => x.Id); + }) + .Annotation("MySQL:Charset", "utf8mb4"); + + migrationBuilder.CreateTable( + name: "Accessaries", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("MySQL:ValueGenerationStrategy", MySQLValueGenerationStrategy.IdentityColumn), + ProductId = table.Column(type: "int", nullable: false), + Name = table.Column(type: "longtext", nullable: false), + Description = table.Column(type: "longtext", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Accessaries", x => x.Id); + table.ForeignKey( + name: "FK_Accessaries_Products_ProductId", + column: x => x.ProductId, + principalTable: "Products", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }) + .Annotation("MySQL:Charset", "utf8mb4"); + + migrationBuilder.CreateIndex( + name: "IX_Accessaries_ProductId", + table: "Accessaries", + column: "ProductId"); + + migrationBuilder.CreateIndex( + name: "IX_Products_Name", + table: "Products", + column: "Name", + unique: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Accessaries"); + + migrationBuilder.DropTable( + name: "Products"); + } + } +} diff --git a/MySolution/InventoryManagement_MariaDB/Migrations/InventoryContextModelSnapshot.cs b/MySolution/InventoryManagement_MariaDB/Migrations/InventoryContextModelSnapshot.cs new file mode 100644 index 0000000..d4985df --- /dev/null +++ b/MySolution/InventoryManagement_MariaDB/Migrations/InventoryContextModelSnapshot.cs @@ -0,0 +1,83 @@ +// +using InventoryManagement.Components; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace InventoryManagement_MariaDB.Migrations +{ + [DbContext(typeof(InventoryContext))] + partial class InventoryContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.12") + .HasAnnotation("Relational:MaxIdentifierLength", 64); + + modelBuilder.Entity("InventoryManagement.Components.Accessary", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("Description") + .HasColumnType("longtext"); + + b.Property("Name") + .IsRequired() + .HasColumnType("longtext"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ProductId"); + + b.ToTable("Accessaries"); + }); + + modelBuilder.Entity("InventoryManagement.Components.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("varchar(255)"); + + b.Property("Quantity") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("Products"); + }); + + modelBuilder.Entity("InventoryManagement.Components.Accessary", b => + { + b.HasOne("InventoryManagement.Components.Product", "CompatibleProduct") + .WithMany("CompatibleAccessaries") + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("CompatibleProduct"); + }); + + modelBuilder.Entity("InventoryManagement.Components.Product", b => + { + b.Navigation("CompatibleAccessaries"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MySolution/InventoryManagement_MariaDB/Program.cs b/MySolution/InventoryManagement_MariaDB/Program.cs new file mode 100644 index 0000000..755df90 --- /dev/null +++ b/MySolution/InventoryManagement_MariaDB/Program.cs @@ -0,0 +1,101 @@ +using InventoryManagement.Components; + +namespace InventoryManagement +{ + internal class Program + { + static void Main(string[] args) + { + using (var context = new InventoryContext()) + { + context.Database.EnsureCreated(); + } + + var InventoryManager = new InventoryManager(); + while (true) + { + Console.WriteLine(); + Console.WriteLine("Pease Enter the command."); + Console.WriteLine("(show: Show current Inventory, add: Add new product, buy: Buy product, sell: Sell product, acc: Add accessary)"); + Console.Write("CMD> "); + string cmd = Console.ReadLine().ToLower(); + switch (cmd) + { + case "show": + InventoryManager.ShowInventory(); + break; + case "add": + { + Console.Write("\tName: "); + string name = Console.ReadLine(); + Console.Write("\tDefault quantity: "); + string qt = Console.ReadLine(); + int quantity = 0; + if (!int.TryParse(qt, out quantity)) + { + Console.WriteLine("Invalid quanitity."); + break; + } + Product product = new Product() { Name = name, Quantity = quantity }; + InventoryManager.AddProduct(product); + break; + } + + case "buy": + { + Console.Write("\tId or Name: "); + string name = Console.ReadLine(); + int id = 0; + bool idAvailable = int.TryParse(name, out id); + Console.Write("\tQuantity: "); + string qt = Console.ReadLine(); + int quantity = 0; + if (!int.TryParse(qt, out quantity)) + { + Console.WriteLine("Invalid quanitity."); + break; + } + if (idAvailable) + InventoryManager.BuyProduct(id, quantity); + else + InventoryManager.BuyProduct(name, quantity); + break; + } + case "sell": + { + Console.Write("\tId or Name: "); + string name = Console.ReadLine(); + int id = 0; + bool idAvailable = int.TryParse(name, out id); + Console.Write("\tQuantity: "); + string qt = Console.ReadLine(); + int quantity = 0; + if (!int.TryParse(qt, out quantity)) + { + Console.WriteLine("Invalid quanitity."); + break; + } + if (idAvailable) + InventoryManager.SellProduct(id, quantity); + else + InventoryManager.SellProduct(name, quantity); + break; + } + case "acc": + { + Console.Write("\tProduct id: "); + string productId = Console.ReadLine(); + int pId = int.Parse(productId); + Console.Write("\tAccessary name: "); + string name = Console.ReadLine(); + Console.Write("\tAccessary description: "); + string desc = Console.ReadLine(); + InventoryManager.AddAccessary(pId, name, desc); + break; + } + } + + } + } + } +} \ No newline at end of file diff --git a/MySolution/MySolution.sln b/MySolution/MySolution.sln index 874fdff..b21c067 100644 --- a/MySolution/MySolution.sln +++ b/MySolution/MySolution.sln @@ -19,7 +19,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpStreamingApp", "HttpStr EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InventoryManagement", "InventoryManagement\InventoryManagement.csproj", "{CDF5F490-9F8C-4CB2-AA5D-2460CBD43CC1}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EF_ChangeLog", "EF_ChangeLog\EF_ChangeLog.csproj", "{B3039951-7560-4D50-93AE-7E1B57AB8EA6}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EF_ChangeLog", "EF_ChangeLog\EF_ChangeLog.csproj", "{B3039951-7560-4D50-93AE-7E1B57AB8EA6}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InventoryManagement_MariaDB", "InventoryManagement_MariaDB\InventoryManagement_MariaDB.csproj", "{EF6B4197-EAF3-4F13-AA42-F25110F82D3E}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -63,6 +65,10 @@ Global {B3039951-7560-4D50-93AE-7E1B57AB8EA6}.Debug|Any CPU.Build.0 = Debug|Any CPU {B3039951-7560-4D50-93AE-7E1B57AB8EA6}.Release|Any CPU.ActiveCfg = Release|Any CPU {B3039951-7560-4D50-93AE-7E1B57AB8EA6}.Release|Any CPU.Build.0 = Release|Any CPU + {EF6B4197-EAF3-4F13-AA42-F25110F82D3E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EF6B4197-EAF3-4F13-AA42-F25110F82D3E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EF6B4197-EAF3-4F13-AA42-F25110F82D3E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EF6B4197-EAF3-4F13-AA42-F25110F82D3E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE