Compare commits

...

2 Commits

Author SHA1 Message Date
maxnes3
5f45e9a620 Сделаная 3 хард 2023-05-04 22:31:08 +04:00
maxnes3
f124f6ef32 Подготовка 2023-05-04 22:30:49 +04:00
13 changed files with 514 additions and 11 deletions

1
.gitignore vendored
View File

@ -398,3 +398,4 @@ FodyWeavers.xsd
# JetBrains Rider # JetBrains Rider
*.sln.iml *.sln.iml
*.dll

View File

@ -59,7 +59,7 @@ namespace ComputersShopBusinessLogic.BusinessLogics
if (model.Status == OrderStatus.Готов) if (model.Status == OrderStatus.Готов)
{ {
model.DateImplement = DateTime.Now; model.DateImplement = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);
var computer = _computerStorage.GetElement(new() { Id = viewModel.ComputerId }); var computer = _computerStorage.GetElement(new() { Id = viewModel.ComputerId });
if (computer == null) if (computer == null)
{ {

View File

@ -12,7 +12,7 @@ namespace ComputersShopContracts.BindingModels
public int Id { get; set; } public int Id { get; set; }
public string ShopName { get; set; } = string.Empty; public string ShopName { get; set; } = string.Empty;
public string ShopAddress { get; set; } = string.Empty; public string ShopAddress { get; set; } = string.Empty;
public DateTime DateOpening { get; set; } = DateTime.Now; public DateTime DateOpening { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);
public Dictionary<int, (IComputerModel, int)> Computers { get; set; } = new(); public Dictionary<int, (IComputerModel, int)> Computers { get; set; } = new();
public int Capacity { get; set; } public int Capacity { get; set; }
} }

View File

@ -23,7 +23,7 @@ namespace ComputersShopContracts.ViewModels
[DisplayName("Статус")] [DisplayName("Статус")]
public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; public OrderStatus Status { get; set; } = OrderStatus.Неизвестен;
[DisplayName("Дата создания")] [DisplayName("Дата создания")]
public DateTime DateCreate { get; set; } = DateTime.Now; public DateTime DateCreate { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);
[DisplayName("Дата выполнения")] [DisplayName("Дата выполнения")]
public DateTime? DateImplement { get; set; } public DateTime? DateImplement { get; set; }
} }

View File

@ -18,7 +18,7 @@ namespace ComputersShopContracts.ViewModels
[DisplayName("Адрес магазина")] [DisplayName("Адрес магазина")]
public string ShopAddress { get; set; } = string.Empty; public string ShopAddress { get; set; } = string.Empty;
[DisplayName("Дата открытия")] [DisplayName("Дата открытия")]
public DateTime DateOpening { get; set; } = DateTime.Now; public DateTime DateOpening { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);
[DisplayName("Вместимость магазина")] [DisplayName("Вместимость магазина")]
public int Capacity { get; set; } public int Capacity { get; set; }
} }

View File

@ -14,7 +14,7 @@ namespace ComputersShopDataBaseImplement
{ {
if (optionsBuilder.IsConfigured == false) if (optionsBuilder.IsConfigured == false)
{ {
optionsBuilder.UseNpgsql(@"Host=localhost;Port=5432;Database=ComputersShopDatabaseFull;Username=postgres;Password=adam200396789"); optionsBuilder.UseNpgsql(@"Host=localhost;Port=5432;Database=ComputersShopDatabaseHard;Username=postgres;Password=adam200396789");
} }
base.OnConfiguring(optionsBuilder); base.OnConfiguring(optionsBuilder);
} }
@ -22,6 +22,8 @@ namespace ComputersShopDataBaseImplement
public virtual DbSet<Computer> Computers { set; get; } public virtual DbSet<Computer> Computers { set; get; }
public virtual DbSet<ComputerComponent> ComputerComponents { set; get; } public virtual DbSet<ComputerComponent> ComputerComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; } public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<ShopComputer> ShopComputers { set; get; }
public virtual DbSet<Shop> Shops { set; get; }
} }
} }

View File

