Вроде сделал

This commit is contained in:
Artyom_Yashin 2024-03-25 22:20:28 +04:00
parent d9f8e1d924
commit 1ceed0d4e0
8 changed files with 522 additions and 4 deletions

View File

@ -9,7 +9,7 @@ namespace ComputersShopDatabaseImplement
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=ComputersShopDataBase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"
optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=ComputersShopDataBaseHard;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"
);
}
base.OnConfiguring(optionsBuilder);
@ -18,5 +18,7 @@ namespace ComputersShopDatabaseImplement
public virtual DbSet<Computer> Computers { set; get; }
public virtual DbSet<ComputerComponent> ComputerComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Shop> Shops { set; get; }
public virtual DbSet<ShopComputer> ShopComputers { set; get; }
}
}

View File

@ -0,0 +1,153 @@
using ComputersShopContracts.BindingModels;
using ComputersShopContracts.SearchModels;
using ComputersShopContracts.StoragesContracts;
using ComputersShopContracts.ViewModels;
using ComputersShopDatabaseImplement.Models;
using ComputersShopDataModels;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputersShopDatabaseImplement.Implements
{
public class ShopStorage : IShopStorage
{
public ShopViewModel? Delete(ShopBindingModel model)
{
using var context = new ComputersShopDatabase();
var element = context.Shops.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
context.Shops.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public ShopViewModel? GetElement(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
{
return null;
}
using var context = new ComputersShopDatabase();
return context.Shops
.Include(x => x.Computers)
.ThenInclude(x => x.Computer)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new ComputersShopDatabase();
return context.Shops
.Include(x => x.Computers)
.ThenInclude(x => x.Computer)
.Select(x => x.GetViewModel)
.Where(x => x.ShopName.Contains(model.Name ?? string.Empty))
.ToList();
}
public List<ShopViewModel> GetFullList()
{
using var context = new ComputersShopDatabase();
return context.Shops
.Include(x => x.Computers)
.ThenInclude(x => x.Computer)
.Select(x => x.GetViewModel)
.ToList();
}
public ShopViewModel? Insert(ShopBindingModel model)
{
using var context = new ComputersShopDatabase();
try
{
var newShop = Shop.Create(context, model);
if (newShop == null)
{
return null;
}
if (context.Shops.Any(x => x.ShopName == newShop.ShopName))
{
throw new Exception("Не должно быть два магазина с одним названием");
}
context.Shops.Add(newShop);
context.SaveChanges();
return newShop.GetViewModel;
}
catch
{
throw;
}
}
public ShopViewModel? Update(ShopBindingModel model)
{
using var context = new ComputersShopDatabase();
var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id);
if (shop == null)
{
return null;
}
try
{
if (context.Shops.Any(x => (x.ShopName == model.ShopName && x.Id != model.Id)))
{
throw new Exception("Не должно быть два магазина с одним названием");
}
shop.Update(model);
shop.UpdateComputers(context, model);
context.SaveChanges();
return shop.GetViewModel;
}
catch
{
throw;
}
}
public bool SellComputers(IComputerModel model, int count)
{
if (model == null)
return false;
using var context = new ComputersShopDatabase();
using var transaction = context.Database.BeginTransaction();
List<ShopComputer> lst = new List<ShopComputer>();
foreach (var el in context.ShopComputers.Where(x => x.ComputerId == model.Id))
{
int dif = count;
if (el.Count < dif)
dif = el.Count;
el.Count -= dif;
count -= dif;
if (el.Count == 0)
{
lst.Add(el);
}
if (count == 0)
break;
}
if (count > 0)
{
transaction.Rollback();
return false;
}
foreach (var el in lst)
{
context.ShopComputers.Remove(el);
}
context.SaveChanges();
transaction.Commit();
return true;
}
}
}

View File

