Compare commits

..

No commits in common. "0fb113ffbda7539a60fdc3c4c63cfd55bbeb84f6" and "cc30da7d11336edf1c80f7769808d6f69d23008c" have entirely different histories.

5 changed files with 35 additions and 454 deletions

View File

@ -46,31 +46,20 @@ namespace LawFirmDatabaseImplement.Implements
public ShopViewModel? Insert(ShopBindingModel model) public ShopViewModel? Insert(ShopBindingModel model)
{ {
using var context = new LawFirmDatabase(); using var context = new LawFirmDatabase();
using var transaction = context.Database.BeginTransaction(); var newShop = Shop.Create(context, model);
try if (newShop == null)
{ {
var newShop = Shop.Create(context, model); return null;
if (newShop == null)
{
return null;
}
if (context.Shops.Any(x => x.Name == newShop.Name))
{
throw new Exception("Название магазина уже занято");
}
context.Shops.Add(newShop);
context.SaveChanges();
transaction.Commit();
return newShop.GetViewModel;
} }
catch
if (context.Shops.Any(x => x.Name == newShop.Name))
{ {
transaction.Rollback(); throw new Exception("Название магазина уже занято");
throw;
} }
context.Shops.Add(newShop);
context.SaveChanges();
return newShop.GetViewModel;
} }
public ShopViewModel? Update(ShopBindingModel model) public ShopViewModel? Update(ShopBindingModel model)
@ -113,30 +102,7 @@ namespace LawFirmDatabaseImplement.Implements
{ {
using var context = new LawFirmDatabase(); using var context = new LawFirmDatabase();
using var transaction = context.Database.BeginTransaction(); using var transaction = context.Database.BeginTransaction();
foreach (var shopDocs in context.ShopDocuments.Where(x => x.DocumentId == model.Id))
{
var min = Math.Min(count, shopDocs.Count);
shopDocs.Count -= min;
count -= min;
if (count <= 0)
{
break;
}
}
if (count == 0)
{
context.SaveChanges();
transaction.Commit();
}
else
{
transaction.Rollback();
}
if (count > 0)
{
return false;
}
return true;
} }
} }

View File

@ -1,248 +0,0 @@
// <auto-generated />
using System;
using LawFirmDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace LawFirmDatabaseImplement.Migrations
{
[DbContext(typeof(LawFirmDatabase))]
[Migration("20230312152057_ShopsAdded")]
partial class ShopsAdded
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("BlankName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Cost")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Blanks");
});
modelBuilder.Entity("LawFirmDatabaseImplement.Models.Document", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("DocumentName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Documents");
});
modelBuilder.Entity("LawFirmDatabaseImplement.Models.DocumentBlank", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("BlankId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("DocumentId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("BlankId");
b.HasIndex("DocumentId");
b.ToTable("DocumentBlanks");
});
modelBuilder.Entity("LawFirmDatabaseImplement.Models.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>("DocumentId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("DocumentId");
b.ToTable("Orders");
});
modelBuilder.Entity("LawFirmDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Adress")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("MaxCountDocuments")
.HasColumnType("int");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("OpeningDate")
.HasColumnType("datetime2");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("LawFirmDatabaseImplement.Models.ShopDocument", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("DocumentId")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("DocumentId");
b.HasIndex("ShopId");
b.ToTable("ShopDocuments");
});
modelBuilder.Entity("LawFirmDatabaseImplement.Models.DocumentBlank", b =>
{
b.HasOne("LawFirmDatabaseImplement.Models.Blank", "Blank")
.WithMany("DocumentBlanks")
.HasForeignKey("BlankId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("LawFirmDatabaseImplement.Models.Document", "Document")
.WithMany("Blanks")
.HasForeignKey("DocumentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Blank");
b.Navigation("Document");
});
modelBuilder.Entity("LawFirmDatabaseImplement.Models.Order", b =>
{
b.HasOne("LawFirmDatabaseImplement.Models.Document", "Document")
.WithMany("Orders")
.HasForeignKey("DocumentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Document");
});
modelBuilder.Entity("LawFirmDatabaseImplement.Models.ShopDocument", b =>
{
b.HasOne("LawFirmDatabaseImplement.Models.Document", "Document")
.WithMany()
.HasForeignKey("DocumentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("LawFirmDatabaseImplement.Models.Shop", "Shop")
.WithMany("Documents")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Document");
b.Navigation("Shop");
});
modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b =>
{
b.Navigation("DocumentBlanks");
});
modelBuilder.Entity("LawFirmDatabaseImplement.Models.Document", b =>
{
b.Navigation("Blanks");
b.Navigation("Orders");
});
modelBuilder.Entity("LawFirmDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Documents");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -1,78 +0,0 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace LawFirmDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class ShopsAdded : 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),
Adress = table.Column<string>(type: "nvarchar(max)", nullable: false),
OpeningDate = table.Column<DateTime>(type: "datetime2", nullable: false),
MaxCountDocuments = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Shops", x => x.Id);
});
migrationBuilder.CreateTable(
name: "ShopDocuments",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
DocumentId = 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_ShopDocuments", x => x.Id);
table.ForeignKey(
name: "FK_ShopDocuments_Documents_DocumentId",
column: x => x.DocumentId,
principalTable: "Documents",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ShopDocuments_Shops_ShopId",
column: x => x.ShopId,
principalTable: "Shops",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_ShopDocuments_DocumentId",
table: "ShopDocuments",
column: "DocumentId");
migrationBuilder.CreateIndex(
name: "IX_ShopDocuments_ShopId",
table: "ShopDocuments",
column: "ShopId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "ShopDocuments");
migrationBuilder.DropTable(
name: "Shops");
}
}
}