@ -0,0 +1,154 @@
using ComputersShopContracts.BindingModels;
using ComputersShopContracts.SearchModels;
using ComputersShopContracts.StoragesContracts;
using ComputersShopContracts.ViewModels;
using ComputersShopDataBaseImplement.Models;
using ComputersShopDataModels.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputersShopDataBaseImplement.Implements
{
public class ShopStorage : IShopStorage
{
public ShopViewModel? Delete(ShopBindingModel model)
{
using var context = new ComputersShopDataBase();
var element = context.Shops
.Include(x => x.shopComputers)
.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.ShopName) && !model.Id.HasValue)
{
return null;
}
using var context = new ComputersShopDataBase();
return context.Shops
.Include(x => x.shopComputers)
.ThenInclude(x => x.Computer)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName))
{
return new();
}
using var context = new ComputersShopDataBase();
return context.Shops
.Include(x => x.shopComputers)
.ThenInclude(x => x.Computer)
.Where(x => x.ShopName.Contains(model.ShopName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ShopViewModel> GetFullList()
{
using var context = new ComputersShopDataBase();
return context.Shops
.Include(x => x.shopComputers)
.ThenInclude(x => x.Computer)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public ShopViewModel? Insert(ShopBindingModel model)
{
using var context = new ComputersShopDataBase();
var newComputer = Shop.Create(context, model);
if (newComputer == null)
{
return null;
}
context.Shops.Add(newComputer);
context.SaveChanges();
return newComputer.GetViewModel;
}
public bool SellComputers(IComputerModel model, int quantity)
{
using var context = new ComputersShopDataBase();
using var transaction = context.Database.BeginTransaction();
try
{
List<Shop> shopsWithComputer = context.Shops.Include(x => x.shopComputers).ThenInclude(x => x.Computer).Where(x => x.shopComputers.Any(x => x.ComputerId == model.Id)).ToList();
foreach (var shop in shopsWithComputer)
{
int computerInShopCount = shop.Computers[model.Id].Item2;
if (quantity - computerInShopCount >= 0)
{
quantity -= computerInShopCount;
context.ShopComputers.Remove(shop.shopComputers.FirstOrDefault(x => x.ComputerId == model.Id)!);
shop.Computers.Remove(model.Id);
}
else
{
shop.Computers[model.Id] = (model, computerInShopCount - quantity);
quantity = 0;
shop.UpdateComputers(context, new()
{
Id = shop.Id,
Computers = shop.Computers
});
}
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 ComputersShopDataBase();
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.UpdateComputers(context, model);
transaction.Commit();
return shop.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -12,8 +12,8 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace ComputersShopDataBaseImplement.Migrations namespace ComputersShopDataBaseImplement.Migrations
{ {
[DbContext(typeof(ComputersShopDataBase))] [DbContext(typeof(ComputersShopDataBase))]
[Migration("20230227200236_Init")] [Migration("20230504182237_InitMig")]
partial class Init partial class InitMig
{ {
/// <inheritdoc /> /// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -124,6 +124,59 @@ namespace ComputersShopDataBaseImplement.Migrations
b.ToTable("Orders"); b.ToTable("Orders");
}); });
modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("Capacity")
.HasColumnType("integer");
b.Property<DateTime>("DateOpening")
.HasColumnType("timestamp with time zone");
b.Property<string>("ShopAddress")
.IsRequired()
.HasColumnType("text");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("ComputersShopDataBaseImplement.Models.ShopComputer", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("ComputerId")
.HasColumnType("integer");
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<int>("ShopId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("ComputerId");
b.HasIndex("ShopId");
b.ToTable("ShopComputers");
});
modelBuilder.Entity("ComputersShopDataBaseImplement.Models.ComputerComponent", b => modelBuilder.Entity("ComputersShopDataBaseImplement.Models.ComputerComponent", b =>
{ {
b.HasOne("ComputersShopDataBaseImplement.Models.Component", "Component") b.HasOne("ComputersShopDataBaseImplement.Models.Component", "Component")
@ -145,13 +198,30 @@ namespace ComputersShopDataBaseImplement.Migrations
modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Order", b => modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Order", b =>
{ {
b.HasOne("ComputersShopDataBaseImplement.Models.Computer", "Computer") b.HasOne("ComputersShopDataBaseImplement.Models.Computer", null)
.WithMany("Orders") .WithMany("Orders")
.HasForeignKey("ComputerId") .HasForeignKey("ComputerId")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired(); .IsRequired();
});
modelBuilder.Entity("ComputersShopDataBaseImplement.Models.ShopComputer", b =>
{
b.HasOne("ComputersShopDataBaseImplement.Models.Computer", "Computer")
.WithMany()
.HasForeignKey("ComputerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("ComputersShopDataBaseImplement.Models.Shop", "Shop")
.WithMany("shopComputers")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Computer"); b.Navigation("Computer");
b.Navigation("Shop");
}); });
modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Component", b => modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Component", b =>
@ -165,6 +235,11 @@ namespace ComputersShopDataBaseImplement.Migrations
b.Navigation("Orders"); b.Navigation("Orders");
}); });
modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Shop", b =>
{
b.Navigation("shopComputers");
});
#pragma warning restore 612, 618 #pragma warning restore 612, 618
} }
} }

