месяц и 3 усл есть :))
This commit is contained in:
parent
ef34305215
commit
b14d279f38
248
SoftwareInstallation/SoftwareInstallationDatabaseImplement/Migrations/20240621171904_Shop.Designer.cs
generated
Normal file
248
SoftwareInstallation/SoftwareInstallationDatabaseImplement/Migrations/20240621171904_Shop.Designer.cs
generated
Normal file
@ -0,0 +1,248 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using SoftwareInstallationDatabaseImplement;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SoftwareInstallationDatabaseImplement.Migrations
|
||||
{
|
||||
[DbContext(typeof(SoftwareInstallationDatabase))]
|
||||
[Migration("20240621171904_Shop")]
|
||||
partial class Shop
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "8.0.3")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||
|
||||
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.Component", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("ComponentName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<double>("Cost")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Components");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.Order", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<DateTime>("DateCreate")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<DateTime?>("DateImplement")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<int>("PackageId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("Status")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<double>("Sum")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("PackageId");
|
||||
|
||||
b.ToTable("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.Package", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("PackageName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<double>("Price")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Packages");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.PackageComponent", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<int>("ComponentId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("PackageId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ComponentId");
|
||||
|
||||
b.HasIndex("PackageId");
|
||||
|
||||
b.ToTable("PackageComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.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>("DateOpening")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<int>("PackageMaxCount")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Shops");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.ShopPackage", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("PackageId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("ShopId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("PackageId");
|
||||
|
||||
b.HasIndex("ShopId");
|
||||
|
||||
b.ToTable("ShopPackages");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.Order", b =>
|
||||
{
|
||||
b.HasOne("SoftwareInstallationDatabaseImplement.Package", "Package")
|
||||
.WithMany("Orders")
|
||||
.HasForeignKey("PackageId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Package");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.PackageComponent", b =>
|
||||
{
|
||||
b.HasOne("SoftwareInstallationDatabaseImplement.Component", "Component")
|
||||
.WithMany("PackageComponents")
|
||||
.HasForeignKey("ComponentId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("SoftwareInstallationDatabaseImplement.Package", "Package")
|
||||
.WithMany("Components")
|
||||
.HasForeignKey("PackageId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Component");
|
||||
|
||||
b.Navigation("Package");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.ShopPackage", b =>
|
||||
{
|
||||
b.HasOne("SoftwareInstallationDatabaseImplement.Package", "Package")
|
||||
.WithMany()
|
||||
.HasForeignKey("PackageId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("SoftwareInstallationDatabaseImplement.Shop", "Shop")
|
||||
.WithMany("Packages")
|
||||
.HasForeignKey("ShopId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Package");
|
||||
|
||||
b.Navigation("Shop");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.Component", b =>
|
||||
{
|
||||
b.Navigation("PackageComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.Package", b =>
|
||||
{
|
||||
b.Navigation("Components");
|
||||
|
||||
b.Navigation("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.Shop", b =>
|
||||
{
|
||||
b.Navigation("Packages");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SoftwareInstallationDatabaseImplement.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class Shop : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Shops",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "int", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
Name = 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),
|
||||
PackageMaxCount = table.Column<int>(type: "int", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Shops", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "ShopPackages",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "int", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
PackageId = 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_ShopPackages", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_ShopPackages_Packages_PackageId",
|
||||
column: x => x.PackageId,
|
||||
principalTable: "Packages",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "FK_ShopPackages_Shops_ShopId",
|
||||
column: x => x.ShopId,
|
||||
principalTable: "Shops",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_ShopPackages_PackageId",
|
||||
table: "ShopPackages",
|
||||
column: "PackageId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_ShopPackages_ShopId",
|
||||
table: "ShopPackages",
|
||||
column: "ShopId");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "ShopPackages");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Shops");
|
||||
}
|
||||
}
|
||||
}
|
@ -121,6 +121,59 @@ namespace SoftwareInstallationDatabaseImplement.Migrations
|
||||
b.ToTable("PackageComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.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>("DateOpening")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<int>("PackageMaxCount")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Shops");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.ShopPackage", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("PackageId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("ShopId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("PackageId");
|
||||
|
||||
b.HasIndex("ShopId");
|
||||
|
||||
b.ToTable("ShopPackages");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.Order", b =>
|
||||
{
|
||||
b.HasOne("SoftwareInstallationDatabaseImplement.Package", "Package")
|
||||
@ -151,6 +204,25 @@ namespace SoftwareInstallationDatabaseImplement.Migrations
|
||||
b.Navigation("Package");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.ShopPackage", b =>
|
||||
{
|
||||
b.HasOne("SoftwareInstallationDatabaseImplement.Package", "Package")
|
||||
.WithMany()
|
||||
.HasForeignKey("PackageId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("SoftwareInstallationDatabaseImplement.Shop", "Shop")
|
||||
.WithMany("Packages")
|
||||
.HasForeignKey("ShopId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Package");
|
||||
|
||||
b.Navigation("Shop");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.Component", b =>
|
||||
{
|
||||
b.Navigation("PackageComponents");
|
||||
@ -162,6 +234,11 @@ namespace SoftwareInstallationDatabaseImplement.Migrations
|
||||
|
||||
b.Navigation("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SoftwareInstallationDatabaseImplement.Shop", b =>
|
||||
{
|
||||
b.Navigation("Packages");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,111 @@
|
||||
using SoftwareInstallationContracts.BindingModels;
|
||||
using SoftwareInstallationContracts.ViewModels;
|
||||
using SoftwareInstallationDataModels;
|
||||
using SoftwareInstallationDataModels.Models;
|
||||
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 SoftwareInstallationDatabaseImplement
|
||||
{
|
||||
public class Shop : IShopModel
|
||||
{
|
||||
[Required]
|
||||
public string Name { get; private set; } = string.Empty;
|
||||
[Required]
|
||||
public string Address { get; private set; } = string.Empty;
|
||||
[Required]
|
||||
public DateTime DateOpening { get; private set; }
|
||||
[Required]
|
||||
public int PackageMaxCount { get; private set; }
|
||||
|
||||
public int Id { get; private set; }
|
||||
|
||||
|
||||
private Dictionary<int, (IPackageModel, int)> _ShopPackages = null;
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IPackageModel, int)> ShopPackages
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_ShopPackages == null)
|
||||
{
|
||||
_ShopPackages = Packages
|
||||
.ToDictionary(recPC => recPC.PackageId, recPC => (recPC.Package as IPackageModel, recPC.Count));
|
||||
}
|
||||
return _ShopPackages;
|
||||
}
|
||||
}
|
||||
|
||||
[ForeignKey("ShopId")]
|
||||
public virtual List<ShopPackage> Packages { get; set; } = new();
|
||||
|
||||
public static Shop Create(SoftwareInstallationDatabase context, ShopBindingModel model)
|
||||
{
|
||||
return new Shop()
|
||||
{
|
||||
Id = model.Id,
|
||||
Name = model.Name,
|
||||
Address = model.Address,
|
||||
DateOpening = model.DateOpening,
|
||||
PackageMaxCount = model.PackageMaxCount,
|
||||
Packages = model.ShopPackages.Select(x => new ShopPackage
|
||||
{
|
||||
Package = context.Packages.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(ShopBindingModel model)
|
||||
{
|
||||
Name = model.Name;
|
||||
Address = model.Address;
|
||||
DateOpening = model.DateOpening;
|
||||
PackageMaxCount = model.PackageMaxCount;
|
||||
}
|
||||
|
||||
public ShopViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
Name = Name,
|
||||
Address = Address,
|
||||
DateOpening = DateOpening,
|
||||
PackageMaxCount = PackageMaxCount,
|
||||
ShopPackages = ShopPackages
|
||||
};
|
||||
|
||||
public void UpdatePackage(SoftwareInstallationDatabase context, ShopBindingModel model)
|
||||
{
|
||||
var ShopPackages = context.ShopPackages.Where(rec => rec.ShopId == model.Id).ToList();
|
||||
if (ShopPackages != null && ShopPackages.Count > 0)
|
||||
{ // удалили те, которых нет в модели
|
||||
context.ShopPackages.RemoveRange(ShopPackages.Where(rec => !model.ShopPackages.ContainsKey(rec.PackageId)));
|
||||
context.SaveChanges();
|
||||
// обновили количество у существующих записей
|
||||
foreach (var updatePackage in ShopPackages)
|
||||
{
|
||||
updatePackage.Count = model.ShopPackages[updatePackage.PackageId].Item2;
|
||||
model.ShopPackages.Remove(updatePackage.PackageId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var Shop = context.Shops.First(x => x.Id == Id);
|
||||
foreach (var sp in model.ShopPackages)
|
||||
{
|
||||
context.ShopPackages.Add(new ShopPackage
|
||||
{
|
||||
Shop = Shop,
|
||||
Package = context.Packages.First(x => x.Id == sp.Key),
|
||||
Count = sp.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_ShopPackages = null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SoftwareInstallationDatabaseImplement
|
||||
{
|
||||
public class ShopPackage
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public int PackageId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int ShopId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
|
||||
public virtual Shop Shop { get; set; } = new();
|
||||
|
||||
public virtual Package Package { get; set; } = new();
|
||||
}
|
||||
}
|
@ -0,0 +1,177 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SoftwareInstallationContracts.BindingModels;
|
||||
using SoftwareInstallationContracts.SearchModels;
|
||||
using SoftwareInstallationContracts.StoragesContracts;
|
||||
using SoftwareInstallationContracts.ViewModels;
|
||||
using SoftwareInstallationDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SoftwareInstallationDatabaseImplement
|
||||
{
|
||||
public class ShopStorage : IShopStorage
|
||||
{
|
||||
public ShopViewModel? Delete(ShopBindingModel model)
|
||||
{
|
||||
using var context = new SoftwareInstallationDatabase();
|
||||
|
||||
var element = context.Shops
|
||||
.Include(x => x.Packages)
|
||||
.FirstOrDefault(rec => rec.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 SoftwareInstallationDatabase();
|
||||
|
||||
return context.Shops
|
||||
.Include(x => x.Packages)
|
||||
.ThenInclude(x => x.Package)
|
||||
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (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 SoftwareInstallationDatabase();
|
||||
|
||||
return context.Shops
|
||||
.Include(x => x.Packages)
|
||||
.ThenInclude(x => x.Package)
|
||||
.Where(x => x.Name.Contains(model.Name))
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<ShopViewModel> GetFullList()
|
||||
{
|
||||
using var context = new SoftwareInstallationDatabase();
|
||||
return context.Shops
|
||||
.Include(x => x.Packages)
|
||||
.ThenInclude(x => x.Package)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public ShopViewModel? Insert(ShopBindingModel model)
|
||||
{
|
||||
using var context = new SoftwareInstallationDatabase();
|
||||
var newShop = Shop.Create(context, model);
|
||||
if (newShop == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Shops.Add(newShop);
|
||||
context.SaveChanges();
|
||||
return newShop.GetViewModel;
|
||||
}
|
||||
|
||||
public bool SellPackage(IPackageModel model, int quantity)
|
||||
{
|
||||
using var context = new SoftwareInstallationDatabase();
|
||||
|
||||
if (context.Shops
|
||||
.Include(x => x.Packages)
|
||||
.ThenInclude(x => x.Package)
|
||||
.ToList()
|
||||
.Where(x => x.ShopPackages.ContainsKey(model.Id)).Select(x => x.PackageMaxCount).Sum() < quantity)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
foreach (var Shop in context.Shops
|
||||
.Include(x => x.Packages)
|
||||
.ThenInclude(x => x.Package)
|
||||
.ToList()
|
||||
.Where(x => x.ShopPackages.ContainsKey(model.Id)))
|
||||
{
|
||||
int countInCurrentShop = Shop.ShopPackages[model.Id].Item2;
|
||||
if (countInCurrentShop <= quantity)
|
||||
{
|
||||
var elem = context.ShopPackages
|
||||
.Where(x => x.PackageId == model.Id)
|
||||
.FirstOrDefault(x => x.ShopId == Shop.Id);
|
||||
context.ShopPackages.Remove(elem);
|
||||
Shop.ShopPackages.Remove(model.Id);
|
||||
quantity -= countInCurrentShop;
|
||||
}
|
||||
else
|
||||
{
|
||||
Shop.ShopPackages[model.Id] = (Shop.ShopPackages[model.Id].Item1, countInCurrentShop - quantity);
|
||||
quantity = 0;
|
||||
Shop.UpdatePackage(context, new()
|
||||
{
|
||||
Id = Shop.Id,
|
||||
ShopPackages = Shop.ShopPackages,
|
||||
});
|
||||
}
|
||||
if (quantity == 0)
|
||||
{
|
||||
context.SaveChanges();
|
||||
transaction.Commit();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
transaction.Rollback();
|
||||
return false;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public ShopViewModel? Update(ShopBindingModel model)
|
||||
{
|
||||
using var context = new SoftwareInstallationDatabase();
|
||||
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);
|
||||
Shop.UpdatePackage(context, model);
|
||||
context.SaveChanges();
|
||||
transaction.Commit();
|
||||
return Shop.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -22,5 +22,7 @@ namespace SoftwareInstallationDatabaseImplement
|
||||
public virtual DbSet<Package> Packages { set; get; }
|
||||
public virtual DbSet<PackageComponent> PackageComponents { set; get; }
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
}
|
||||
public virtual DbSet<Shop> Shops { set; get; }
|
||||
public virtual DbSet<ShopPackage> ShopPackages { set; get; }
|
||||
}
|
||||
}
|
||||
|
@ -18,6 +18,7 @@
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SoftwareInstallationContracts\SoftwareInstallationContracts.csproj" />
|
||||
<ProjectReference Include="..\SoftwareInstallationDataModels\SoftwareInstallationDataModels.csproj" />
|
||||
<ProjectReference Include="..\SoftwareInstallationListImplement\SoftwareInstallationListImplement.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
Loading…
x
Reference in New Issue
Block a user