не успел дотестить, потому что мсскл сдох (жаль что не я)
This commit is contained in:
parent
2483d90b45
commit
085fac1e22
@ -23,5 +23,7 @@ namespace ComputersShopDatabaseImplements
|
||||
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 { get; set; }
|
||||
public virtual DbSet<ShopComputer> ShopComputers { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,164 @@
|
||||
using ComputersShopContracts.BindingModels;
|
||||
using ComputersShopContracts.SearchModels;
|
||||
using ComputersShopContracts.StoragesContracts;
|
||||
using ComputersShopContracts.ViewModels;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ComputersShopDatabaseImplements.Models;
|
||||
using ComputersShopDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ComputersShopDatabaseImplements.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> GetFiltredList(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.UpdateDresses(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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -12,8 +12,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
namespace ComputersShopDatabaseImplements.Migrations
|
||||
{
|
||||
[DbContext(typeof(ComputersShopDatabase))]
|
||||
[Migration("20240308192454_InitialCreate")]
|
||||
partial class InitialCreate
|
||||
[Migration("20240517011907_iwantsleep")]
|
||||
partial class iwantsleep
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
@ -124,6 +124,59 @@ namespace ComputersShopDatabaseImplements.Migrations
|
||||
b.ToTable("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ComputersShopDatabaseImplements.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>("MaxCount")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("ShopName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Shops");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ComputersShopDatabaseImplements.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("ComputersShopDatabaseImplements.Models.ComputerComponent", b =>
|
||||
{
|
||||
b.HasOne("ComputersShopDatabaseImplements.Models.Component", "Component")
|
||||
@ -154,6 +207,25 @@ namespace ComputersShopDatabaseImplements.Migrations
|
||||
b.Navigation("Computer");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ComputersShopDatabaseImplements.Models.ShopComputer", b =>
|
||||
{
|
||||
b.HasOne("ComputersShopDatabaseImplements.Models.Computer", "Computer")
|
||||
.WithMany()
|
||||
.HasForeignKey("ComputerId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("ComputersShopDatabaseImplements.Models.Shop", "Shop")
|
||||
.WithMany("Computers")
|
||||
.HasForeignKey("ShopId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Computer");
|
||||
|
||||
b.Navigation("Shop");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ComputersShopDatabaseImplements.Models.Component", b =>
|
||||
{
|
||||
b.Navigation("ComputerComponents");
|
||||
@ -165,6 +237,11 @@ namespace ComputersShopDatabaseImplements.Migrations
|
||||
|
||||
b.Navigation("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ComputersShopDatabaseImplements.Models.Shop", b =>
|
||||
{
|
||||
b.Navigation("Computers");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
|
||||
namespace ComputersShopDatabaseImplements.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class InitialCreate : Migration
|
||||
public partial class iwantsleep : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
@ -39,6 +39,22 @@ namespace ComputersShopDatabaseImplements.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),
|
||||
MaxCount = table.Column<int>(type: "int", nullable: false),
|
||||
DateOpen = table.Column<DateTime>(type: "datetime2", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Shops", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "ComputerComponents",
|
||||
columns: table => new
|
||||
@ -90,6 +106,33 @@ namespace ComputersShopDatabaseImplements.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 ComputersShopDatabaseImplements.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 ComputersShopDatabaseImplements.Migrations
|
||||
migrationBuilder.DropTable(
|
||||
name: "Orders");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "ShopComputers");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Components");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Computers");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Shops");
|
||||
}
|
||||
}
|
||||
}
|
@ -121,6 +121,59 @@ namespace ComputersShopDatabaseImplements.Migrations
|
||||
b.ToTable("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ComputersShopDatabaseImplements.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>("MaxCount")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("ShopName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Shops");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ComputersShopDatabaseImplements.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("ComputersShopDatabaseImplements.Models.ComputerComponent", b =>
|
||||
{
|
||||
b.HasOne("ComputersShopDatabaseImplements.Models.Component", "Component")
|
||||
@ -151,6 +204,25 @@ namespace ComputersShopDatabaseImplements.Migrations
|
||||
b.Navigation("Computer");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ComputersShopDatabaseImplements.Models.ShopComputer", b =>
|
||||
{
|
||||
b.HasOne("ComputersShopDatabaseImplements.Models.Computer", "Computer")
|
||||
.WithMany()
|
||||
.HasForeignKey("ComputerId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("ComputersShopDatabaseImplements.Models.Shop", "Shop")
|
||||
.WithMany("Computers")
|
||||
.HasForeignKey("ShopId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Computer");
|
||||
|
||||
b.Navigation("Shop");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ComputersShopDatabaseImplements.Models.Component", b =>
|
||||
{
|
||||
b.Navigation("ComputerComponents");
|
||||
@ -162,6 +234,11 @@ namespace ComputersShopDatabaseImplements.Migrations
|
||||
|
||||
b.Navigation("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ComputersShopDatabaseImplements.Models.Shop", b =>
|
||||
{
|
||||
b.Navigation("Computers");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
|
101
ComputersShop/ComputersShopDatabaseImplements/Models/Shop.cs
Normal file
101
ComputersShop/ComputersShopDatabaseImplements/Models/Shop.cs
Normal file
@ -0,0 +1,101 @@
|
||||
using ComputersShopContracts.BindingModels;
|
||||
using ComputersShopContracts.ViewModels;
|
||||
using ComputersShopDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ComputersShopDatabaseImplements.Models
|
||||
{
|
||||
public class Shop: IShopModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string ShopName { get; set;} = string.Empty;
|
||||
public string Address { get; set;} = string.Empty;
|
||||
public int MaxCount { get; set;}
|
||||
public DateTime DateOpen { get; set; }
|
||||
public 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,
|
||||
MaxCount = model.MaxCount,
|
||||
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;
|
||||
MaxCount = model.MaxCount;
|
||||
}
|
||||
public ShopViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
ShopName = ShopName,
|
||||
Address = Address,
|
||||
DateOpen = DateOpen,
|
||||
MaxCount = MaxCount,
|
||||
ShopComputers = ShopComputers
|
||||
};
|
||||
|
||||
public void UpdateDresses(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.ComputerId)));
|
||||
context.SaveChanges();
|
||||
foreach (var uDress in shopComputers)
|
||||
{
|
||||
uDress.Count = model.ShopComputers[uDress.ComputerId].Item2;
|
||||
model.ShopComputers.Remove(uDress.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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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 ComputersShopDatabaseImplements.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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user