View File

@ -7,7 +7,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace ComputersShopDataBaseImplement.Migrations namespace ComputersShopDataBaseImplement.Migrations
{ {
/// <inheritdoc /> /// <inheritdoc />
public partial class Init : Migration public partial class InitMig : Migration
{ {
/// <inheritdoc /> /// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder) protected override void Up(MigrationBuilder migrationBuilder)
@ -40,6 +40,22 @@ namespace ComputersShopDataBaseImplement.Migrations
table.PrimaryKey("PK_Computers", x => x.Id); table.PrimaryKey("PK_Computers", x => x.Id);
}); });
migrationBuilder.CreateTable(
name: "Shops",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
ShopName = table.Column<string>(type: "text", nullable: false),
ShopAddress = table.Column<string>(type: "text", nullable: false),
DateOpening = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
Capacity = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Shops", x => x.Id);
});
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "ComputerComponents", name: "ComputerComponents",
columns: table => new columns: table => new
@ -91,6 +107,33 @@ namespace ComputersShopDataBaseImplement.Migrations
onDelete: ReferentialAction.Cascade); onDelete: ReferentialAction.Cascade);
}); });
migrationBuilder.CreateTable(
name: "ShopComputers",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
ShopId = table.Column<int>(type: "integer", nullable: false),
ComputerId = table.Column<int>(type: "integer", nullable: false),
Count = table.Column<int>(type: "integer", 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( migrationBuilder.CreateIndex(
name: "IX_ComputerComponents_ComponentId", name: "IX_ComputerComponents_ComponentId",
table: "ComputerComponents", table: "ComputerComponents",
@ -105,6 +148,16 @@ namespace ComputersShopDataBaseImplement.Migrations
name: "IX_Orders_ComputerId", name: "IX_Orders_ComputerId",
table: "Orders", table: "Orders",
column: "ComputerId"); column: "ComputerId");
migrationBuilder.CreateIndex(
name: "IX_ShopComputers_ComputerId",
table: "ShopComputers",
column: "ComputerId");
migrationBuilder.CreateIndex(
name: "IX_ShopComputers_ShopId",
table: "ShopComputers",
column: "ShopId");
} }
/// <inheritdoc /> /// <inheritdoc />
@ -116,11 +169,17 @@ namespace ComputersShopDataBaseImplement.Migrations
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Orders"); name: "Orders");
migrationBuilder.DropTable(
name: "ShopComputers");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Components"); name: "Components");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Computers"); name: "Computers");
migrationBuilder.DropTable(
name: "Shops");
} }
} }
} }

View File

@ -121,6 +121,59 @@ namespace ComputersShopDataBaseImplement.Migrations
b.ToTable("Orders"); b.ToTable("Orders");
}); });
modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("Capacity")
.HasColumnType("integer");
b.Property<DateTime>("DateOpening")
.HasColumnType("timestamp with time zone");
b.Property<string>("ShopAddress")
.IsRequired()
.HasColumnType("text");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("ComputersShopDataBaseImplement.Models.ShopComputer", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("ComputerId")
.HasColumnType("integer");
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<int>("ShopId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("ComputerId");
b.HasIndex("ShopId");
b.ToTable("ShopComputers");
});
modelBuilder.Entity("ComputersShopDataBaseImplement.Models.ComputerComponent", b => modelBuilder.Entity("ComputersShopDataBaseImplement.Models.ComputerComponent", b =>
{ {
b.HasOne("ComputersShopDataBaseImplement.Models.Component", "Component") b.HasOne("ComputersShopDataBaseImplement.Models.Component", "Component")
@ -142,13 +195,30 @@ namespace ComputersShopDataBaseImplement.Migrations
modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Order", b => modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Order", b =>
{ {
b.HasOne("ComputersShopDataBaseImplement.Models.Computer", "Computer") b.HasOne("ComputersShopDataBaseImplement.Models.Computer", null)
.WithMany("Orders") .WithMany("Orders")
.HasForeignKey("ComputerId") .HasForeignKey("ComputerId")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired(); .IsRequired();
});
modelBuilder.Entity("ComputersShopDataBaseImplement.Models.ShopComputer", b =>
{
b.HasOne("ComputersShopDataBaseImplement.Models.Computer", "Computer")
.WithMany()
.HasForeignKey("ComputerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("ComputersShopDataBaseImplement.Models.Shop", "Shop")
.WithMany("shopComputers")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Computer"); b.Navigation("Computer");
b.Navigation("Shop");
}); });
modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Component", b => modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Component", b =>
@ -162,6 +232,11 @@ namespace ComputersShopDataBaseImplement.Migrations
b.Navigation("Orders"); b.Navigation("Orders");
}); });
modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Shop", b =>
{
b.Navigation("shopComputers");
});
#pragma warning restore 612, 618 #pragma warning restore 612, 618
} }
} }

