PIbd-21. Razubaev SM Lab work 5 hard #13

Closed
Sergey wants to merge 13 commits from lab_5_hard into lab_5
10 changed files with 500 additions and 14 deletions
Showing only changes of commit 70c477002d - Show all commits

View File

@ -13,7 +13,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PlumbingRepairDataModels",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PlumbingRepairListImplement", "PlumbingRepairListImplement\PlumbingRepairListImplement.csproj", "{A5D22B59-19BC-4DB6-A234-24AEA47605C2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PlumbingRepairFileImplement", "PlumbingRepairFileImplement\PlumbingRepairFileImplement.csproj", "{8BB0E6DF-704B-4660-8D5E-4D263CD40C98}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PlumbingRepairFileImplement", "PlumbingRepairFileImplement\PlumbingRepairFileImplement.csproj", "{102B6C63-8EB6-4786-BFEA-88B7DB0DB3F0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PlumbingRepairDatabaseImplement", "PlumbingRepairDatabaseImplement\PlumbingRepairDatabaseImplement.csproj", "{6DDC0CCD-97E0-4CB7-8024-C27DB9AEF97B}"
EndProject
@ -43,10 +43,10 @@ Global
{A5D22B59-19BC-4DB6-A234-24AEA47605C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A5D22B59-19BC-4DB6-A234-24AEA47605C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A5D22B59-19BC-4DB6-A234-24AEA47605C2}.Release|Any CPU.Build.0 = Release|Any CPU
{8BB0E6DF-704B-4660-8D5E-4D263CD40C98}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8BB0E6DF-704B-4660-8D5E-4D263CD40C98}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8BB0E6DF-704B-4660-8D5E-4D263CD40C98}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8BB0E6DF-704B-4660-8D5E-4D263CD40C98}.Release|Any CPU.Build.0 = Release|Any CPU
{102B6C63-8EB6-4786-BFEA-88B7DB0DB3F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{102B6C63-8EB6-4786-BFEA-88B7DB0DB3F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{102B6C63-8EB6-4786-BFEA-88B7DB0DB3F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{102B6C63-8EB6-4786-BFEA-88B7DB0DB3F0}.Release|Any CPU.Build.0 = Release|Any CPU
{6DDC0CCD-97E0-4CB7-8024-C27DB9AEF97B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6DDC0CCD-97E0-4CB7-8024-C27DB9AEF97B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6DDC0CCD-97E0-4CB7-8024-C27DB9AEF97B}.Release|Any CPU.ActiveCfg = Release|Any CPU

View File

@ -0,0 +1,143 @@

using Microsoft.EntityFrameworkCore;
using PlumbingRepairContracts.BindingModels;
using PlumbingRepairContracts.SearchModels;
using PlumbingRepairContracts.StoragesContracts;
using PlumbingRepairContracts.ViewModels;
using PlumbingRepairDatabaseImplement.Models;
using PlumbingRepairDataModels.Models;
namespace PlumbingRepairDatabaseImplement.Implements
{
public class ShopStorage : IShopStorage
{
public List<ShopViewModel> GetFullList()
{
using var context = new PlumbingRepairDatabase();
return context.Shops
.Include(x => x.Works)
.ThenInclude(x => x.Work)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName))
{
return new();
}
using var context = new PlumbingRepairDatabase();
return context.Shops
.Include(x => x.Works)
.ThenInclude(x => x.Work)
.Where(x => x.ShopName.Contains(model.ShopName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public ShopViewModel? GetElement(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue)
{
return null;
}
using var context = new PlumbingRepairDatabase();
return context.Shops
.Include(x => x.Works)
.ThenInclude(x => x.Work)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public ShopViewModel? Insert(ShopBindingModel model)
{
using var context = new PlumbingRepairDatabase();
var newShop = Shop.Create(context, model);
if (newShop == null)
{
return null;
}
context.Shops.Add(newShop);
context.SaveChanges();
return newShop.GetViewModel;
}
public ShopViewModel? Update(ShopBindingModel model)
{
using var context = new PlumbingRepairDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var shop = context.Shops.FirstOrDefault(rec => rec.Id == model.Id);
if (shop == null)
{
return null;
}
shop.Update(model);
context.SaveChanges();
shop.UpdateWorks(context, model);
transaction.Commit();
return shop.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public ShopViewModel? Delete(ShopBindingModel model)
{
using var context = new PlumbingRepairDatabase();
var element = context.Shops
.Include(x => x.Works)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Shops.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public bool SellWork(IWorkModel model, int count)
{
if (model == null)
return false;
using var context = new PlumbingRepairDatabase();
using var transaction = context.Database.BeginTransaction();
foreach (var shopWork in context.ShopWorks.Where(x => x.WorkId == model.Id))
{
int min = Math.Min(shopWork.Count, count);
if (min == shopWork.Count)
{
context.ShopWorks.Remove(shopWork);
}
else
{
shopWork.Count -= min;
}
count -= min;
if (count <= 0)
{
break;
}
}
if (count > 0)
{
transaction.Rollback();
return false;
}
context.SaveChanges();
transaction.Commit();
return true;
}
}
}

View File

@ -12,7 +12,7 @@ using PlumbingRepairDatabaseImplement;
namespace PlumbingRepairDatabaseImplement.Migrations
{
[DbContext(typeof(PlumbingRepairDatabase))]
[Migration("20240308152131_InitialCreate")]
[Migration("20240410163831_InitialCreate")]
partial class InitialCreate
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -77,6 +77,59 @@ namespace PlumbingRepairDatabaseImplement.Migrations
b.ToTable("Orders");
});
modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("Address")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("DateOpening")
.HasColumnType("datetime2");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("maxCountWorks")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.ShopWork", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.Property<int>("WorkId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ShopId");
b.HasIndex("WorkId");
b.ToTable("ShopWorks");
});
modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Work", b =>
{
b.Property<int>("Id")
@ -125,12 +178,31 @@ namespace PlumbingRepairDatabaseImplement.Migrations
modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Order", b =>
{
b.HasOne("PlumbingRepairDatabaseImplement.Models.Work", "Work")
.WithMany("Orders")
.HasForeignKey("WorkId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Work");
});
modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.ShopWork", b =>
{
b.HasOne("PlumbingRepairDatabaseImplement.Models.Shop", "Shop")
.WithMany("Works")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("PlumbingRepairDatabaseImplement.Models.Work", "Work")
.WithMany()
.HasForeignKey("WorkId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Shop");
b.Navigation("Work");
});
@ -158,9 +230,16 @@ namespace PlumbingRepairDatabaseImplement.Migrations
b.Navigation("WorkComponents");
});
modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Works");
});
modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Work", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}