View File

@ -121,59 +121,6 @@ namespace LawFirmDatabaseImplement.Migrations
b.ToTable("Orders"); b.ToTable("Orders");
}); });
modelBuilder.Entity("LawFirmDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Adress")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("MaxCountDocuments")
.HasColumnType("int");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("OpeningDate")
.HasColumnType("datetime2");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("LawFirmDatabaseImplement.Models.ShopDocument", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("DocumentId")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("DocumentId");
b.HasIndex("ShopId");
b.ToTable("ShopDocuments");
});
modelBuilder.Entity("LawFirmDatabaseImplement.Models.DocumentBlank", b => modelBuilder.Entity("LawFirmDatabaseImplement.Models.DocumentBlank", b =>
{ {
b.HasOne("LawFirmDatabaseImplement.Models.Blank", "Blank") b.HasOne("LawFirmDatabaseImplement.Models.Blank", "Blank")
@ -204,25 +151,6 @@ namespace LawFirmDatabaseImplement.Migrations
b.Navigation("Document"); b.Navigation("Document");
}); });
modelBuilder.Entity("LawFirmDatabaseImplement.Models.ShopDocument", b =>
{
b.HasOne("LawFirmDatabaseImplement.Models.Document", "Document")
.WithMany()
.HasForeignKey("DocumentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("LawFirmDatabaseImplement.Models.Shop", "Shop")
.WithMany("Documents")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Document");
b.Navigation("Shop");
});
modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b => modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b =>
{ {
b.Navigation("DocumentBlanks"); b.Navigation("DocumentBlanks");
@ -234,11 +162,6 @@ namespace LawFirmDatabaseImplement.Migrations
b.Navigation("Orders"); b.Navigation("Orders");
}); });
modelBuilder.Entity("LawFirmDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Documents");
});
#pragma warning restore 612, 618 #pragma warning restore 612, 618
} }
} }

View File

@ -119,15 +119,33 @@ namespace LawFirmFileImplement.Implements
var shop = source.Shops[i]; var shop = source.Shops[i];
var documents = shop.ShopDocuments; var documents = shop.ShopDocuments;
foreach (var doc in documents.Where(x => x.Value.Item1.Id == document.Id)) for (int j =1; j < documents.Count + 1; j++)
{ {
var min = Math.Min(doc.Value.Item2, count); if (documents[j].Item1.Id == document.Id)
documents[doc.Value.Item1.Id] = (doc.Value.Item1, doc.Value.Item2 - min); {
count -= min; while (documents[j].Item2 > 0 && count > 0)
{
int tempItem2 = documents[j].Item2;
tempItem2--;
count--;
documents[j] = (documents[j].Item1, tempItem2);
}
}
if (count <= 0) if (count <= 0)
{ {
break; shop.Update(new ShopBindingModel
{
Id = shop.Id,
Name = shop.Name,
Adress = shop.Adress,
OpeningDate = shop.OpeningDate,
MaxCountDocuments = shop.MaxCountDocuments,
ShopDocuments = documents
});
source.SaveShops();
return true;
} }
} }