main
syneffort 2 years ago
parent cbce3bf7ed
commit b36ad8fd09
  1. 18
      MySolution/InventoryManagement_MariaDB/Components/Accessary.cs
  2. 21
      MySolution/InventoryManagement_MariaDB/Components/InventoryContext.cs
  3. 158
      MySolution/InventoryManagement_MariaDB/Components/InventoryManager.cs
  4. 21
      MySolution/InventoryManagement_MariaDB/Components/Product.cs
  5. 24
      MySolution/InventoryManagement_MariaDB/InventoryManagement_MariaDB.csproj
  6. 86
      MySolution/InventoryManagement_MariaDB/Migrations/20231019083443_initialize.Designer.cs
  7. 76
      MySolution/InventoryManagement_MariaDB/Migrations/20231019083443_initialize.cs
  8. 83
      MySolution/InventoryManagement_MariaDB/Migrations/InventoryContextModelSnapshot.cs
  9. 101
      MySolution/InventoryManagement_MariaDB/Program.cs
  10. 8
      MySolution/MySolution.sln

@ -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; }
}
}

@ -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<Product> Products { get; set; }
public DbSet<Accessary> 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);
}
}
}

@ -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("****************************************");
}
}
}
}

@ -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<Accessary> CompatibleAccessaries { get; set; }
}
}

@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateRuntimeConfigurationFiles>True</GenerateRuntimeConfigurationFiles>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.12" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.12">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.12">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="MySql.EntityFrameworkCore" Version="7.0.5" />
</ItemGroup>
</Project>

@ -0,0 +1,86 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("Description")
.HasColumnType("longtext");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext");
b.Property<int>("ProductId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ProductId");
b.ToTable("Accessaries");
});
modelBuilder.Entity("InventoryManagement.Components.Product", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("varchar(255)");
b.Property<int>("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
}
}
}

@ -0,0 +1,76 @@
using Microsoft.EntityFrameworkCore.Migrations;
using MySql.EntityFrameworkCore.Metadata;
#nullable disable
namespace InventoryManagement_MariaDB.Migrations
{
/// <inheritdoc />
public partial class initialize : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterDatabase()
.Annotation("MySQL:Charset", "utf8mb4");
migrationBuilder.CreateTable(
name: "Products",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("MySQL:ValueGenerationStrategy", MySQLValueGenerationStrategy.IdentityColumn),
Name = table.Column<string>(type: "varchar(255)", nullable: false),
Quantity = table.Column<int>(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<int>(type: "int", nullable: false)
.Annotation("MySQL:ValueGenerationStrategy", MySQLValueGenerationStrategy.IdentityColumn),
ProductId = table.Column<int>(type: "int", nullable: false),
Name = table.Column<string>(type: "longtext", nullable: false),
Description = table.Column<string>(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);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Accessaries");
migrationBuilder.DropTable(
name: "Products");
}
}
}

@ -0,0 +1,83 @@
// <auto-generated />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("Description")
.HasColumnType("longtext");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext");
b.Property<int>("ProductId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ProductId");
b.ToTable("Accessaries");
});
modelBuilder.Entity("InventoryManagement.Components.Product", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("varchar(255)");
b.Property<int>("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
}
}
}

@ -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;
}
}
}
}
}
}

@ -19,7 +19,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpStreamingApp", "HttpStr
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InventoryManagement", "InventoryManagement\InventoryManagement.csproj", "{CDF5F490-9F8C-4CB2-AA5D-2460CBD43CC1}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InventoryManagement", "InventoryManagement\InventoryManagement.csproj", "{CDF5F490-9F8C-4CB2-AA5D-2460CBD43CC1}"
EndProject 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 EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.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.ActiveCfg = Release|Any CPU
{B3039951-7560-4D50-93AE-7E1B57AB8EA6}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

Loading…
Cancel
Save