Готово

This commit is contained in:
Yunusov_Niyaz 2024-03-25 21:16:58 +04:00
parent 2d539bb4fe
commit 538000e257
9 changed files with 514 additions and 9 deletions

View File

@ -23,7 +23,7 @@ namespace CarRepairShopDatabaseImplement.Implements
return new();
}
using var context = new RepairsShopDatabase();
return context.Orders
return context.Orders.Include (x => x.Repair)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
@ -35,12 +35,12 @@ namespace CarRepairShopDatabaseImplement.Implements
return null;
}
using var context = new RepairsShopDatabase();
return context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
return context.Orders.Include(x => x.Repair).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public OrderViewModel? Insert(OrderBindingModel model)
{
using var context = new RepairsShopDatabase();
var newOrder = Order.Create(context, model);
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
@ -52,8 +52,7 @@ namespace CarRepairShopDatabaseImplement.Implements
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new RepairsShopDatabase();
var order = context.Orders.FirstOrDefault(x => x.Id ==
model.Id);
var order = context.Orders.Include(x => x.Repair).FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
@ -65,8 +64,7 @@ namespace CarRepairShopDatabaseImplement.Implements
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new RepairsShopDatabase();
var element = context.Orders.FirstOrDefault(rec => rec.Id ==
model.Id);
var element = context.Orders.Include(x => x.Repair).FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Orders.Remove(element);

View File

@ -0,0 +1,153 @@
using CarRepairShopContracts.BindingModels;
using CarRepairShopContracts.SearchModels;
using CarRepairShopContracts.StoragesContracts;
using CarRepairShopContracts.ViewModels;
using CarRepairShopDatabaseImplement.Models;
using CarRepairShopDataModels.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarRepairShopDatabaseImplement.Implements
{
public class ShopStorage : IShopStorage
{
public ShopViewModel? Delete(ShopBindingModel model)
{
using var context = new RepairsShopDatabase();
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 RepairsShopDatabase();
return context.Shops
.Include(x => x.Repairs)
.ThenInclude(x => x.Repair)
.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 RepairsShopDatabase();
return context.Shops
.Include(x => x.Repairs)
.ThenInclude(x => x.Repair)
.Select(x => x.GetViewModel)
.Where(x => x.ShopName.Contains(model.Name ?? string.Empty))
.ToList();
}
public List<ShopViewModel> GetFullList()
{
using var context = new RepairsShopDatabase();
return context.Shops
.Include(x => x.Repairs)
.ThenInclude(x => x.Repair)
.Select(x => x.GetViewModel)
.ToList();
}
public ShopViewModel? Insert(ShopBindingModel model)
{
using var context = new RepairsShopDatabase();
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 RepairsShopDatabase();
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.UpdateRepairs(context, model);
context.SaveChanges();
return shop.GetViewModel;
}
catch
{
throw;
}
}
public bool SellRepairs(IRepairModel model, int count)
{
if (model == null)
return false;
using var context = new RepairsShopDatabase();
using var transaction = context.Database.BeginTransaction();
List<ShopRepair> lst = new List<ShopRepair>();
foreach (var el in context.ShopRepairs.Where(x => x.RepairId == 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.ShopRepairs.Remove(el);
}
context.SaveChanges();
transaction.Commit();
return true;
}
}
}

View File

@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace CarRepairShopDatabaseImplement.Migrations
{
[DbContext(typeof(RepairsShopDatabase))]
[Migration("20240324163100_InitialCreate")]
[Migration("20240325165319_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
@ -124,6 +124,59 @@ namespace CarRepairShopDatabaseImplement.Migrations
b.ToTable("RepairComponents");
});
modelBuilder.Entity("CarRepairShopDatabaseImplement.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("CarRepairShopDatabaseImplement.Models.ShopRepair", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("RepairId")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("RepairId");
b.HasIndex("ShopId");
b.ToTable("ShopRepairs");
});
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Order", b =>
{
b.HasOne("CarRepairShopDatabaseImplement.Models.Repair", "Repair")
@ -154,6 +207,25 @@ namespace CarRepairShopDatabaseImplement.Migrations
b.Navigation("Repair");
});
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.ShopRepair", b =>
{
b.HasOne("CarRepairShopDatabaseImplement.Models.Repair", "Repair")
.WithMany("ShopRepairs")
.HasForeignKey("RepairId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("CarRepairShopDatabaseImplement.Models.Shop", "Shop")
.WithMany("Repairs")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Repair");
b.Navigation("Shop");
});
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Component", b =>
{
b.Navigation("RepairComponents");
@ -164,6 +236,13 @@ namespace CarRepairShopDatabaseImplement.Migrations
b.Navigation("Components");
b.Navigation("Orders");
b.Navigation("ShopRepairs");
});
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Repairs");
});
#pragma warning restore 612, 618
}

View File

@ -39,6 +39,22 @@ namespace CarRepairShopDatabaseImplement.Migrations
table.PrimaryKey("PK_Repairs", 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: "Orders",
columns: table => new
@ -90,6 +106,33 @@ namespace CarRepairShopDatabaseImplement.Migrations
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "ShopRepairs",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
RepairId = 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_ShopRepairs", x => x.Id);
table.ForeignKey(
name: "FK_ShopRepairs_Repairs_RepairId",
column: x => x.RepairId,
principalTable: "Repairs",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ShopRepairs_Shops_ShopId",
column: x => x.ShopId,
principalTable: "Shops",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Orders_RepairId",
table: "Orders",
@ -104,6 +147,16 @@ namespace CarRepairShopDatabaseImplement.Migrations
name: "IX_RepairComponents_RepairId",
table: "RepairComponents",
column: "RepairId");
migrationBuilder.CreateIndex(
name: "IX_ShopRepairs_RepairId",
table: "ShopRepairs",
column: "RepairId");
migrationBuilder.CreateIndex(
name: "IX_ShopRepairs_ShopId",
table: "ShopRepairs",
column: "ShopId");
}
/// <inheritdoc />
@ -115,11 +168,17 @@ namespace CarRepairShopDatabaseImplement.Migrations
migrationBuilder.DropTable(
name: "RepairComponents");
migrationBuilder.DropTable(
name: "ShopRepairs");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "Repairs");
migrationBuilder.DropTable(
name: "Shops");
}
}
}