@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace ComputersShopDatabaseImplement.Migrations
{
[DbContext(typeof(ComputersShopDatabase))]
[Migration("20240325171651_InitialCreate")]
[Migration("20240325181021_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
@ -124,6 +124,59 @@ namespace ComputersShopDatabaseImplement.Migrations
b.ToTable("Orders");
});
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Address")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("DateOpen")
.HasColumnType("datetime2");
b.Property<int>("MaxCapacity")
.HasColumnType("int");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.ShopComputer", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ComputerId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComputerId");
b.HasIndex("ShopId");
b.ToTable("ShopComputers");
});
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.ComputerComponent", b =>
{
b.HasOne("ComputersShopDatabaseImplement.Models.Component", "Component")
@ -145,11 +198,32 @@ namespace ComputersShopDatabaseImplement.Migrations
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Order", b =>
{
b.HasOne("ComputersShopDatabaseImplement.Models.Computer", null)
b.HasOne("ComputersShopDatabaseImplement.Models.Computer", "Computers")
.WithMany("Orders")
.HasForeignKey("ComputerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Computers");
});
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.ShopComputer", b =>
{
b.HasOne("ComputersShopDatabaseImplement.Models.Computer", "Computer")
.WithMany("ShopComputers")
.HasForeignKey("ComputerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("ComputersShopDatabaseImplement.Models.Shop", "Shop")
.WithMany("Computers")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Computer");
b.Navigation("Shop");
});
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Component", b =>
@ -162,6 +236,13 @@ namespace ComputersShopDatabaseImplement.Migrations
b.Navigation("Components");
b.Navigation("Orders");
b.Navigation("ShopComputers");
});
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Computers");
});
#pragma warning restore 612, 618
}

View File

@ -39,6 +39,22 @@ namespace ComputersShopDatabaseImplement.Migrations
table.PrimaryKey("PK_Computers", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Shops",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ShopName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Address = table.Column<string>(type: "nvarchar(max)", nullable: false),
DateOpen = table.Column<DateTime>(type: "datetime2", nullable: false),
MaxCapacity = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Shops", x => x.Id);
});
migrationBuilder.CreateTable(
name: "ComputerComponents",
columns: table => new
@ -90,6 +106,33 @@ namespace ComputersShopDatabaseImplement.Migrations
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "ShopComputers",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ComputerId = table.Column<int>(type: "int", nullable: false),
ShopId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ShopComputers", x => x.Id);
table.ForeignKey(
name: "FK_ShopComputers_Computers_ComputerId",
column: x => x.ComputerId,
principalTable: "Computers",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ShopComputers_Shops_ShopId",
column: x => x.ShopId,
principalTable: "Shops",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_ComputerComponents_ComponentId",
table: "ComputerComponents",
@ -104,6 +147,16 @@ namespace ComputersShopDatabaseImplement.Migrations
name: "IX_Orders_ComputerId",
table: "Orders",
column: "ComputerId");
migrationBuilder.CreateIndex(
name: "IX_ShopComputers_ComputerId",
table: "ShopComputers",
column: "ComputerId");
migrationBuilder.CreateIndex(
name: "IX_ShopComputers_ShopId",
table: "ShopComputers",
column: "ShopId");
}
/// <inheritdoc />
@ -115,11 +168,17 @@ namespace ComputersShopDatabaseImplement.Migrations
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "ShopComputers");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "Computers");
migrationBuilder.DropTable(
name: "Shops");
}
}
}

View File

@ -121,6 +121,59 @@ namespace ComputersShopDatabaseImplement.Migrations
b.ToTable("Orders");
});
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Address")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("DateOpen")
.HasColumnType("datetime2");
b.Property<int>("MaxCapacity")
.HasColumnType("int");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.ShopComputer", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ComputerId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComputerId");
b.HasIndex("ShopId");
b.ToTable("ShopComputers");
});
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.ComputerComponent", b =>
{
b.HasOne("ComputersShopDatabaseImplement.Models.Component", "Component")
@ -142,11 +195,32 @@ namespace ComputersShopDatabaseImplement.Migrations
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Order", b =>
{
b.HasOne("ComputersShopDatabaseImplement.Models.Computer", null)
b.HasOne("ComputersShopDatabaseImplement.Models.Computer", "Computers")
.WithMany("Orders")
.HasForeignKey("ComputerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Computers");
});
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.ShopComputer", b =>
{
b.HasOne("ComputersShopDatabaseImplement.Models.Computer", "Computer")
.WithMany("ShopComputers")
.HasForeignKey("ComputerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("ComputersShopDatabaseImplement.Models.Shop", "Shop")
.WithMany("Computers")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Computer");
b.Navigation("Shop");
});
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Component", b =>
@ -159,6 +233,13 @@ namespace ComputersShopDatabaseImplement.Migrations
b.Navigation("Components");
b.Navigation("Orders");
b.Navigation("ShopComputers");
});
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Computers");
});
#pragma warning restore 612, 618
}