View File

@ -0,0 +1,109 @@
using ComputersShopContracts.BindingModels;
using ComputersShopContracts.ViewModels;
using ComputersShopDataModels.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;
using System.Net;
namespace ComputersShopDataBaseImplement.Models
{
public class Shop : IShopModel
{
public int Id { get; set; }
[Required]
public string ShopName { get; set; } = string.Empty;
[Required]
public string ShopAddress { get; set; } = string.Empty;
[Required]
public DateTime DateOpening { get; set; }
[Required]
public int Capacity { get; set; }
private Dictionary<int, (IComputerModel, int)>? _computers = null;
[NotMapped]
public Dictionary<int, (IComputerModel, int)> Computers
{
get
{
if (_computers == null)
{
_computers = shopComputers
.ToDictionary(rec => rec.ComputerId,
rec => (rec.Computer as IComputerModel,
rec.Count));
}
return _computers;
}
}
[ForeignKey("ShopId")]
public virtual List<ShopComputer> shopComputers { get; set; } = new();
public static Shop Create(ComputersShopDataBase context, ShopBindingModel model)
{
return new Shop()
{
Id = model.Id,
ShopName = model.ShopName,
ShopAddress = model.ShopAddress,
DateOpening = model.DateOpening,
Capacity = model.Capacity,
shopComputers = model.Computers.Select(x => new ShopComputer
{
Computer = context.Computers.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(ShopBindingModel model)
{
ShopName = model.ShopName;
ShopAddress = model.ShopAddress;
DateOpening = model.DateOpening;
Capacity = model.Capacity;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
ShopAddress = ShopAddress,
DateOpening = DateOpening,
Capacity = Capacity,
Computers = Computers
};
public void UpdateComputers(ComputersShopDataBase context, ShopBindingModel model)
{
var computers = context.ShopComputers.Where(rec => rec.ShopId == model.Id).ToList();
if (computers != null && computers.Count > 0)
{
context.ShopComputers.RemoveRange(computers.Where(rec => !model.Computers.ContainsKey(rec.ComputerId)));
context.SaveChanges();
foreach (var computer in computers)
{
computer.Count = model.Computers[computer.ComputerId].Item2;
model.Computers.Remove(computer.ComputerId);
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == Id);
foreach (var id in model.Computers)
{
context.ShopComputers.Add(new ShopComputer
{
Shop = shop,
Computer = context.Computers.First(x => x.Id == id.Key),
Count = id.Value.Item2
});
context.SaveChanges();
}
_computers = null;
}
}
}

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
namespace ComputersShopDataBaseImplement.Models
{
public class ShopComputer
{
public int Id { get; set; }
[Required]
public int ShopId { get; set; }
[Required]
public int ComputerId { get; set; }
[Required]
public int Count { get; set; }
public virtual Computer Computer { get; set; } = new();
public virtual Shop Shop { get; set; } = new();
}
}

View File

@ -100,7 +100,7 @@ namespace ComputersShopView
Id = _id ?? 0, Id = _id ?? 0,
ShopName = textBoxName.Text, ShopName = textBoxName.Text,
ShopAddress = textBoxAddress.Text, ShopAddress = textBoxAddress.Text,
DateOpening = dateTimePicker.Value.Date, DateOpening = DateTime.SpecifyKind(DateTime.Parse(dateTimePicker.Text), DateTimeKind.Utc),
Capacity = (int)numericUpDownCapacity.Value, Capacity = (int)numericUpDownCapacity.Value,
Computers = _shopComputers Computers = _shopComputers
}; };