View File

@ -23,6 +23,22 @@ namespace PlumbingRepairDatabaseImplement.Migrations
table.PrimaryKey("PK_Components", 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),
DateOpening = table.Column<DateTime>(type: "datetime2", nullable: false),
maxCountWorks = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Shops", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Works",
columns: table => new
@ -61,6 +77,33 @@ namespace PlumbingRepairDatabaseImplement.Migrations
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "ShopWorks",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
WorkId = 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_ShopWorks", x => x.Id);
table.ForeignKey(
name: "FK_ShopWorks_Shops_ShopId",
column: x => x.ShopId,
principalTable: "Shops",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ShopWorks_Works_WorkId",
column: x => x.WorkId,
principalTable: "Works",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "WorkComponents",
columns: table => new
@ -93,6 +136,16 @@ namespace PlumbingRepairDatabaseImplement.Migrations
table: "Orders",
column: "WorkId");
migrationBuilder.CreateIndex(
name: "IX_ShopWorks_ShopId",
table: "ShopWorks",
column: "ShopId");
migrationBuilder.CreateIndex(
name: "IX_ShopWorks_WorkId",
table: "ShopWorks",
column: "WorkId");
migrationBuilder.CreateIndex(
name: "IX_WorkComponents_ComponentId",
table: "WorkComponents",
@ -109,9 +162,15 @@ namespace PlumbingRepairDatabaseImplement.Migrations
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "ShopWorks");
migrationBuilder.DropTable(
name: "WorkComponents");
migrationBuilder.DropTable(
name: "Shops");
migrationBuilder.DropTable(
name: "Components");

View File