View File

@ -37,6 +37,8 @@ namespace ComputersShopDatabaseImplement.Models
public virtual List<ComputerComponent> Components { get; set; } = new();
[ForeignKey("ComputerId")]
public virtual List<Order> Orders { get; set; } = new();
[ForeignKey("ComputerId")]
public virtual List<ShopComputer> ShopComputers { get; set; } = new();
public static Computer Create(ComputersShopDatabase context, ComputerBindingModel model)
{
return new Computer()

View File

@ -0,0 +1,118 @@
using ComputersShopContracts.BindingModels;
using ComputersShopContracts.ViewModels;
using ComputersShopDataModels;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputersShopDatabaseImplement.Models
{
public class Shop : IShopModel
{
public int Id { get; private set; }
[Required]
public string ShopName { get; private set; } = string.Empty;
[Required]
public string Address { get; private set; } = string.Empty;
[Required]
public DateTime DateOpen { get; private set; }
[Required]
public int MaxCapacity { get; private set; }
private Dictionary<int, (IComputerModel, int)>? _shopComputers =
null;
[NotMapped]
public Dictionary<int, (IComputerModel, int)> ShopComputers
{
get
{
if (_shopComputers == null)
{
_shopComputers = Computers
.ToDictionary(x => x.ComputerId, x =>
(x.Computer as IComputerModel, x.Count));
}
return _shopComputers;
}
}
[ForeignKey("ShopId")]
public virtual List<ShopComputer> Computers { get; set; } = new();
public static Shop? Create(ComputersShopDatabase context, ShopBindingModel model)
{
if (model == null)
return null;
return new Shop()
{
Id = model.Id,
ShopName = model.ShopName,
Address = model.Address,
DateOpen = model.DateOpen,
MaxCapacity = model.MaxCapacity,
Computers = model.ShopComputers.Select(x => new
ShopComputer
{
Computer = context.Computers.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(ShopBindingModel? model)
{
if (model == null)
{
return;
}
ShopName = model.ShopName;
Address = model.Address;
DateOpen = model.DateOpen;
MaxCapacity = model.MaxCapacity;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
Address = Address,
DateOpen = DateOpen,
ShopComputers = ShopComputers,
MaxCapacity = MaxCapacity
};
public void UpdateComputers(ComputersShopDatabase context,
ShopBindingModel model)
{
var shopComputers = context.ShopComputers.Where(rec =>
rec.ShopId == model.Id).ToList();
if (shopComputers != null && shopComputers.Count > 0)
{
context.ShopComputers.RemoveRange(shopComputers.Where(rec
=> !model.ShopComputers.ContainsKey(rec.ShopId)));
context.SaveChanges();
foreach (var updateComputer in shopComputers)
{
updateComputer.Count =
model.ShopComputers[updateComputer.ComputerId].Item2;
model.ShopComputers.Remove(updateComputer.ComputerId);
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == Id);
foreach (var pc in model.ShopComputers)
{
context.ShopComputers.Add(new ShopComputer
{
Shop = shop,
Computer = context.Computers.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_shopComputers = null;
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputersShopDatabaseImplement.Models
{
public class ShopComputer
{
public int Id { get; set; }
[Required]
public int ComputerId { get; set; }
[Required]
public int ShopId { get; set; }
[Required]
public int Count { get; set; }
public virtual Shop Shop { get; set; } = new();
public virtual Computer Computer { get; set; } = new();
}
}