View File

@ -121,6 +121,59 @@ namespace CarRepairShopDatabaseImplement.Migrations
b.ToTable("RepairComponents");
});
modelBuilder.Entity("CarRepairShopDatabaseImplement.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("CarRepairShopDatabaseImplement.Models.ShopRepair", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("RepairId")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("RepairId");
b.HasIndex("ShopId");
b.ToTable("ShopRepairs");
});
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Order", b =>
{
b.HasOne("CarRepairShopDatabaseImplement.Models.Repair", "Repair")
@ -151,6 +204,25 @@ namespace CarRepairShopDatabaseImplement.Migrations
b.Navigation("Repair");
});
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.ShopRepair", b =>
{
b.HasOne("CarRepairShopDatabaseImplement.Models.Repair", "Repair")
.WithMany("ShopRepairs")
.HasForeignKey("RepairId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("CarRepairShopDatabaseImplement.Models.Shop", "Shop")
.WithMany("Repairs")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Repair");
b.Navigation("Shop");
});
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Component", b =>
{
b.Navigation("RepairComponents");
@ -161,6 +233,13 @@ namespace CarRepairShopDatabaseImplement.Migrations
b.Navigation("Components");
b.Navigation("Orders");
b.Navigation("ShopRepairs");
});
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Repairs");
});
#pragma warning restore 612, 618
}

View File

@ -32,6 +32,8 @@ namespace CarRepairShopDatabaseImplement.Models
public virtual List<RepairComponent> Components { get; set; } = new();
[ForeignKey("RepairId")]
public virtual List<Order> Orders { get; set; } = new();
[ForeignKey("RepairId")]
public virtual List<ShopRepair> ShopRepairs { get; set; } = new();
public static Repair Create(RepairsShopDatabase context, RepairBindingModel model)
{
return new Repair()

View File

@ -0,0 +1,111 @@
using CarRepairShopContracts.BindingModels;
using CarRepairShopContracts.ViewModels;
using CarRepairShopDataModels;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using CarRepairShopDataModels.Models;
namespace CarRepairShopDatabaseImplement.Models
{
public class Shop : IShopModel
{
public int Id { get; private set; }
[Required]
public string ShopName { get; private set; }
[Required]
public string Address { get; private set; }
[Required]
public DateTime DateOpen { get; private set; }
[Required]
public int MaxCapacity { get; private set; }
private Dictionary<int, (IRepairModel, int)>? _shopRepairs =
null;
[NotMapped]
public Dictionary<int, (IRepairModel, int)> ShopRepairs
{
get
{
if (_shopRepairs == null)
{
_shopRepairs = Repairs
.ToDictionary(x => x.RepairId, x =>
(x.Repair as IRepairModel, x.Count));
}
return _shopRepairs;
}
}
[ForeignKey("ShopId")]
public virtual List<ShopRepair> Repairs { get; set; } = new();
public static Shop? Create(RepairsShopDatabase 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,
Repairs = model.ShopRepairs.Select(x => new ShopRepair
{
Repair = context.Repairs.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,
ShopRepairs = ShopRepairs,
MaxCapacity = MaxCapacity
};
public void UpdateRepairs(RepairsShopDatabase context, ShopBindingModel model)
{
var shopRepairs = context.ShopRepairs.Where(rec =>
rec.ShopId == model.Id).ToList();
if (shopRepairs != null && shopRepairs.Count > 0)
{
context.ShopRepairs.RemoveRange(shopRepairs.Where(rec => !model.ShopRepairs.ContainsKey(rec.ShopId)));
context.SaveChanges();
foreach (var updateRepair in shopRepairs)
{
updateRepair.Count =
model.ShopRepairs[updateRepair.RepairId].Item2;
model.ShopRepairs.Remove(updateRepair.RepairId);
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == Id);
foreach (var pc in model.ShopRepairs)
{
context.ShopRepairs.Add(new ShopRepair
{
Shop = shop,
Repair = context.Repairs.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_shopRepairs = 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 CarRepairShopDatabaseImplement.Models
{
public class ShopRepair
{
public int Id { get; set; }
[Required]
public int RepairId { get; set; }
[Required]
public int ShopId { get; set; }
[Required]
public int Count { get; set; }
public virtual Shop Shop { get; set; } = new();
public virtual Repair Repair { get; set; } = new();
}
}

View File

@ -9,7 +9,7 @@ namespace CarRepairShopDatabaseImplement
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=RepairsShopDataBase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"
optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=RepairsShopDataBaseHard;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"
);
}
base.OnConfiguring(optionsBuilder);
@ -18,5 +18,7 @@ namespace CarRepairShopDatabaseImplement
public virtual DbSet<Repair> Repairs { set; get; }
public virtual DbSet<RepairComponent> RepairComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Shop> Shops { set; get; }
public virtual DbSet<ShopRepair> ShopRepairs { set; get; }
}
}