@ -75,6 +75,59 @@ namespace PlumbingRepairDatabaseImplement.Migrations
b.ToTable("Orders");
});
modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("Address")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("DateOpening")
.HasColumnType("datetime2");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("maxCountWorks")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.ShopWork", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.Property<int>("WorkId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ShopId");
b.HasIndex("WorkId");
b.ToTable("ShopWorks");
});
modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Work", b =>
{
b.Property<int>("Id")
@ -132,6 +185,25 @@ namespace PlumbingRepairDatabaseImplement.Migrations
b.Navigation("Work");
});
modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.ShopWork", b =>
{
b.HasOne("PlumbingRepairDatabaseImplement.Models.Shop", "Shop")
.WithMany("Works")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("PlumbingRepairDatabaseImplement.Models.Work", "Work")
.WithMany()
.HasForeignKey("WorkId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Shop");
b.Navigation("Work");
});
modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.WorkComponent", b =>
{
b.HasOne("PlumbingRepairDatabaseImplement.Models.Component", "Component")
@ -156,6 +228,11 @@ namespace PlumbingRepairDatabaseImplement.Migrations
b.Navigation("WorkComponents");
});
modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Works");
});
modelBuilder.Entity("PlumbingRepairDatabaseImplement.Models.Work", b =>
{
b.Navigation("Components");

View File

@ -0,0 +1,109 @@

using PlumbingRepairContracts.BindingModels;
using PlumbingRepairContracts.ViewModels;
using PlumbingRepairDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace PlumbingRepairDatabaseImplement.Models
{
public class Shop
{
public int Id { get; set; }
[Required]
public string ShopName { get; set; } = string.Empty;
[Required]
public string Address { get; set; } = string.Empty;
[Required]
public DateTime DateOpening { get; set; }
[Required]
public int maxCountWorks { get; set; }
private Dictionary<int, (IWorkModel, int)>? _shopWorks = null;
[NotMapped]
public Dictionary<int, (IWorkModel, int)> ShopWorks
{
get
{
if (_shopWorks == null)
{
_shopWorks = Works
.ToDictionary(recSW => recSW.WorkId, recSW => (recSW.Work as IWorkModel, recSW.Count));
}
return _shopWorks;
}
}
[ForeignKey("ShopId")]
public virtual List<ShopWork> Works { get; set; } = new();
public static Shop? Create(PlumbingRepairDatabase context, ShopBindingModel? model)
{
if (model == null)
return null;
return new Shop()
{
Id = model.Id,
maxCountWorks = model.maxCountWorks,
Address = model.Address,
ShopName = model.ShopName,
DateOpening = model.DateOpening,
Works = model.ShopWorks.Select(x => new ShopWork
{
Work = context.Works.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;
DateOpening = model.DateOpening;
maxCountWorks = model.maxCountWorks;
}
public ShopViewModel GetViewModel => new()
{
ShopName = ShopName,
Address = Address,
DateOpening = DateOpening,
maxCountWorks = maxCountWorks,
Id = Id,
ShopWorks = ShopWorks
};
public void UpdateWorks(PlumbingRepairDatabase context, ShopBindingModel model)
{
var shopWorks = context.ShopWorks.Where(rec => rec.ShopId == model.Id).ToList();
if (shopWorks != null && shopWorks.Count > 0)
{
context.ShopWorks.RemoveRange(shopWorks.Where(rec => !model.ShopWorks.ContainsKey(rec.WorkId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateWork in shopWorks)
{
updateWork.Count = model.ShopWorks[updateWork.WorkId].Item2;
model.ShopWorks.Remove(updateWork.WorkId);
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == Id);
foreach (var pc in model.ShopWorks)
{
context.ShopWorks.Add(new ShopWork
{
Shop = shop,
Work = context.Works.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_shopWorks = null;
}
}
}

View File

@ -0,0 +1,21 @@
using System.ComponentModel.DataAnnotations;
namespace PlumbingRepairDatabaseImplement.Models
{
public class ShopWork
{
public int Id { get; set; }
[Required]
public int WorkId { get; set; }
[Required]
public int ShopId { get; set; }
[Required]
public int Count { get; set; }
public virtual Work Work { get; set; } = new();
public virtual Shop Shop { get; set; } = new();
}
}

View File

@ -9,7 +9,7 @@ namespace PlumbingRepairDatabaseImplement
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Server=localhost\SQLEXPRESS;Database=master;Trusted_Connection=True;");
optionsBuilder.UseSqlServer(@"Server=localhost\SQLEXPRESS;Database=PlumbingRepairHard;Trusted_Connection=True;");
}
base.OnConfiguring(optionsBuilder);
}
@ -21,5 +21,9 @@ namespace PlumbingRepairDatabaseImplement
public virtual DbSet<WorkComponent> WorkComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Shop> Shops { set; get; }
public virtual DbSet<ShopWork> ShopWorks { set; get; }
}
}

View File

@ -11,8 +11,4 @@
<ProjectReference Include="..\PlumbingRepairDataModels\PlumbingRepairDataModels.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Implements\" />
</ItemGroup>
</Project>

View File

@ -14,10 +14,8 @@
<ItemGroup>
<ProjectReference Include="..\PlumbingRepairBusinessLogic\PlumbingRepairBusinessLogic.csproj" />
<<<<<<< HEAD
=======
<ProjectReference Include="..\PlumbingRepairFileImplement\PlumbingRepairFileImplement.csproj" />
<ProjectReference Include="..\PlumbingRepairDatabaseImplement\PlumbingRepairDatabaseImplement.csproj" />
>>>>>>> laba_3
<ProjectReference Include="..\PlumbingRepairFileImplement\PlumbingRepairFileImplement.csproj" />
<ProjectReference Include="..\PlumbingRepairListImplement\PlumbingRepairListImplement.csproj" />
</ItemGroup>