PIbd-23. Radaev A.V. Lab work 03 hard #18

Closed
Arkadiy wants to merge 2 commits from Lab3_hard into Lab2_hard
54 changed files with 4754 additions and 158 deletions

View File

@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GiftShopListImplement", "Gi
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GiftShopBusinessLogic", "GiftShopBusinessLogic\GiftShopBusinessLogic.csproj", "{07C14020-5905-4CCC-9DAC-53507C8F28F8}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GiftShopBusinessLogic", "GiftShopBusinessLogic\GiftShopBusinessLogic.csproj", "{07C14020-5905-4CCC-9DAC-53507C8F28F8}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GiftShopFileImplement", "GiftShopFileImplement\GiftShopFileImplement.csproj", "{05AE2E86-5464-4B1F-BE91-60FA43CE69F1}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GiftShopFileImplement", "GiftShopFileImplement\GiftShopFileImplement.csproj", "{05AE2E86-5464-4B1F-BE91-60FA43CE69F1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GiftShopDatabaseImplement", "GiftShopDatabaseImplement\GiftShopDatabaseImplement.csproj", "{50B7E5D7-A58E-4E36-8A5C-F7E78BB526B0}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -45,6 +47,10 @@ Global
{05AE2E86-5464-4B1F-BE91-60FA43CE69F1}.Debug|Any CPU.Build.0 = Debug|Any CPU {05AE2E86-5464-4B1F-BE91-60FA43CE69F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{05AE2E86-5464-4B1F-BE91-60FA43CE69F1}.Release|Any CPU.ActiveCfg = Release|Any CPU {05AE2E86-5464-4B1F-BE91-60FA43CE69F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{05AE2E86-5464-4B1F-BE91-60FA43CE69F1}.Release|Any CPU.Build.0 = Release|Any CPU {05AE2E86-5464-4B1F-BE91-60FA43CE69F1}.Release|Any CPU.Build.0 = Release|Any CPU
{50B7E5D7-A58E-4E36-8A5C-F7E78BB526B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{50B7E5D7-A58E-4E36-8A5C-F7E78BB526B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{50B7E5D7-A58E-4E36-8A5C-F7E78BB526B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{50B7E5D7-A58E-4E36-8A5C-F7E78BB526B0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -4,7 +4,9 @@ using GiftShopContracts.SearchModels;
using GiftShopContracts.StoragesContracts; using GiftShopContracts.StoragesContracts;
using GiftShopContracts.ViewModels; using GiftShopContracts.ViewModels;
using GiftShopDataModels.Enums; using GiftShopDataModels.Enums;
using GiftShopDataModels.Models;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System.Xml.Linq;
namespace GiftShopBusinessLogic.BusinessLogics namespace GiftShopBusinessLogic.BusinessLogics
{ {
@ -14,10 +16,17 @@ namespace GiftShopBusinessLogic.BusinessLogics
private readonly IOrderStorage _orderStorage; private readonly IOrderStorage _orderStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage) private readonly IShopStorage _shopStorage;
private readonly IShopLogic _shopLogic;
private readonly IGiftStorage _giftStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IGiftStorage giftStorage, IShopLogic shopLogic, IShopStorage shopStorage)
{ {
_logger = logger; _logger = logger;
_orderStorage = orderStorage; _orderStorage = orderStorage;
_giftStorage = giftStorage;
_shopLogic = shopLogic;
_shopStorage = shopStorage;
} }
public bool CreateOrder(OrderBindingModel model) public bool CreateOrder(OrderBindingModel model)
@ -41,37 +50,97 @@ namespace GiftShopBusinessLogic.BusinessLogics
return true; return true;
} }
public bool CheckSupply(IGiftModel gift, int count)
{
if (count <= 0)
{
_logger.LogWarning("Check then supply operation error. Gift count < 0.");
return false;
}
public bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus) int sumCapacity = 0;
int sumCount = 0;
sumCapacity = _shopStorage.GetFullList().Select(x => x.MaxCapacity).Sum();
sumCount = _shopStorage.GetFullList().Select(x => x.ShopGifts.Select(y => y.Value.Item2).Sum()).Sum();
int freeSpace = sumCapacity - sumCount;
if (freeSpace - count < 0)
{
_logger.LogWarning("Check then supply operation error. There's no place for new Gift in shops.");
return false;
}
foreach (var shop in _shopStorage.GetFullList())
{
freeSpace = shop.MaxCapacity;
foreach (var doc in shop.ShopGifts)
{
freeSpace -= doc.Value.Item2;
}
if (freeSpace == 0)
{
continue;
}
if (freeSpace - count >= 0)
{
if (_shopLogic.MakeSupply(new() { Id = shop.Id }, gift, count))
count = 0;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (freeSpace - count < 0)
{
if (_shopLogic.MakeSupply(new() { Id = shop.Id }, gift, freeSpace))
count -= freeSpace;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (count <= 0)
{
return true;
}
}
return false;
}
public bool StatusUpdate(OrderBindingModel model, OrderStatus status)
{ {
var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); CheckModel(model);
//CheckModel(model); var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
if (element == null)
if (model.Status + 1 != newStatus) {
{ _logger.LogWarning("Read operation failed");
_logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect."); return false;
return false; }
} if (element.Status != status - 1)
{
model.Status = newStatus; _logger.LogWarning("Status change operation failed");
throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный");
if (model.Status == OrderStatus.Выдан) }
model.DateImplement = DateTime.Now; if (element.Status == OrderStatus.Готов)
else {
{ var gift = _giftStorage.GetElement(new GiftSearchModel() { Id = model.GiftId });
model.DateImplement = viewModel.DateImplement; if (gift == null)
} {
CheckModel(model); _logger.LogWarning("Status update to " + status.ToString() + " operation failed. Document not found.");
return false;
if (_orderStorage.Update(model) == null) }
{ if (CheckSupply(gift, model.Count) == false)
model.Status--; {
_logger.LogWarning("Update operation failed"); _logger.LogWarning("Status update to " + status.ToString() + " operation failed. Shop supply error.");
return false; return false;
} }
}
return true; model.Status = status;
} if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now;
_orderStorage.Update(model);
return true;
}
public bool TakeOrderInWork(OrderBindingModel model) public bool TakeOrderInWork(OrderBindingModel model)
{ {
@ -110,27 +179,18 @@ namespace GiftShopBusinessLogic.BusinessLogics
{ {
throw new ArgumentNullException(nameof(model)); throw new ArgumentNullException(nameof(model));
} }
if (!withParams) if (!withParams)
{ {
return; return;
} }
if (model.GiftId < 0)
{
throw new ArgumentNullException("Некорректный идентификатор изделия", nameof(model.GiftId));
}
if (model.Count <= 0) if (model.Count <= 0)
{ {
throw new ArgumentNullException("Количество изделий в заказе должно быть больше 0", nameof(model.Count)); throw new ArgumentNullException("Количество изделий в заказе должно быть больше 0", nameof(model.Count));
} }
if (model.Sum <= 0) if (model.Sum <= 0)
{ {
throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum)); throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum));
} }
_logger.LogInformation("Order. OrderId:{Id}.Sum:{ Sum}. EngineId: { EngineId}", model.Id, model.Sum, model.GiftId); _logger.LogInformation("Order. OrderId:{Id}.Sum:{ Sum}. EngineId: { EngineId}", model.Id, model.Sum, model.GiftId);
} }
} }

View File

@ -0,0 +1,236 @@
using Microsoft.Extensions.Logging;
using GiftShopContracts.BindingModels;
using GiftShopContracts.BusinessLogicsContracts;
using GiftShopContracts.SearchModels;
using GiftShopContracts.StoragesContracts;
using GiftShopContracts.ViewModels;
using GiftShopDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GiftShopBusinessLogic.BusinessLogics
{
public class ShopLogic: IShopLogic
{
private readonly ILogger _logger;
private readonly IShopStorage _shopStorage;
public ShopLogic(ILogger<ShopLogic> logger, IShopStorage ShopStorage)
{
_logger = logger;
_shopStorage = ShopStorage;
}
public bool AddGift(ShopSearchModel model, IGiftModel gift, int quantity)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (quantity <= 0)
{
throw new ArgumentException("Количество добавляемого изделия должно быть больше 0", nameof(quantity));
}
_logger.LogInformation("AddGiftInShop. ShopName:{ShopName}.Id:{Id}", model.ShopName, model.Id);
var element = _shopStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("AddGiftInShop element not found");
return false;
}
if (element.MaxCapacity - element.ShopGifts.Select(x => x.Value.Item2).Sum() < quantity)
{
throw new ArgumentNullException("Магазин переполнен", nameof(quantity));
}
_logger.LogInformation("AddGiftInShop find. Id:{Id}", element.Id);
if (element.ShopGifts.TryGetValue(gift.Id, out var pair))
{
element.ShopGifts[gift.Id] = (gift, quantity + pair.Item2);
_logger.LogInformation("AddGiftInShop. Has been added {quantity} {gift} in {ShopName}", quantity, gift.GiftName, element.ShopName);
}
else
{
element.ShopGifts[gift.Id] = (gift, quantity);
_logger.LogInformation("AddGiftInShop. Has been added {quantity} new gift {gift} in {ShopName}", quantity, gift.GiftName, element.ShopName);
}
_shopStorage.Update(new()
{
Id = element.Id,
ShopAdress = element.ShopAdress,
ShopName = element.ShopName,
OpeningDate = element.OpeningDate,
ShopGifts = element.ShopGifts
});
return true;
}
public bool Create(ShopBindingModel model)
{
CheckModel(model);
if (_shopStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(ShopBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_shopStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public bool MakeSell(IGiftModel gift, int count)
{
return _shopStorage.SellGifts(gift, count);
}
public ShopViewModel? ReadElement(ShopSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ShopName:{ShopName}.Id:{ Id}", model.ShopName, model.Id);
var element = _shopStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public List<ShopViewModel>? ReadList(ShopSearchModel? model)
{
_logger.LogInformation("ReadList. ShopName:{ShopName}.Id:{ Id} ", model?.ShopName, model?.Id);
var list = (model == null) ? _shopStorage.GetFullList() : _shopStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool MakeSupply(ShopSearchModel model, IGiftModel gift, int count)
{
if (model == null)
throw new ArgumentNullException(nameof(model));
if (gift == null)
throw new ArgumentNullException(nameof(gift));
if (count <= 0)
throw new ArgumentNullException("Количество должно быть положительным числом");
ShopViewModel curModel = _shopStorage.GetElement(model);
if (curModel == null)
throw new ArgumentNullException(nameof(curModel));
var countItems = curModel.ShopGifts.Select(x => x.Value.Item2).Sum();
if (curModel.MaxCapacity - countItems >= count)
{
if (curModel.ShopGifts.TryGetValue(gift.Id, out var sameDocument))
{
curModel.ShopGifts[gift.Id] = (gift, sameDocument.Item2 + count);
_logger.LogInformation("Same gift found by supply. Added {0} of {1} in {2} shop", count, gift.GiftName, curModel.ShopName);
}
else
{
curModel.ShopGifts[gift.Id] = (gift, count);
_logger.LogInformation("New gift added by supply. Added {0} of {1} in {2} shop", count, gift.GiftName, curModel.ShopName);
}
_shopStorage.Update(new()
{
Id = curModel.Id,
ShopName = curModel.ShopName,
ShopAdress = curModel.ShopAdress,
OpeningDate = curModel.OpeningDate,
ShopGifts = curModel.ShopGifts,
MaxCapacity = curModel.MaxCapacity
});
}
else
{
_logger.LogWarning("Required shop is overflowed");
return false;
}
return true;
}
public bool Update(ShopBindingModel model)
{
CheckModel(model, false);
if (string.IsNullOrEmpty(model.ShopName))
{
throw new ArgumentNullException("Нет названия магазина", nameof(model.ShopName));
}
if (_shopStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(ShopBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.ShopName))
{
throw new ArgumentNullException("Нет названия магазина", nameof(model.ShopName));
}
if (string.IsNullOrEmpty(model.ShopAdress))
{
throw new ArgumentNullException("Нет адресса магазина", nameof(model.ShopAdress));
}
if (model.OpeningDate == null)
{
throw new ArgumentNullException("Нет даты открытия магазина", nameof(model.OpeningDate));
}
_logger.LogInformation("Shop. ShopName:{0}.ShopAddress:{1}. Id: {2}", model.ShopName, model.ShopAdress, model.Id);
var element = _shopStorage.GetElement(new ShopSearchModel
{
ShopName = model.ShopName
});
if (element != null && element.Id != model.Id && element.ShopName == model.ShopName)
{
throw new InvalidOperationException("Магазин с таким названием уже есть");
}
}
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GiftShopDataModels.Models;
namespace GiftShopContracts.BindingModels
{
public class ShopBindingModel : IShopModel
{
public int Id { get; set; }
public string ShopName { get; set; } = string.Empty;
public string ShopAdress { get; set; } = string.Empty;
public DateTime OpeningDate { get; set; } = DateTime.Now;
public int MaxCapacity { get; set; }
public Dictionary<int, (IGiftModel, int)> ShopGifts { get; set; } = new();
}
}

View File

@ -0,0 +1,24 @@
using GiftShopContracts.BindingModels;
using GiftShopContracts.ViewModels;
using GiftShopContracts.SearchModels;
using GiftShopDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GiftShopContracts.BusinessLogicsContracts
{
public interface IShopLogic
{
List<ShopViewModel>? ReadList(ShopSearchModel? model);
ShopViewModel? ReadElement(ShopSearchModel model);
bool Create(ShopBindingModel model);
bool Update(ShopBindingModel model);
bool Delete(ShopBindingModel model);
bool AddGift(ShopSearchModel model, IGiftModel gift, int quantity);
bool MakeSupply(ShopSearchModel model, IGiftModel gift, int count);
bool MakeSell(IGiftModel gift, int count);
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GiftShopContracts.SearchModels
{
public class ShopSearchModel
{
public int? Id { get; set; }
public string? ShopName { get; set; }
}
}

View File

@ -0,0 +1,23 @@
using GiftShopContracts.BindingModels;
using GiftShopContracts.SearchModels;
using GiftShopContracts.ViewModels;
using GiftShopDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GiftShopContracts.StoragesContracts
{
public interface IShopStorage
{
List<ShopViewModel> GetFullList();
List<ShopViewModel> GetFilteredList(ShopSearchModel model);
ShopViewModel? GetElement(ShopSearchModel model);
ShopViewModel? Insert(ShopBindingModel model);
ShopViewModel? Update(ShopBindingModel model);
ShopViewModel? Delete(ShopBindingModel model);
public bool SellGifts(IGiftModel model, int count);
}
}

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GiftShopDataModels.Models;
namespace GiftShopContracts.ViewModels
{
public class ShopViewModel : IShopModel
{
public Dictionary<int, (IGiftModel, int)> ShopGifts { get; set; } = new();
public int Id { get; set; }
[DisplayName("Название магазина")]
public string ShopName { get; set; } = string.Empty;
[DisplayName("Адрес магазина")]
public string ShopAdress { get; set; } = string.Empty;
[DisplayName("Дата открытия")]
public DateTime OpeningDate { get; set; } = DateTime.Now;
[DisplayName("Вместимость")]
public int MaxCapacity { get; set; }
}
}

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GiftShopDataModels.Models
{
public interface IShopModel : IId
{
public int Id { get; set; }
public string ShopName { get; }
public string ShopAdress { get; }
DateTime OpeningDate { get; }
Dictionary<int, (IGiftModel, int)> ShopGifts { get; }
int MaxCapacity { get; }
}
}

View File

@ -0,0 +1,29 @@
using GiftShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace GiftShopDatabaseImplement
{
public class GiftShopDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-Arkad1y;Initial Catalog=GiftShopDataBaseHard;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Gift> Gifts { set; get; }
public virtual DbSet<GiftComponent> GiftComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Shop> Shops { set; get; }
public virtual DbSet<ShopGift> ShopGifts { set; get;}
}
}

View File

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\GiftShopContracts\GiftShopContracts.csproj" />
<ProjectReference Include="..\GiftShopDataModels\GiftShopDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,85 @@
using GiftShopContracts.BindingModels;
using GiftShopContracts.SearchModels;
using GiftShopContracts.StoragesContracts;
using GiftShopContracts.ViewModels;
using GiftShopDatabaseImplement.Models;
namespace GiftShopDatabaseImplement.Implements
{
public class ComponentStorage : IComponentStorage
{
public List<ComponentViewModel> GetFullList()
{
using var context = new GiftShopDatabase();
return context.Components
.Select(x => x.GetViewModel)
.ToList();
}
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName))
{
return new();
}
using var context = new GiftShopDatabase();
return context.Components
.Where(x => x.ComponentName.Contains(model.ComponentName))
.Select(x => x.GetViewModel)
.ToList();
}
public ComponentViewModel? GetElement(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
{
return null;
}
using var context = new GiftShopDatabase();
return context.Components
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public ComponentViewModel? Insert(ComponentBindingModel model)
{
var newComponent = Component.Create(model);
if (newComponent == null)
{
return null;
}
using var context = new GiftShopDatabase();
context.Components.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
using var context = new GiftShopDatabase();
var component = context.Components.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
public ComponentViewModel? Delete(ComponentBindingModel model)
{
using var context = new GiftShopDatabase();
var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Components.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,107 @@
using GiftShopContracts.BindingModels;
using GiftShopContracts.SearchModels;
using GiftShopContracts.StoragesContracts;
using GiftShopContracts.ViewModels;
using GiftShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace GiftShopDatabaseImplement.Implements
{
public class GiftStorage : IGiftStorage
{
public List<GiftViewModel> GetFullList()
{
using var context = new GiftShopDatabase();
return context.Gifts
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<GiftViewModel> GetFilteredList(GiftSearchModel model)
{
if (string.IsNullOrEmpty(model.GiftName))
{
return new();
}
using var context = new GiftShopDatabase();
return context.Gifts
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Where(x => x.GiftName.Contains(model.GiftName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public GiftViewModel? GetElement(GiftSearchModel model)
{
if (string.IsNullOrEmpty(model.GiftName) &&
!model.Id.HasValue)
{
return null;
}
using var context = new GiftShopDatabase();
return context.Gifts
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.GiftName) &&
x.GiftName == model.GiftName) || (model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public GiftViewModel? Insert(GiftBindingModel model)
{
using var context = new GiftShopDatabase();
var newGift = Gift.Create(context, model);
if (newGift == null)
{
return null;
}
context.Gifts.Add(newGift);
context.SaveChanges();
return newGift.GetViewModel;
}
public GiftViewModel? Update(GiftBindingModel model)
{
using var context = new GiftShopDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var gift = context.Gifts.FirstOrDefault(rec => rec.Id == model.Id);
if (gift == null)
{
return null;
}
gift.Update(model);
context.SaveChanges();
gift.UpdateComponents(context, model);
transaction.Commit();
return gift.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public GiftViewModel? Delete(GiftBindingModel model)
{
using var context = new GiftShopDatabase();
var element = context.Gifts
.Include(x => x.Components)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Gifts.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,76 @@
using GiftShopContracts.BindingModels;
using GiftShopContracts.SearchModels;
using GiftShopContracts.StoragesContracts;
using GiftShopContracts.ViewModels;
using GiftShopDatabaseImplement.Models;
namespace GiftShopDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new GiftShopDatabase();
var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new GiftShopDatabase();
return context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new GiftShopDatabase();
return context.Orders.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
}
public List<OrderViewModel> GetFullList()
{
using var context = new GiftShopDatabase();
return context.Orders.Select(x => x.GetViewModel).ToList();
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new GiftShopDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new GiftShopDatabase();
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return order.GetViewModel;
}
}
}

View File

@ -0,0 +1,148 @@
using GiftShopContracts.BindingModels;
using GiftShopContracts.SearchModels;
using GiftShopContracts.StoragesContracts;
using GiftShopContracts.ViewModels;
using GiftShopDataModels.Models;
using GiftShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace GiftShopDatabaseImplement.Implements
{
public class ShopStorage : IShopStorage
{
public ShopViewModel? Delete(ShopBindingModel model)
{
using var context = new GiftShopDatabase();
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.ShopName) && !model.Id.HasValue)
{
return null;
}
using var context = new GiftShopDatabase();
return context.Shops
.Include(x => x.Gifts)
.ThenInclude(x => x.Gift)
.FirstOrDefault(x => 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 GiftShopDatabase();
return context.Shops
.Include(x => x.Gifts)
.ThenInclude(x => x.Gift)
.Select(x => x.GetViewModel)
.Where(x => x.ShopName.Contains(model.ShopName ?? string.Empty))
.ToList();
}
public List<ShopViewModel> GetFullList()
{
using var context = new GiftShopDatabase();
return context.Shops
.Include(x => x.Gifts)
.ThenInclude(x => x.Gift)
.Select(x => x.GetViewModel)
.ToList();
}
public ShopViewModel? Insert(ShopBindingModel model)
{
using var context = new GiftShopDatabase();
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 GiftShopDatabase();
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.UpdateGifts(context, model);
context.SaveChanges();
return shop.GetViewModel;
}
catch
{
throw;
}
}
public bool SellGifts(IGiftModel model, int count)
{
if (model == null)
return false;
using var context = new GiftShopDatabase();
using var transaction = context.Database.BeginTransaction();
List<ShopGift> lst = new List<ShopGift>();
foreach(var el in context.ShopGifts.Where(x => x.GiftId == 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.ShopGifts.Remove(el);
}
context.SaveChanges();
transaction.Commit();
return true;
}
}
}

View File

@ -0,0 +1,252 @@
// <auto-generated />
using System;
using GiftShopDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace GiftShopDatabaseImplement.Migrations
{
[DbContext(typeof(GiftShopDatabase))]
[Migration("20240511053931_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.4")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("GiftShopDatabaseImplement.Models.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("GiftShopDatabaseImplement.Models.Gift", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("GiftName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Gifts");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.GiftComponent", 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>("GiftId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("GiftId");
b.ToTable("GiftComponents");
});
modelBuilder.Entity("GiftShopDatabaseImplement.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>("GiftId")
.HasColumnType("int");
b.Property<string>("GiftName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("GiftId");
b.ToTable("Orders");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("MaxCapacity")
.HasColumnType("int");
b.Property<DateTime>("OpeningDate")
.HasColumnType("datetime2");
b.Property<string>("ShopAdress")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.ShopGift", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("GiftId")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("GiftId");
b.HasIndex("ShopId");
b.ToTable("ShopGifts");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.GiftComponent", b =>
{
b.HasOne("GiftShopDatabaseImplement.Models.Component", "Component")
.WithMany("GiftComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("GiftShopDatabaseImplement.Models.Gift", "Gift")
.WithMany("Components")
.HasForeignKey("GiftId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Gift");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.Order", b =>
{
b.HasOne("GiftShopDatabaseImplement.Models.Gift", "Gift")
.WithMany("Orders")
.HasForeignKey("GiftId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Gift");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.ShopGift", b =>
{
b.HasOne("GiftShopDatabaseImplement.Models.Gift", "Gift")
.WithMany()
.HasForeignKey("GiftId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("GiftShopDatabaseImplement.Models.Shop", "Shop")
.WithMany("Gifts")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Gift");
b.Navigation("Shop");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.Component", b =>
{
b.Navigation("GiftComponents");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.Gift", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Gifts");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,185 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace GiftShopDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Components",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ComponentName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Cost = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Components", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Gifts",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
GiftName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Gifts", 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),
ShopAdress = table.Column<string>(type: "nvarchar(max)", nullable: false),
OpeningDate = 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: "GiftComponents",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
GiftId = table.Column<int>(type: "int", nullable: false),
ComponentId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_GiftComponents", x => x.Id);
table.ForeignKey(
name: "FK_GiftComponents_Components_ComponentId",
column: x => x.ComponentId,
principalTable: "Components",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_GiftComponents_Gifts_GiftId",
column: x => x.GiftId,
principalTable: "Gifts",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "Orders",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
GiftId = table.Column<int>(type: "int", nullable: false),
GiftName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Count = table.Column<int>(type: "int", nullable: false),
Sum = table.Column<double>(type: "float", nullable: false),
Status = table.Column<int>(type: "int", nullable: false),
DateCreate = table.Column<DateTime>(type: "datetime2", nullable: false),
DateImplement = table.Column<DateTime>(type: "datetime2", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Orders", x => x.Id);
table.ForeignKey(
name: "FK_Orders_Gifts_GiftId",
column: x => x.GiftId,
principalTable: "Gifts",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "ShopGifts",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
GiftId = 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_ShopGifts", x => x.Id);
table.ForeignKey(
name: "FK_ShopGifts_Gifts_GiftId",
column: x => x.GiftId,
principalTable: "Gifts",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ShopGifts_Shops_ShopId",
column: x => x.ShopId,
principalTable: "Shops",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_GiftComponents_ComponentId",
table: "GiftComponents",
column: "ComponentId");
migrationBuilder.CreateIndex(
name: "IX_GiftComponents_GiftId",
table: "GiftComponents",
column: "GiftId");
migrationBuilder.CreateIndex(
name: "IX_Orders_GiftId",
table: "Orders",
column: "GiftId");
migrationBuilder.CreateIndex(
name: "IX_ShopGifts_GiftId",
table: "ShopGifts",
column: "GiftId");
migrationBuilder.CreateIndex(
name: "IX_ShopGifts_ShopId",
table: "ShopGifts",
column: "ShopId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "GiftComponents");
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "ShopGifts");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "Gifts");
migrationBuilder.DropTable(
name: "Shops");
}
}
}

View File

@ -0,0 +1,249 @@
// <auto-generated />
using System;
using GiftShopDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace GiftShopDatabaseImplement.Migrations
{
[DbContext(typeof(GiftShopDatabase))]
partial class GiftShopDatabaseModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.4")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("GiftShopDatabaseImplement.Models.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("GiftShopDatabaseImplement.Models.Gift", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("GiftName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Gifts");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.GiftComponent", 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>("GiftId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("GiftId");
b.ToTable("GiftComponents");
});
modelBuilder.Entity("GiftShopDatabaseImplement.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>("GiftId")
.HasColumnType("int");
b.Property<string>("GiftName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("GiftId");
b.ToTable("Orders");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("MaxCapacity")
.HasColumnType("int");
b.Property<DateTime>("OpeningDate")
.HasColumnType("datetime2");
b.Property<string>("ShopAdress")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.ShopGift", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("GiftId")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("GiftId");
b.HasIndex("ShopId");
b.ToTable("ShopGifts");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.GiftComponent", b =>
{
b.HasOne("GiftShopDatabaseImplement.Models.Component", "Component")
.WithMany("GiftComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("GiftShopDatabaseImplement.Models.Gift", "Gift")
.WithMany("Components")
.HasForeignKey("GiftId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Gift");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.Order", b =>
{
b.HasOne("GiftShopDatabaseImplement.Models.Gift", "Gift")
.WithMany("Orders")
.HasForeignKey("GiftId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Gift");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.ShopGift", b =>
{
b.HasOne("GiftShopDatabaseImplement.Models.Gift", "Gift")
.WithMany()
.HasForeignKey("GiftId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("GiftShopDatabaseImplement.Models.Shop", "Shop")
.WithMany("Gifts")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Gift");
b.Navigation("Shop");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.Component", b =>
{
b.Navigation("GiftComponents");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.Gift", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
modelBuilder.Entity("GiftShopDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Gifts");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,63 @@
using GiftShopDataModels.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using GiftShopContracts.ViewModels;
using GiftShopContracts.BindingModels;
namespace GiftShopDatabaseImplement.Models
{
public class Component : IComponentModel
{
public int Id { get; private set; }
[Required]
public string ComponentName { get; private set; } = string.Empty;
[Required]
public double Cost { get; set; }
[ForeignKey("ComponentId")]
public virtual List<GiftComponent> GiftComponents { get; set; } = new();
public static Component? Create(ComponentBindingModel model)
{
if (model == null)
{
return null;
}
return new Component()
{
Id = model.Id,
ComponentName = model.ComponentName,
Cost = model.Cost
};
}
public static Component Create(ComponentViewModel model)
{
return new Component
{
Id = model.Id,
ComponentName = model.ComponentName,
Cost = model.Cost
};
}
public void Update(ComponentBindingModel model)
{
if (model == null)
{
return;
}
ComponentName = model.ComponentName;
Cost = model.Cost;
}
public ComponentViewModel GetViewModel => new()
{
Id = Id,
ComponentName = ComponentName,
Cost = Cost
};
}
}

View File

@ -0,0 +1,99 @@
using GiftShopDataModels.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using GiftShopContracts.BindingModels;
using GiftShopContracts.ViewModels;
namespace GiftShopDatabaseImplement.Models
{
public class Gift : IGiftModel
{
public int Id { get; set; }
[Required]
public string GiftName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _giftComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> GiftComponents
{
get
{
if (_giftComponents == null)
{
_giftComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC =>
(recPC.Component as IComponentModel, recPC.Count));
}
return _giftComponents;
}
}
[ForeignKey("GiftId")]
public virtual List<GiftComponent> Components { get; set; } = new();
[ForeignKey("GiftId")]
public virtual List<Order> Orders { get; set; } = new();
public static Gift Create(GiftShopDatabase context, GiftBindingModel model)
{
return new Gift()
{
Id = model.Id,
GiftName = model.GiftName,
Price = model.Price,
Components = model.GiftComponents.Select(x => new GiftComponent
{
Component = context.Components.First(y => y.Id == x.Key), Count = x.Value.Item2
}).ToList()
};
}
public void Update(GiftBindingModel model)
{
GiftName = model.GiftName;
Price = model.Price;
}
public GiftViewModel GetViewModel => new()
{
Id = Id,
GiftName = GiftName,
Price = Price,
GiftComponents = GiftComponents
};
public void UpdateComponents(GiftShopDatabase context, GiftBindingModel model)
{
var giftComponents = context.GiftComponents.Where(rec => rec.GiftId == model.Id).ToList();
if (giftComponents != null && giftComponents.Count > 0)
{ // удалили те, которых нет в модели
context.GiftComponents.RemoveRange(giftComponents.Where
(rec => !model.GiftComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateComponent in giftComponents)
{
updateComponent.Count = model.GiftComponents[updateComponent.ComponentId].Item2;
model.GiftComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var gift = context.Gifts.First(x => x.Id == Id);
foreach (var pc in model.GiftComponents)
{
context.GiftComponents.Add(new GiftComponent
{
Gift = gift,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_giftComponents = null;
}
}
}

View File

@ -0,0 +1,22 @@
using System.ComponentModel.DataAnnotations;
namespace GiftShopDatabaseImplement.Models
{
public class GiftComponent
{
public int Id { get; set; }
[Required]
public int GiftId { get; set; }
[Required]
public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Component Component { get; set; } = new();
public virtual Gift Gift { get; set; } = new();
}
}

View File

@ -0,0 +1,75 @@
using GiftShopContracts.BindingModels;
using GiftShopContracts.ViewModels;
using GiftShopDataModels.Enums;
using GiftShopDataModels.Models;
using System.ComponentModel.DataAnnotations;
namespace GiftShopDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
public int GiftId { get; private set; }
public string GiftName { get; private set; } = string.Empty;
[Required]
public int Count { get; private set; }
[Required]
public double Sum { get; private set; }
[Required]
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
[Required]
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
public virtual Gift Gift { get; set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
GiftId = model.GiftId,
GiftName = model.GiftName,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
GiftId = GiftId,
GiftName = GiftName,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement
};
}
}

View File

@ -0,0 +1,115 @@
using GiftShopContracts.BindingModels;
using GiftShopContracts.ViewModels;
using GiftShopDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GiftShopDatabaseImplement.Models
{
public class Shop : IShopModel
{
public int Id { get; set; }
[Required]
public string ShopName { get; private set; }
[Required]
public string ShopAdress { get; private set; }
[Required]
public DateTime OpeningDate { get; private set; }
[Required]
public int MaxCapacity { get; private set; }
private Dictionary<int, (IGiftModel, int)>? _shopGifts =
null;
[NotMapped]
public Dictionary<int, (IGiftModel, int)> ShopGifts
{
get
{
if (_shopGifts == null)
{
_shopGifts = Gifts
.ToDictionary(x => x.GiftId, x =>
(x.Gift as IGiftModel, x.Count));
}
return _shopGifts;
}
}
[ForeignKey("ShopId")]
public virtual List<ShopGift> Gifts { get; set; } = new();
public static Shop? Create(GiftShopDatabase context, ShopBindingModel model)
{
if (model == null)
return null;
return new Shop()
{
Id = model.Id,
ShopName = model.ShopName,
ShopAdress = model.ShopAdress,
OpeningDate = model.OpeningDate,
MaxCapacity = model.MaxCapacity,
Gifts = model.ShopGifts.Select(x => new ShopGift
{
Gift = context.Gifts.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(ShopBindingModel? model)
{
if (model == null)
{
return;
}
ShopName = model.ShopName;
ShopAdress = model.ShopAdress;
OpeningDate = model.OpeningDate;
MaxCapacity = model.MaxCapacity;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
ShopAdress = ShopAdress,
OpeningDate = OpeningDate,
ShopGifts = ShopGifts,
MaxCapacity = MaxCapacity
};
public void UpdateGifts(GiftShopDatabase context, ShopBindingModel model)
{
var shopGifts = context.ShopGifts.Where(rec =>
rec.ShopId == model.Id).ToList();
if (shopGifts != null && shopGifts.Count > 0)
{
context.ShopGifts.RemoveRange(shopGifts.Where(rec => !model.ShopGifts.ContainsKey(rec.GiftId)));
context.SaveChanges();
foreach (var updateGift in shopGifts)
{
updateGift.Count =
model.ShopGifts[updateGift.GiftId].Item2;
model.ShopGifts.Remove(updateGift.GiftId);
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == Id);
foreach (var pc in model.ShopGifts)
{
context.ShopGifts.Add(new ShopGift
{
Shop = shop,
Gift = context.Gifts.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_shopGifts = 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 GiftShopDatabaseImplement.Models
{
public class ShopGift
{
public int Id { get; set; }
[Required]
public int GiftId { get; set; }
[Required]
public int ShopId { get; set; }
[Required]
public int Count { get; set; }
public virtual Shop Shop { get; set; } = new();
public virtual Gift Gift { get; set; } = new();
}
}

View File

@ -12,13 +12,16 @@ namespace GiftShopFileImplement
private readonly string OrderFileName = "Order.xml"; private readonly string OrderFileName = "Order.xml";
private readonly string GiftFileName = "Gift.xml"; private readonly string GiftFileName = "Gift.xml";
private readonly string ShopFileName = "Shops.xml";
public List<Component> Components { get; private set; } public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; } public List<Order> Orders { get; private set; }
public List<Gift> Gifts { get; private set; } public List<Gift> Gifts { get; private set; }
public List<Shop> Shops { get; private set; }
public static DataFileSingleton GetInstance() public static DataFileSingleton GetInstance()
{ {
if (instance == null) if (instance == null)
@ -34,12 +37,15 @@ namespace GiftShopFileImplement
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
private DataFileSingleton() public void SaveShops() => SaveData(Shops, OrderFileName, "Shops", x => x.GetXElement);
private DataFileSingleton()
{ {
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
Gifts = LoadData(GiftFileName, "Gift", x => Gift.Create(x)!)!; Gifts = LoadData(GiftFileName, "Gift", x => Gift.Create(x)!)!;
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
} Shops = LoadData(ShopFileName, "Shop", x => Shop.Create(x)!)!;
}
private static List<T>? LoadData<T>(string filename, string xmlNodeName, Func<XElement, T> selectFunction) private static List<T>? LoadData<T>(string filename, string xmlNodeName, Func<XElement, T> selectFunction)
{ {

View File

@ -30,7 +30,8 @@ namespace GiftShopFileImplement.Implements
} }
return source.Components return source.Components
.Where(x => x.ComponentName.Contains(model.ComponentName)) .Where(x => x.ComponentName.Contains(model.ComponentName))
.Select(x => x.GetViewModel).ToList(); .Select(x => x.GetViewModel)
.ToList();
} }
public ComponentViewModel? GetElement(ComponentSearchModel model) public ComponentViewModel? GetElement(ComponentSearchModel model)

View File

@ -0,0 +1,128 @@
using GiftShopContracts.BindingModels;
using GiftShopContracts.SearchModels;
using GiftShopContracts.StoragesContracts;
using GiftShopContracts.ViewModels;
using GiftShopDataModels.Models;
using GiftShopFileImplement;
using GiftShopFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GiftShopFileImplement.Implements
{
public class ShopStorage : IShopStorage
{
private readonly DataFileSingleton _source;
public ShopStorage()
{
_source = DataFileSingleton.GetInstance();
}
public List<ShopViewModel> GetFullList()
{
return _source.Shops.Select(x => x.GetViewModel).ToList();
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName))
{
return new();
}
return _source.Shops.Where(x => x.ShopName.Contains(model.ShopName)).Select(x => x.GetViewModel).ToList(); ;
}
public ShopViewModel? GetElement(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue)
{
return null;
}
return _source.Shops.FirstOrDefault(x =>(!string.IsNullOrEmpty(model.ShopName) && x.ShopName ==model.ShopName) ||
(model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel;
}
public ShopViewModel? Insert(ShopBindingModel model)
{
model.Id = _source.Shops.Count > 0 ? _source.Shops.Max(x =>x.Id) + 1 : 1;
var newShop = Shop.Create(model);
if (newShop == null)
{
return null;
}
_source.Shops.Add(newShop);
_source.SaveShops();
return newShop.GetViewModel;
}
public ShopViewModel? Update(ShopBindingModel model)
{
var component = _source.Shops.FirstOrDefault(x => x.Id ==model.Id);
if (component == null)
{
return null;
}
component.Update(model);
_source.SaveShops();
return component.GetViewModel;
}
public ShopViewModel? Delete(ShopBindingModel model)
{
var element = _source.Shops.FirstOrDefault(x => x.Id ==
model.Id);
if (element != null)
{
_source.Shops.Remove(element);
_source.SaveShops();
return element.GetViewModel;
}
return null;
}
public bool CheckAvailability(int giftId, int count)
{
int minus = _source.Shops.Select(x => x.ShopGifts.Select(y => (y.Value.Item1.Id == giftId ? y.Value.Item2 : 0)).Sum()).Sum();
count -= minus;
return count <= 0;
}
public bool SellGifts(IGiftModel model, int count)
{
var gift = _source.Gifts.FirstOrDefault(x => x.Id == model.Id);
if (gift == null || !CheckAvailability(gift.Id, count))
{
return false;
}
for (int i = 0; i < _source.Shops.Count; i++)
{
var shop = _source.Shops[i];
var gifts = shop.ShopGifts;
foreach (var gifty in gifts.Where(x => x.Value.Item1.Id == gift.Id))
{
var min = Math.Min(gifty.Value.Item2, count);
gifts[gifty.Value.Item1.Id] = (gifty.Value.Item1, gifty.Value.Item2 - min);
count -= min;
if (count <= 0)
{
break;
}
}
shop.Update(new ShopBindingModel
{
Id = shop.Id,
ShopName = shop.ShopName,
ShopAdress = shop.ShopAdress,
OpeningDate = shop.OpeningDate,
MaxCapacity = shop.MaxCapacity,
ShopGifts = gifts
});
}
_source.SaveShops();
return true;
}
}
}

View File

@ -7,7 +7,7 @@ namespace GiftShopFileImplement.Models
{ {
internal class Component : IComponentModel internal class Component : IComponentModel
{ {
public int Id { get; private set; } // 3 public int Id { get; private set; }
public string ComponentName { get; private set; } = string.Empty; public string ComponentName { get; private set; } = string.Empty;

View File

@ -89,6 +89,7 @@ namespace GiftShopFileImplement.Models
new XElement("Price", Price.ToString()), new XElement("Price", Price.ToString()),
new XElement("GiftComponents", Components.Select(x => new XElement("GiftComponent", new XElement("GiftComponents", Components.Select(x => new XElement("GiftComponent",
new XElement("Key", x.Key), new XElement("Key", x.Key),
new XElement("Value", x.Value))).ToArray())); new XElement("Value", x.Value)))
.ToArray()));
} }
} }

View File

@ -10,8 +10,6 @@ namespace GiftShopFileImplement.Models
{ {
public int GiftId { get; private set; } public int GiftId { get; private set; }
public string GiftName { get; private set; }
public int Count { get; private set; } public int Count { get; private set; }
public double Sum { get; private set; } public double Sum { get; private set; }
@ -34,7 +32,6 @@ namespace GiftShopFileImplement.Models
{ {
Id = model.Id, Id = model.Id,
GiftId = model.GiftId, GiftId = model.GiftId,
GiftName = model.GiftName,
Count = model.Count, Count = model.Count,
Sum = model.Sum, Sum = model.Sum,
Status = model.Status, Status = model.Status,
@ -53,7 +50,6 @@ namespace GiftShopFileImplement.Models
{ {
Id = Convert.ToInt32(element.Attribute("Id")!.Value), Id = Convert.ToInt32(element.Attribute("Id")!.Value),
GiftId = Convert.ToInt32(element.Element("GiftId")!.Value), GiftId = Convert.ToInt32(element.Element("GiftId")!.Value),
GiftName = element.Element("GiftName")!.Value,
Count = Convert.ToInt32(element.Element("Count")!.Value), Count = Convert.ToInt32(element.Element("Count")!.Value),
Sum = Convert.ToDouble(element.Element("Sum")!.Value), Sum = Convert.ToDouble(element.Element("Sum")!.Value),
Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value),
@ -72,7 +68,11 @@ namespace GiftShopFileImplement.Models
{ {
return; return;
} }
GiftId = model.GiftId;
Count = model.Count;
Sum = model.Sum;
Status = model.Status; Status = model.Status;
DateCreate = model.DateCreate;
DateImplement = model.DateImplement; DateImplement = model.DateImplement;
} }
@ -80,7 +80,6 @@ namespace GiftShopFileImplement.Models
{ {
Id = Id, Id = Id,
GiftId = GiftId, GiftId = GiftId,
GiftName = GiftName,
Count = Count, Count = Count,
Sum = Sum, Sum = Sum,
Status = Status, Status = Status,
@ -90,7 +89,6 @@ namespace GiftShopFileImplement.Models
public XElement GetXElement => new("Order", public XElement GetXElement => new("Order",
new XAttribute("Id", Id), new XAttribute("Id", Id),
new XElement("GiftName", GiftName),
new XElement("GiftId", GiftId.ToString()), new XElement("GiftId", GiftId.ToString()),
new XElement("Count", Count.ToString()), new XElement("Count", Count.ToString()),
new XElement("Sum", Sum.ToString()), new XElement("Sum", Sum.ToString()),

View File

@ -0,0 +1,104 @@
using GiftShopContracts.BindingModels;
using GiftShopContracts.ViewModels;
using GiftShopDataModels.Models;
using System.Xml.Linq;
namespace GiftShopFileImplement.Models
{
public class Shop : IShopModel
{
public int Id { get; set; }
public string ShopName { get; private set; }
public string ShopAdress { get; private set; }
public DateTime OpeningDate { get; private set; }
public Dictionary<int, int> Gifts { get; private set; } = new();
private Dictionary<int, (IGiftModel, int)>? _shopGifts = null;
public Dictionary<int, (IGiftModel, int)> ShopGifts
{
get
{
if (_shopGifts == null)
{
var source = DataFileSingleton.GetInstance();
_shopGifts = Gifts.ToDictionary(x => x.Key, y =>
((source.Gifts.FirstOrDefault(z => z.Id == y.Key) as IGiftModel)!,
y.Value));
}
return _shopGifts;
}
}
public int MaxCapacity { get; private set; }
public static Shop? Create(ShopBindingModel model)
{
if (model == null)
return null;
return new Shop()
{
Id = model.Id,
ShopName = model.ShopName,
ShopAdress = model.ShopAdress,
OpeningDate = model.OpeningDate,
MaxCapacity = model.MaxCapacity,
Gifts = model.ShopGifts.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Shop? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Shop()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ShopName = element.Element("ShopName")!.Value,
ShopAdress = element.Element("ShopAdress")!.Value,
MaxCapacity = Convert.ToInt32(element.Element("MaxCapacity")!.Value),
OpeningDate = Convert.ToDateTime(element.Element("OpeningDate")!.Value),
Gifts = element.Element("ShopGifts")!.Elements("ShopGifts").ToDictionary(x =>Convert.ToInt32(x.Element("Key")?.Value), x =>Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(ShopBindingModel model)
{
if (model == null)
{
return;
}
ShopName = model.ShopName;
ShopAdress = model.ShopAdress;
OpeningDate = model.OpeningDate;
MaxCapacity = model.MaxCapacity;
if (model.ShopGifts.Count > 0)
{
Gifts = model.ShopGifts.ToDictionary(x => x.Key, x => x.Value.Item2);
_shopGifts = null;
}
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
ShopAdress = ShopAdress,
OpeningDate = OpeningDate,
MaxCapacity = MaxCapacity,
ShopGifts = ShopGifts
};
public XElement GetXElement => new("Shop",
new XAttribute("Id", Id),
new XElement("ShopName", ShopName),
new XElement("ShopAdress", ShopAdress),
new XElement("OpeningDate", OpeningDate),
new XElement("MaxCapacity", MaxCapacity),
new XElement("Gifts", Gifts.Select(x => new XElement("Gifts",
new XElement("Key", x.Key),
new XElement("Value", x.Value))).ToArray()));
}
}

View File

@ -8,12 +8,14 @@ namespace GiftShopListImplement
public List<Component> Components { get; set; } public List<Component> Components { get; set; }
public List<Order> Orders { get; set; } public List<Order> Orders { get; set; }
public List<Gift> Gifts { get; set; } public List<Gift> Gifts { get; set; }
public List<Shop> Shops { get; set; }
private DataListSingleton() private DataListSingleton()
{ {
Components = new List<Component>(); Components = new List<Component>();
Orders = new List<Order>(); Orders = new List<Order>();
Gifts = new List<Gift>(); Gifts = new List<Gift>();
Shops = new List<Shop>();
} }
public static DataListSingleton GetInstance() public static DataListSingleton GetInstance()
{ {

View File

@ -0,0 +1,170 @@
using GiftShopContracts.BindingModels;
using GiftShopContracts.SearchModels;
using GiftShopContracts.StoragesContracts;
using GiftShopContracts.ViewModels;
using GiftShopListImplement.Models;
using GiftShopDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace GiftShopListImplement.Implements
{
public class ShopStorage : IShopStorage
{
private readonly DataListSingleton _source;
public ShopStorage()
{
_source = DataListSingleton.GetInstance();
}
public ShopViewModel? Delete(ShopBindingModel model)
{
for (int i = 0; i < _source.Shops.Count; ++i)
{
if (_source.Shops[i].Id == model.Id)
{
var element = _source.Shops[i];
_source.Shops.RemoveAt(i);
return element.GetViewModel;
}
}
return null;
}
public ShopViewModel? GetElement(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue)
{
return null;
}
foreach (var shop in _source.Shops)
{
if ((!string.IsNullOrEmpty(model.ShopName) && shop.ShopName == model.ShopName) || (model.Id.HasValue && shop.Id == model.Id))
{
return shop.GetViewModel;
}
}
return null;
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
var result = new List<ShopViewModel>();
if (string.IsNullOrEmpty(model.ShopName))
{
return result;
}
foreach (var shop in _source.Shops)
{
if (shop.ShopName.Contains(model.ShopName))
{
result.Add(shop.GetViewModel);
}
}
return result;
}
public List<ShopViewModel> GetFullList()
{
var result = new List<ShopViewModel>();
foreach (var shop in _source.Shops)
{
result.Add(shop.GetViewModel);
}
return result;
}
public ShopViewModel? Insert(ShopBindingModel model)
{
model.Id = 1;
foreach (var shop in _source.Shops)
{
if (model.Id <= shop.Id)
{
model.Id = shop.Id + 1;
}
}
var newShop = Shop.Create(model);
if (newShop == null)
{
return null;
}
_source.Shops.Add(newShop);
return newShop.GetViewModel;
}
public ShopViewModel? Update(ShopBindingModel model)
{
foreach (var shop in _source.Shops)
{
if (shop.Id == model.Id)
{
shop.Update(model);
return shop.GetViewModel;
}
}
return null;
}
public bool CheckAvailability(int giftId, int count)
{
int minus = _source.Shops.Select(x => x.ShopGifts.Select(y => (y.Value.Item1.Id == giftId ? y.Value.Item2 : 0)).Sum()).Sum();
count -= minus;
return count <= 0;
}
public bool SellGifts(IGiftModel model, int count)
{
var gift = _source.Gifts.FirstOrDefault(x => x.Id == model.Id);
if (gift == null || !CheckAvailability(gift.Id, count))
{
return false;
}
for (int i = 0; i < _source.Shops.Count; i++)
{
var shop = _source.Shops[i];
var gifts = shop.ShopGifts;
foreach (var shopgift in gifts.Where(x => x.Value.Item1.Id == gift.Id))
{
var min = Math.Min(shopgift.Value.Item2, count);
gifts[shopgift.Value.Item1.Id] = (shopgift.Value.Item1, shopgift.Value.Item2 - min);
count -= min;
if (count <= 0)
{
break;
}
}
shop.Update(new ShopBindingModel
{
Id = shop.Id,
ShopName = shop.ShopName,
ShopAdress = shop.ShopAdress,
OpeningDate = shop.OpeningDate,
MaxCapacity = shop.MaxCapacity,
ShopGifts = gifts
});
}
return true;
}
}
}

View File

@ -0,0 +1,64 @@
using GiftShopContracts.BindingModels;
using GiftShopContracts.ViewModels;
using GiftShopDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GiftShopListImplement.Models
{
public class Shop : IShopModel
{
public int Id { get; set; }
public string ShopName { get; private set; } = string.Empty;
public string ShopAdress { get; private set; } = string.Empty;
public DateTime OpeningDate { get; private set; }
public int MaxCapacity { get; private set; }
public Dictionary<int, (IGiftModel, int)> ShopGifts { get; private set; } = new();
public static Shop? Create(ShopBindingModel? model)
{
if (model == null)
{
return null;
}
return new Shop()
{
Id = model.Id,
ShopName = model.ShopName,
ShopAdress = model.ShopAdress,
OpeningDate = model.OpeningDate,
MaxCapacity = model.MaxCapacity,
ShopGifts = new()
};
}
public void Update(ShopBindingModel? model)
{
if (model == null)
{
return;
}
ShopName = model.ShopName;
ShopAdress = model.ShopAdress;
OpeningDate = model.OpeningDate;
MaxCapacity = model.MaxCapacity;
ShopGifts = model.ShopGifts;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
ShopAdress = ShopAdress,
OpeningDate = OpeningDate,
MaxCapacity = MaxCapacity,
ShopGifts = ShopGifts
};
}
}

View File

@ -28,136 +28,178 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.menuStrip = new System.Windows.Forms.MenuStrip(); menuStrip = new MenuStrip();
this.справочникиToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); справочникиToolStripMenuItem = new ToolStripMenuItem();
this.компонентыToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); компонентыToolStripMenuItem = new ToolStripMenuItem();
this.изделияToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); изделияToolStripMenuItem = new ToolStripMenuItem();
this.dataGridView = new System.Windows.Forms.DataGridView(); магазиныToolStripMenuItem = new ToolStripMenuItem();
this.buttonCreateOrder = new System.Windows.Forms.Button(); поставкиToolStripMenuItem = new ToolStripMenuItem();
this.buttonTakeOrderInWork = new System.Windows.Forms.Button(); продажиToolStripMenuItem = new ToolStripMenuItem();
this.buttonOrderReady = new System.Windows.Forms.Button(); dataGridView = new DataGridView();
this.buttonIssuedOrder = new System.Windows.Forms.Button(); buttonCreateOrder = new Button();
this.buttonRef = new System.Windows.Forms.Button(); buttonTakeOrderInWork = new Button();
this.menuStrip.SuspendLayout(); buttonOrderReady = new Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); buttonIssuedOrder = new Button();
this.SuspendLayout(); buttonRef = new Button();
shopReplenishment = new Button();
menuStrip.SuspendLayout();
((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
SuspendLayout();
// //
// menuStrip // menuStrip
// //
this.menuStrip.ImageScalingSize = new System.Drawing.Size(20, 20); menuStrip.ImageScalingSize = new Size(20, 20);
this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { menuStrip.Items.AddRange(new ToolStripItem[] { справочникиToolStripMenuItem });
this.справочникиToolStripMenuItem}); menuStrip.Location = new Point(0, 0);
this.menuStrip.Location = new System.Drawing.Point(0, 0); menuStrip.Name = "menuStrip";
this.menuStrip.Name = "menuStrip1"; menuStrip.Padding = new Padding(8, 2, 0, 2);
this.menuStrip.Size = new System.Drawing.Size(1367, 28); menuStrip.Size = new Size(1709, 33);
this.menuStrip.TabIndex = 0; menuStrip.TabIndex = 0;
this.menuStrip.Text = "menuStrip1"; menuStrip.Text = "menuStrip1";
// //
// справочникиToolStripMenuItem // справочникиToolStripMenuItem
// //
this.справочникиToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { справочникиToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { компонентыToolStripMenuItem, изделияToolStripMenuItem, магазиныToolStripMenuItem, поставкиToolStripMenuItem, продажиToolStripMenuItem });
this.компонентыToolStripMenuItem, справочникиToolStripMenuItem.Name = "справочникиToolStripMenuItem";
this.изделияToolStripMenuItem}); справочникиToolStripMenuItem.Size = new Size(139, 29);
this.справочникиToolStripMenuItem.Name = "справочникиToolStripMenuItem"; справочникиToolStripMenuItem.Text = "Справочники";
this.справочникиToolStripMenuItem.Size = new System.Drawing.Size(117, 24);
this.справочникиToolStripMenuItem.Text = "Справочники";
// //
// компонентыToolStripMenuItem // компонентыToolStripMenuItem
// //
this.компонентыToolStripMenuItem.Name = омпонентыToolStripMenuItem"; компонентыToolStripMenuItem.Name = омпонентыToolStripMenuItem";
this.компонентыToolStripMenuItem.Size = new System.Drawing.Size(182, 26); компонентыToolStripMenuItem.Size = new Size(270, 34);
this.компонентыToolStripMenuItem.Text = "Компоненты"; компонентыToolStripMenuItem.Text = "Компоненты";
this.компонентыToolStripMenuItem.Click += new System.EventHandler(this.КомпонентыToolStripMenuItem_Click); компонентыToolStripMenuItem.Click += КомпонентыToolStripMenuItem_Click;
// //
// изделияToolStripMenuItem // изделияToolStripMenuItem
// //
this.изделияToolStripMenuItem.Name = "изделияToolStripMenuItem"; изделияToolStripMenuItem.Name = "изделияToolStripMenuItem";
this.изделияToolStripMenuItem.Size = new System.Drawing.Size(182, 26); изделияToolStripMenuItem.Size = new Size(270, 34);
this.изделияToolStripMenuItem.Text = "Изделия"; изделияToolStripMenuItem.Text = "Изделия";
this.изделияToolStripMenuItem.Click += new System.EventHandler(this.ИзделияToolStripMenuItem_Click); изделияToolStripMenuItem.Click += ИзделияToolStripMenuItem_Click;
//
// магазиныToolStripMenuItem
//
магазиныToolStripMenuItem.Name = агазиныToolStripMenuItem";
магазиныToolStripMenuItem.Size = new Size(270, 34);
магазиныToolStripMenuItem.Text = "Магазины";
магазиныToolStripMenuItem.Click += МагазиныToolStripMenuItem_Click;
//
// поставкиToolStripMenuItem
//
поставкиToolStripMenuItem.Name = "поставкиToolStripMenuItem";
поставкиToolStripMenuItem.Size = new Size(270, 34);
поставкиToolStripMenuItem.Text = "Поставки";
поставкиToolStripMenuItem.Click += поставкиToolStripMenuItem_Click;
//
// продажиToolStripMenuItem
//
продажиToolStripMenuItem.Name = "продажиToolStripMenuItem";
продажиToolStripMenuItem.Size = new Size(270, 34);
продажиToolStripMenuItem.Text = "Продажи";
продажиToolStripMenuItem.Click += продажиToolStripMenuItem_Click;
// //
// dataGridView // dataGridView
// //
this.dataGridView.BackgroundColor = System.Drawing.SystemColors.ControlLightLight; dataGridView.BackgroundColor = SystemColors.ControlLightLight;
this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView.Location = new System.Drawing.Point(12, 41); dataGridView.Location = new Point(15, 51);
this.dataGridView.Name = "dataGridView"; dataGridView.Margin = new Padding(4);
this.dataGridView.RowHeadersWidth = 51; dataGridView.Name = "dataGridView";
this.dataGridView.RowTemplate.Height = 29; dataGridView.RowHeadersWidth = 51;
this.dataGridView.Size = new System.Drawing.Size(1123, 423); dataGridView.RowTemplate.Height = 29;
this.dataGridView.TabIndex = 1; dataGridView.Size = new Size(1404, 529);
dataGridView.TabIndex = 1;
// //
// buttonCreateOrder // buttonCreateOrder
// //
this.buttonCreateOrder.Location = new System.Drawing.Point(1155, 67); buttonCreateOrder.Location = new Point(1444, 84);
this.buttonCreateOrder.Name = "buttonCreateOrder"; buttonCreateOrder.Margin = new Padding(4);
this.buttonCreateOrder.Size = new System.Drawing.Size(189, 41); buttonCreateOrder.Name = "buttonCreateOrder";
this.buttonCreateOrder.TabIndex = 2; buttonCreateOrder.Size = new Size(236, 51);
this.buttonCreateOrder.Text = "Создать заказ"; buttonCreateOrder.TabIndex = 2;
this.buttonCreateOrder.UseVisualStyleBackColor = true; buttonCreateOrder.Text = "Создать заказ";
this.buttonCreateOrder.Click += new System.EventHandler(this.ButtonCreateOrder_Click); buttonCreateOrder.UseVisualStyleBackColor = true;
buttonCreateOrder.Click += ButtonCreateOrder_Click;
// //
// buttonTakeOrderInWork // buttonTakeOrderInWork
// //
this.buttonTakeOrderInWork.Location = new System.Drawing.Point(1155, 136); buttonTakeOrderInWork.Location = new Point(1444, 170);
this.buttonTakeOrderInWork.Name = "buttonTakeOrderInWork"; buttonTakeOrderInWork.Margin = new Padding(4);
this.buttonTakeOrderInWork.Size = new System.Drawing.Size(189, 41); buttonTakeOrderInWork.Name = "buttonTakeOrderInWork";
this.buttonTakeOrderInWork.TabIndex = 3; buttonTakeOrderInWork.Size = new Size(236, 51);
this.buttonTakeOrderInWork.Text = "Отдать на выполнение"; buttonTakeOrderInWork.TabIndex = 3;
this.buttonTakeOrderInWork.UseVisualStyleBackColor = true; buttonTakeOrderInWork.Text = "Отдать на выполнение";
this.buttonTakeOrderInWork.Click += new System.EventHandler(this.ButtonTakeOrderInWork_Click); buttonTakeOrderInWork.UseVisualStyleBackColor = true;
buttonTakeOrderInWork.Click += ButtonTakeOrderInWork_Click;
// //
// buttonOrderReady // buttonOrderReady
// //
this.buttonOrderReady.Location = new System.Drawing.Point(1155, 211); buttonOrderReady.Location = new Point(1444, 264);
this.buttonOrderReady.Name = "buttonOrderReady"; buttonOrderReady.Margin = new Padding(4);
this.buttonOrderReady.Size = new System.Drawing.Size(189, 41); buttonOrderReady.Name = "buttonOrderReady";
this.buttonOrderReady.TabIndex = 4; buttonOrderReady.Size = new Size(236, 51);
this.buttonOrderReady.Text = "Заказ готов"; buttonOrderReady.TabIndex = 4;
this.buttonOrderReady.UseVisualStyleBackColor = true; buttonOrderReady.Text = "Заказ готов";
this.buttonOrderReady.Click += new System.EventHandler(this.ButtonOrderReady_Click); buttonOrderReady.UseVisualStyleBackColor = true;
buttonOrderReady.Click += ButtonOrderReady_Click;
// //
// buttonIssuedOrder // buttonIssuedOrder
// //
this.buttonIssuedOrder.Location = new System.Drawing.Point(1155, 290); buttonIssuedOrder.Location = new Point(1444, 362);
this.buttonIssuedOrder.Name = "buttonIssuedOrder"; buttonIssuedOrder.Margin = new Padding(4);
this.buttonIssuedOrder.Size = new System.Drawing.Size(189, 41); buttonIssuedOrder.Name = "buttonIssuedOrder";
this.buttonIssuedOrder.TabIndex = 5; buttonIssuedOrder.Size = new Size(236, 51);
this.buttonIssuedOrder.Text = "Заказ выдан"; buttonIssuedOrder.TabIndex = 5;
this.buttonIssuedOrder.UseVisualStyleBackColor = true; buttonIssuedOrder.Text = "Заказ выдан";
this.buttonIssuedOrder.Click += new System.EventHandler(this.ButtonIssuedOrder_Click); buttonIssuedOrder.UseVisualStyleBackColor = true;
buttonIssuedOrder.Click += ButtonIssuedOrder_Click;
// //
// buttonRef // buttonRef
// //
this.buttonRef.Location = new System.Drawing.Point(1155, 356); buttonRef.Location = new Point(1444, 445);
this.buttonRef.Name = "buttonRef"; buttonRef.Margin = new Padding(4);
this.buttonRef.Size = new System.Drawing.Size(189, 41); buttonRef.Name = "buttonRef";
this.buttonRef.TabIndex = 6; buttonRef.Size = new Size(236, 51);
this.buttonRef.Text = "Обновить список"; buttonRef.TabIndex = 6;
this.buttonRef.UseVisualStyleBackColor = true; buttonRef.Text = "Обновить список";
this.buttonRef.Click += new System.EventHandler(this.ButtonRef_Click); buttonRef.UseVisualStyleBackColor = true;
buttonRef.Click += ButtonRef_Click;
//
// shopReplenishment
//
shopReplenishment.Location = new Point(1444, 525);
shopReplenishment.Margin = new Padding(4);
shopReplenishment.Name = "shopReplenishment";
shopReplenishment.Size = new Size(236, 51);
shopReplenishment.TabIndex = 7;
shopReplenishment.Text = "Пополнение магазина";
shopReplenishment.UseVisualStyleBackColor = true;
shopReplenishment.Click += ShopReplenishment_Click;
// //
// FormMain // FormMain
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); AutoScaleDimensions = new SizeF(10F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1367, 471); ClientSize = new Size(1709, 589);
this.Controls.Add(this.buttonRef); Controls.Add(shopReplenishment);
this.Controls.Add(this.buttonIssuedOrder); Controls.Add(buttonRef);
this.Controls.Add(this.buttonOrderReady); Controls.Add(buttonIssuedOrder);
this.Controls.Add(this.buttonTakeOrderInWork); Controls.Add(buttonOrderReady);
this.Controls.Add(this.buttonCreateOrder); Controls.Add(buttonTakeOrderInWork);
this.Controls.Add(this.dataGridView); Controls.Add(buttonCreateOrder);
this.Controls.Add(this.menuStrip); Controls.Add(dataGridView);
this.MainMenuStrip = this.menuStrip; Controls.Add(menuStrip);
this.Name = "FormMain"; MainMenuStrip = menuStrip;
this.Text = "Магазин подарков"; Margin = new Padding(4);
this.Load += new System.EventHandler(this.FormMain_Load); Name = "FormMain";
this.menuStrip.ResumeLayout(false); Text = "Магазин подарков";
this.menuStrip.PerformLayout(); Load += FormMain_Load;
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); menuStrip.ResumeLayout(false);
this.ResumeLayout(false); menuStrip.PerformLayout();
this.PerformLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit();
ResumeLayout(false);
PerformLayout();
} }
#endregion #endregion
@ -166,11 +208,15 @@
private ToolStripMenuItem справочникиToolStripMenuItem; private ToolStripMenuItem справочникиToolStripMenuItem;
private ToolStripMenuItem компонентыToolStripMenuItem; private ToolStripMenuItem компонентыToolStripMenuItem;
private ToolStripMenuItem изделияToolStripMenuItem; private ToolStripMenuItem изделияToolStripMenuItem;
private ToolStripMenuItem магазиныToolStripMenuItem;
private DataGridView dataGridView; private DataGridView dataGridView;
private Button buttonCreateOrder; private Button buttonCreateOrder;
private Button buttonTakeOrderInWork; private Button buttonTakeOrderInWork;
private Button buttonOrderReady; private Button buttonOrderReady;
private Button buttonIssuedOrder; private Button buttonIssuedOrder;
private Button buttonRef; private Button buttonRef;
private Button shopReplenishment;
private ToolStripMenuItem поставкиToolStripMenuItem;
private ToolStripMenuItem продажиToolStripMenuItem;
} }
} }

View File

@ -63,7 +63,34 @@ namespace GiftShopView
form.ShowDialog(); form.ShowDialog();
} }
} }
private void МагазиныToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormShops));
if (service is FormShops form)
{
form.ShowDialog();
}
}
private void продажиToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormSell));
if (service is FormSell form)
{
form.ShowDialog();
}
}
private void поставкиToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormSupply));
if (service is FormSupply form)
{
form.ShowDialog();
}
}
private void ButtonCreateOrder_Click(object sender, EventArgs e) private void ButtonCreateOrder_Click(object sender, EventArgs e)
{ {
var service = Program.ServiceProvider?.GetService(typeof(FormCreateOrder)); var service = Program.ServiceProvider?.GetService(typeof(FormCreateOrder));
@ -84,10 +111,14 @@ namespace GiftShopView
{ {
var operationResult = _orderLogic.TakeOrderInWork(new OrderBindingModel var operationResult = _orderLogic.TakeOrderInWork(new OrderBindingModel
{ {
Id = id, Id = id,
GiftId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["GiftId"].Value), GiftId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["GiftId"].Value),
Status = Enum.Parse<OrderStatus>(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()) GiftName = dataGridView.SelectedRows[0].Cells["GiftName"].Value.ToString(),
}); Status = Enum.Parse<OrderStatus>(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()),
Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value),
Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()),
DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()),
});
if (!operationResult) if (!operationResult)
{ {
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
@ -167,10 +198,20 @@ namespace GiftShopView
} }
} }
} }
private void ShopReplenishment_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormShopReplenishment));
if (service is FormShopReplenishment form)
{
form.ShowDialog();
}
}
private void ButtonRef_Click(object sender, EventArgs e) private void ButtonRef_Click(object sender, EventArgs e)
{ {
LoadData(); LoadData();
} }
} }
} }

View File

@ -1,4 +1,64 @@
<root> <?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" /> <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true"> <xsd:element name="root" msdata:IsDataSet="true">

124
GiftShop/GiftShopView/FormSell.Designer.cs generated Normal file
View File

@ -0,0 +1,124 @@
namespace GiftShopView
{
partial class FormSell
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
GiftComboBox = new ComboBox();
GiftLabel = new Label();
CountLabel = new Label();
CountTextBox = new TextBox();
SaveButton = new Button();
CancelButton = new Button();
SuspendLayout();
//
// GiftComboBox
//
GiftComboBox.FormattingEnabled = true;
GiftComboBox.Location = new Point(149, 30);
GiftComboBox.Margin = new Padding(4, 5, 4, 5);
GiftComboBox.Name = "GiftComboBox";
GiftComboBox.Size = new Size(241, 33);
GiftComboBox.TabIndex = 0;
//
// GiftLabel
//
GiftLabel.AutoSize = true;
GiftLabel.Location = new Point(51, 33);
GiftLabel.Margin = new Padding(4, 0, 4, 0);
GiftLabel.Name = "GiftLabel";
GiftLabel.Size = new Size(90, 25);
GiftLabel.TabIndex = 1;
GiftLabel.Text = "Подарок:";
//
// CountLabel
//
CountLabel.AutoSize = true;
CountLabel.Location = new Point(30, 82);
CountLabel.Margin = new Padding(4, 0, 4, 0);
CountLabel.Name = "CountLabel";
CountLabel.Size = new Size(111, 25);
CountLabel.TabIndex = 2;
CountLabel.Text = "Количество:";
//
// CountTextBox
//
CountTextBox.Location = new Point(149, 79);
CountTextBox.Margin = new Padding(4, 5, 4, 5);
CountTextBox.Name = "CountTextBox";
CountTextBox.Size = new Size(241, 31);
CountTextBox.TabIndex = 3;
//
// SaveButton
//
SaveButton.Location = new Point(51, 157);
SaveButton.Margin = new Padding(4, 5, 4, 5);
SaveButton.Name = "SaveButton";
SaveButton.Size = new Size(152, 50);
SaveButton.TabIndex = 4;
SaveButton.Text = "Сохранить";
SaveButton.UseVisualStyleBackColor = true;
SaveButton.Click += SaveButton_Click;
//
// CancelButton
//
CancelButton.Location = new Point(250, 157);
CancelButton.Margin = new Padding(4, 5, 4, 5);
CancelButton.Name = "CancelButton";
CancelButton.Size = new Size(152, 50);
CancelButton.TabIndex = 5;
CancelButton.Text = "Отмена";
CancelButton.UseVisualStyleBackColor = true;
//
// SellForm
//
AutoScaleDimensions = new SizeF(10F, 25F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(415, 221);
Controls.Add(CancelButton);
Controls.Add(SaveButton);
Controls.Add(CountTextBox);
Controls.Add(CountLabel);
Controls.Add(GiftLabel);
Controls.Add(GiftComboBox);
Margin = new Padding(4, 5, 4, 5);
Name = "SellForm";
Text = "Форма продажи";
ResumeLayout(false);
PerformLayout();
}
#endregion
private ComboBox GiftComboBox;
private Label GiftLabel;
private Label CountLabel;
private TextBox CountTextBox;
private Button SaveButton;
private Button CancelButton;
}
}

View File

@ -0,0 +1,116 @@
using GiftShopContracts.BusinessLogicsContracts;
using GiftShopContracts.SearchModels;
using GiftShopContracts.ViewModels;
using GiftShopDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GiftShopView
{
public partial class FormSell : Form
{
private readonly List<GiftViewModel>? _GiftList;
IShopLogic _shopLogic;
IGiftLogic _GiftLogic;
public FormSell(IGiftLogic GiftLogic, IShopLogic shopLogic)
{
InitializeComponent();
_shopLogic = shopLogic;
_GiftLogic = GiftLogic;
_GiftList = GiftLogic.ReadList(null);
if (_GiftList != null)
{
GiftComboBox.DisplayMember = "GiftName";
GiftComboBox.ValueMember = "Id";
GiftComboBox.DataSource = _GiftList;
GiftComboBox.SelectedItem = null;
}
}
public int GiftId
{
get
{
return Convert.ToInt32(GiftComboBox.SelectedValue);
}
set
{
GiftComboBox.SelectedValue = value;
}
}
public IGiftModel? GiftModel
{
get
{
if (_GiftList == null)
{
return null;
}
foreach (var elem in _GiftList)
{
if (elem.Id == GiftId)
{
return elem;
}
}
return null;
}
}
public int Count
{
get { return Convert.ToInt32(CountTextBox.Text); }
set
{ CountTextBox.Text = value.ToString(); }
}
private void SaveButton_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(CountTextBox.Text))
{
MessageBox.Show("Заполните поле Количество", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (GiftComboBox.SelectedValue == null)
{
MessageBox.Show("Выберите подарок", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
int count = Convert.ToInt32(CountTextBox.Text);
bool res = _shopLogic.MakeSell(
_GiftLogic.ReadElement(new() { Id = Convert.ToInt32(GiftComboBox.SelectedValue) }),
count
);
if (!res)
{
throw new Exception("Ошибка при продаже.");
}
MessageBox.Show("Продажа прошла успешно");
DialogResult = DialogResult.OK;
Close();
}
catch (Exception err)
{
MessageBox.Show("Ошибка продажи");
return;
}
}
}
}

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

221
GiftShop/GiftShopView/FormShop.Designer.cs generated Normal file
View File

@ -0,0 +1,221 @@
namespace GiftShopView
{
partial class FormShop
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
shopNameLabel = new Label();
shopAddressLabel = new Label();
openingDateLabel = new Label();
addressTextBox = new TextBox();
openingDatePicker = new DateTimePicker();
dataGridView = new DataGridView();
PackageName = new DataGridViewTextBoxColumn();
PackagePrice = new DataGridViewTextBoxColumn();
PackageCount = new DataGridViewTextBoxColumn();
SaveButton = new Button();
ButtonCancel = new Button();
nameTextBox = new TextBox();
labelCapacity = new Label();
CapacityUpDown = new NumericUpDown();
((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
SuspendLayout();
//
// shopNameLabel
//
shopNameLabel.AutoSize = true;
shopNameLabel.Location = new Point(18, 15);
shopNameLabel.Margin = new Padding(4, 0, 4, 0);
shopNameLabel.Name = "shopNameLabel";
shopNameLabel.Size = new Size(179, 25);
shopNameLabel.TabIndex = 0;
shopNameLabel.Text = "Название магазина: ";
//
// shopAddressLabel
//
shopAddressLabel.AutoSize = true;
shopAddressLabel.Location = new Point(18, 66);
shopAddressLabel.Margin = new Padding(4, 0, 4, 0);
shopAddressLabel.Name = "shopAddressLabel";
shopAddressLabel.Size = new Size(151, 25);
shopAddressLabel.TabIndex = 1;
shopAddressLabel.Text = "Адрес магазина: ";
//
// openingDateLabel
//
openingDateLabel.AutoSize = true;
openingDateLabel.Location = new Point(18, 120);
openingDateLabel.Margin = new Padding(4, 0, 4, 0);
openingDateLabel.Name = "openingDateLabel";
openingDateLabel.Size = new Size(140, 25);
openingDateLabel.TabIndex = 2;
openingDateLabel.Text = "Дата открытия: ";
//
// addressTextBox
//
addressTextBox.Location = new Point(204, 62);
addressTextBox.Margin = new Padding(4, 5, 4, 5);
addressTextBox.Name = "addressTextBox";
addressTextBox.Size = new Size(246, 31);
addressTextBox.TabIndex = 4;
//
// openingDatePicker
//
openingDatePicker.Location = new Point(204, 114);
openingDatePicker.Margin = new Padding(4);
openingDatePicker.Name = "openingDatePicker";
openingDatePicker.Size = new Size(246, 31);
openingDatePicker.TabIndex = 9;
//
// dataGridView
//
dataGridView.AllowUserToAddRows = false;
dataGridView.AllowUserToDeleteRows = false;
dataGridView.BackgroundColor = SystemColors.Window;
dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridView.Columns.AddRange(new DataGridViewColumn[] { PackageName, PackagePrice, PackageCount });
dataGridView.Location = new Point(20, 225);
dataGridView.Margin = new Padding(4, 5, 4, 5);
dataGridView.MultiSelect = false;
dataGridView.Name = "dataGridView";
dataGridView.RowHeadersWidth = 51;
dataGridView.RowTemplate.Height = 25;
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView.Size = new Size(1109, 418);
dataGridView.TabIndex = 6;
//
// PackageName
//
PackageName.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
PackageName.HeaderText = "Название изделия";
PackageName.MinimumWidth = 6;
PackageName.Name = "PackageName";
//
// PackagePrice
//
PackagePrice.HeaderText = "Цена";
PackagePrice.MinimumWidth = 6;
PackagePrice.Name = "PackagePrice";
PackagePrice.Width = 125;
//
// PackageCount
//
PackageCount.HeaderText = "Количество";
PackageCount.MinimumWidth = 6;
PackageCount.Name = "PackageCount";
PackageCount.Width = 125;
//
// SaveButton
//
SaveButton.Location = new Point(789, 671);
SaveButton.Margin = new Padding(4, 5, 4, 5);
SaveButton.Name = "SaveButton";
SaveButton.Size = new Size(164, 59);
SaveButton.TabIndex = 7;
SaveButton.Text = "Сохранить";
SaveButton.UseVisualStyleBackColor = true;
SaveButton.Click += SaveButton_Click;
//
// ButtonCancel
//
ButtonCancel.Location = new Point(961, 671);
ButtonCancel.Margin = new Padding(4, 5, 4, 5);
ButtonCancel.Name = "ButtonCancel";
ButtonCancel.Size = new Size(164, 59);
ButtonCancel.TabIndex = 8;
ButtonCancel.Text = "Отменить";
ButtonCancel.UseVisualStyleBackColor = true;
ButtonCancel.Click += ButtonCancel_Click;
//
// nameTextBox
//
nameTextBox.Location = new Point(204, 11);
nameTextBox.Margin = new Padding(4);
nameTextBox.Name = "nameTextBox";
nameTextBox.Size = new Size(246, 31);
nameTextBox.TabIndex = 10;
//
// labelCapacity
//
labelCapacity.AutoSize = true;
labelCapacity.Location = new Point(20, 167);
labelCapacity.Margin = new Padding(4, 0, 4, 0);
labelCapacity.Name = "labelCapacity";
labelCapacity.Size = new Size(121, 25);
labelCapacity.TabIndex = 11;
labelCapacity.Text = "Вместимость:";
//
// CapacityUpDown
//
CapacityUpDown.Location = new Point(204, 161);
CapacityUpDown.Name = "CapacityUpDown";
CapacityUpDown.Size = new Size(246, 31);
CapacityUpDown.TabIndex = 12;
//
// FormShop
//
AutoScaleDimensions = new SizeF(10F, 25F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1142, 750);
Controls.Add(CapacityUpDown);
Controls.Add(labelCapacity);
Controls.Add(nameTextBox);
Controls.Add(ButtonCancel);
Controls.Add(SaveButton);
Controls.Add(dataGridView);
Controls.Add(openingDatePicker);
Controls.Add(addressTextBox);
Controls.Add(openingDateLabel);
Controls.Add(shopAddressLabel);
Controls.Add(shopNameLabel);
Margin = new Padding(4, 5, 4, 5);
Name = "FormShop";
Text = "Магазин";
Load += FormShop_Load;
((System.ComponentModel.ISupportInitialize)dataGridView).EndInit();
ResumeLayout(false);
PerformLayout();
}
#endregion
private Label shopNameLabel;
private Label shopAddressLabel;
private Label openingDateLabel;
private TextBox addressTextBox;
private DateTimePicker openingDatePicker;
private DataGridView dataGridView;
private Button SaveButton;
private Button ButtonCancel;
private DataGridViewTextBoxColumn PackageName;
private DataGridViewTextBoxColumn PackagePrice;
private DataGridViewTextBoxColumn PackageCount;
private TextBox nameTextBox;
private Label labelCapacity;
private NumericUpDown CapacityUpDown;
}
}

View File

@ -0,0 +1,144 @@
using GiftShopContracts.BindingModels;
using GiftShopContracts.BusinessLogicsContracts;
using GiftShopContracts.ViewModels;
using GiftShopDataModels.Models;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GiftShopView
{
public partial class FormShop : Form
{
private readonly List<ShopViewModel>? _listShops;
private readonly IShopLogic _logic;
private readonly ILogger _logger;
public int Id { get; set; }
public FormShop(ILogger<FormShop> logger, IShopLogic logic)
{
InitializeComponent();
_logger = logger;
_listShops = logic.ReadList(null);
_logic = logic;
}
private IShopModel? GetShop(int id)
{
if (_listShops == null)
{
return null;
}
foreach (var elem in _listShops)
{
if (elem.Id == id)
{
return elem;
}
}
return null;
}
private void SaveButton_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(nameTextBox.Text))
{
MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(addressTextBox.Text))
{
MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Сохранение изделия");
try
{
DateTime.TryParse(openingDatePicker.Text, out var dateTime);
ShopBindingModel model = new()
{
ShopName = nameTextBox.Text,
ShopAdress = addressTextBox.Text,
OpeningDate = dateTime,
MaxCapacity = Convert.ToInt32(CapacityUpDown.Value)
};
var vmodel = GetShop(Id);
bool operationResult = false;
if (vmodel != null)
{
model.Id = vmodel.Id;
model.ShopGifts = vmodel.ShopGifts;
operationResult = _logic.Update(model);
}
else
{
operationResult = _logic.Create(model);
}
if (!operationResult)
{
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
}
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка сохранения изделия");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void FormShop_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData(bool extendDate = true)
{
try
{
var model = GetShop(extendDate ? Id : Convert.ToInt32(nameTextBox.Text));
if (model != null)
{
nameTextBox.Text = model.ShopName;
addressTextBox.Text = model.ShopAdress;
openingDatePicker.Text = Convert.ToString(model.OpeningDate);
CapacityUpDown.Value = Convert.ToInt32(model.MaxCapacity);
dataGridView.Rows.Clear();
foreach (var el in model.ShopGifts.Values)
{
dataGridView.Rows.Add(new object[] { el.Item1.GiftName, el.Item1.Price, el.Item2 });
}
}
_logger.LogInformation("Загрузка магазинов");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки магазинов");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
}

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -0,0 +1,152 @@
namespace GiftShopView
{
partial class FormShopReplenishment
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Text = "FormShopReplenishment";
shopNameLabel = new Label();
packageNameLabel = new Label();
countLabel = new Label();
shopNameComboBox = new ComboBox();
giftNameComboBox = new ComboBox();
countTextBox = new TextBox();
buttonSave = new Button();
buttonCancel = new Button();
SuspendLayout();
//
// shopNameLabel
//
shopNameLabel.AutoSize = true;
shopNameLabel.Location = new Point(14, 12);
shopNameLabel.Name = "shopNameLabel";
shopNameLabel.Size = new Size(154, 20);
shopNameLabel.TabIndex = 0;
shopNameLabel.Text = "Название магазина: ";
//
// packageNameLabel
//
packageNameLabel.AutoSize = true;
packageNameLabel.Location = new Point(14, 49);
packageNameLabel.Name = "packageNameLabel";
packageNameLabel.Size = new Size(145, 20);
packageNameLabel.TabIndex = 1;
packageNameLabel.Text = "Название изделия: ";
//
// countLabel
//
countLabel.AutoSize = true;
countLabel.Location = new Point(14, 88);
countLabel.Name = "countLabel";
countLabel.Size = new Size(97, 20);
countLabel.TabIndex = 2;
countLabel.Text = "Количество: ";
//
// shopNameComboBox
//
shopNameComboBox.FormattingEnabled = true;
shopNameComboBox.Location = new Point(171, 9);
shopNameComboBox.Margin = new Padding(3, 4, 3, 4);
shopNameComboBox.Name = "shopNameComboBox";
shopNameComboBox.Size = new Size(170, 28);
shopNameComboBox.TabIndex = 3;
//
// giftNameComboBox
//
giftNameComboBox.FormattingEnabled = true;
giftNameComboBox.Location = new Point(171, 49);
giftNameComboBox.Margin = new Padding(3, 4, 3, 4);
giftNameComboBox.Name = "giftNameComboBox";
giftNameComboBox.Size = new Size(170, 28);
giftNameComboBox.TabIndex = 4;
//
// countTextBox
//
countTextBox.Location = new Point(171, 87);
countTextBox.Margin = new Padding(3, 4, 3, 4);
countTextBox.Name = "countTextBox";
countTextBox.Size = new Size(170, 27);
countTextBox.TabIndex = 5;
//
// buttonSave
//
buttonSave.Location = new Point(151, 144);
buttonSave.Margin = new Padding(3, 4, 3, 4);
buttonSave.Name = "buttonSave";
buttonSave.Size = new Size(98, 31);
buttonSave.TabIndex = 6;
buttonSave.Text = "Сохранить";
buttonSave.UseVisualStyleBackColor = true;
buttonSave.Click += SaveButton_Click;
//
// buttonCancel
//
buttonCancel.Location = new Point(255, 144);
buttonCancel.Margin = new Padding(3, 4, 3, 4);
buttonCancel.Name = "buttonCancel";
buttonCancel.Size = new Size(86, 31);
buttonCancel.TabIndex = 7;
buttonCancel.Text = "Отмена";
buttonCancel.UseVisualStyleBackColor = true;
buttonCancel.Click += ButtonCancel_Click;
//
// FormShopReplenishment
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(353, 200);
Controls.Add(buttonCancel);
Controls.Add(buttonSave);
Controls.Add(countTextBox);
Controls.Add(giftNameComboBox);
Controls.Add(shopNameComboBox);
Controls.Add(countLabel);
Controls.Add(packageNameLabel);
Controls.Add(shopNameLabel);
Margin = new Padding(3, 4, 3, 4);
Name = "FormShopReplenishment";
Text = "Пополнение магазина";
ResumeLayout(false);
PerformLayout();
}
#endregion
private Label shopNameLabel;
private Label packageNameLabel;
private Label countLabel;
private ComboBox shopNameComboBox;
private ComboBox giftNameComboBox;
private TextBox countTextBox;
private Button buttonSave;
private Button buttonCancel;
}
}

View File

@ -0,0 +1,104 @@
using GiftShopContracts.BusinessLogicsContracts;
using GiftShopContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GiftShopView
{
public partial class FormShopReplenishment : Form
{
private readonly ILogger _logger;
private readonly IShopLogic _ShopLogic;
private readonly IGiftLogic _giftLogic;
private readonly List<ShopViewModel>? _listShops;
private readonly List<GiftViewModel>? _listGifts;
public FormShopReplenishment(ILogger<FormShopReplenishment> logger, IShopLogic ShopLogic, IGiftLogic giftLogic)
{
InitializeComponent();
_ShopLogic = ShopLogic;
_giftLogic = giftLogic;
_logger = logger;
_listShops = ShopLogic.ReadList(null);
if (_listShops != null)
{
shopNameComboBox.DisplayMember = "ShopName";
shopNameComboBox.ValueMember = "Id";
shopNameComboBox.DataSource = _listShops;
shopNameComboBox.SelectedItem = null;
}
_listGifts = giftLogic.ReadList(null);
if (_listGifts != null)
{
giftNameComboBox.DisplayMember = "GiftName";
giftNameComboBox.ValueMember = "Id";
giftNameComboBox.DataSource = _listGifts;
giftNameComboBox.SelectedItem = null;
}
}
private void SaveButton_Click(object sender, EventArgs e)
{
if (shopNameComboBox.SelectedValue == null)
{
MessageBox.Show("Выберите магазин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (giftNameComboBox.SelectedValue == null)
{
MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Добавление изделия в магазин");
try
{
var gift = _giftLogic.ReadElement(new()
{
Id = (int)giftNameComboBox.SelectedValue
});
if (gift == null)
{
throw new Exception("Не найдено изделие. Дополнительная информация в логах.");
}
var resultOperation = _ShopLogic.AddGift(
model: new() { Id = (int)shopNameComboBox.SelectedValue },
gift: gift,
quantity: Convert.ToInt32(countTextBox.Text)
);
if (!resultOperation)
{
throw new Exception("Ошибка при добавлении. Дополнительная информация в логах.");
}
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка сохранения изделия");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -0,0 +1,126 @@
namespace GiftShopView
{
partial class FormShops
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
dataGridView = new DataGridView();
buttonAdd = new Button();
buttonUpdate = new Button();
buttonDelete = new Button();
buttonRefresh = new Button();
((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
SuspendLayout();
//
// dataGridView
//
dataGridView.AllowUserToAddRows = false;
dataGridView.AllowUserToDeleteRows = false;
dataGridView.BackgroundColor = SystemColors.Window;
dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridView.Location = new Point(1, 1);
dataGridView.Margin = new Padding(4, 5, 4, 5);
dataGridView.MultiSelect = false;
dataGridView.Name = "dataGridView";
dataGridView.RowHeadersVisible = false;
dataGridView.RowHeadersWidth = 51;
dataGridView.RowTemplate.Height = 25;
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView.Size = new Size(786, 745);
dataGridView.TabIndex = 0;
//
// buttonAdd
//
buttonAdd.Location = new Point(836, 20);
buttonAdd.Margin = new Padding(4, 5, 4, 5);
buttonAdd.Name = "buttonAdd";
buttonAdd.Size = new Size(172, 66);
buttonAdd.TabIndex = 1;
buttonAdd.Text = "Добавить";
buttonAdd.UseVisualStyleBackColor = true;
buttonAdd.Click += AddButton_Click;
//
// buttonUpdate
//
buttonUpdate.Location = new Point(836, 111);
buttonUpdate.Margin = new Padding(4, 5, 4, 5);
buttonUpdate.Name = "buttonUpdate";
buttonUpdate.Size = new Size(172, 66);
buttonUpdate.TabIndex = 2;
buttonUpdate.Text = "Изменить";
buttonUpdate.UseVisualStyleBackColor = true;
buttonUpdate.Click += UpdateButton_Click;
//
// buttonDelete
//
buttonDelete.Location = new Point(836, 204);
buttonDelete.Margin = new Padding(4, 5, 4, 5);
buttonDelete.Name = "buttonDelete";
buttonDelete.Size = new Size(172, 66);
buttonDelete.TabIndex = 3;
buttonDelete.Text = "Удалить";
buttonDelete.UseVisualStyleBackColor = true;
buttonDelete.Click += DeleteButton_Click;
//
// buttonRefresh
//
buttonRefresh.Location = new Point(836, 299);
buttonRefresh.Margin = new Padding(4, 5, 4, 5);
buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(172, 66);
buttonRefresh.TabIndex = 4;
buttonRefresh.Text = "Обновить";
buttonRefresh.UseVisualStyleBackColor = true;
buttonRefresh.Click += RefreshButton_Click;
//
// FormShops
//
AutoScaleDimensions = new SizeF(10F, 25F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1026, 750);
Controls.Add(buttonRefresh);
Controls.Add(buttonDelete);
Controls.Add(buttonUpdate);
Controls.Add(buttonAdd);
Controls.Add(dataGridView);
Margin = new Padding(4, 5, 4, 5);
Name = "FormShops";
Text = "Магазины";
Load += FormShops_Load;
((System.ComponentModel.ISupportInitialize)dataGridView).EndInit();
ResumeLayout(false);
}
#endregion
private DataGridView dataGridView;
private Button buttonAdd;
private Button buttonUpdate;
private Button buttonDelete;
private Button buttonRefresh;
}
}

View File

@ -0,0 +1,125 @@
using GiftShopContracts.BindingModels;
using GiftShopContracts.BusinessLogicsContracts;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GiftShopView
{
public partial class FormShops : Form
{
private readonly ILogger _logger;
private readonly IShopLogic _logic;
public FormShops(ILogger<FormShops> logger, IShopLogic logic)
{
InitializeComponent();
_logger = logger;
_logic = logic;
}
private void FormShops_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
try
{
var list = _logic.ReadList(null);
if (list != null)
{
dataGridView.DataSource = list;
dataGridView.Columns["Id"].Visible = false;
dataGridView.Columns["ShopName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["ShopAdress"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["OpeningDate"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["MaxCapacity"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["ShopGifts"].Visible = false;
}
_logger.LogInformation("Загрузка магазинов");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки магазинов");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void RefreshButton_Click(object sender, EventArgs e)
{
LoadData();
}
private void DeleteButton_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
_logger.LogInformation("Удаление магазина");
try
{
if (!_logic.Delete(new ShopBindingModel
{
Id = id
}))
{
throw new Exception("Ошибка при удалении. Дополнительная информация в логах.");
}
LoadData();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления изделия");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void UpdateButton_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
var service = Program.ServiceProvider?.GetService(typeof(FormShop));
if (service is FormShop form)
{
form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
}
private void AddButton_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormShop));
if (service is FormShop form)
{
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
}
}

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -0,0 +1,111 @@
namespace GiftShopView
{
partial class FormSupply
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
ShopComboBox = new ComboBox();
GiftComboBox = new ComboBox();
CountTextBox = new TextBox();
SaveButton = new Button();
CancelButton = new Button();
SuspendLayout();
//
// ShopComboBox
//
ShopComboBox.FormattingEnabled = true;
ShopComboBox.Location = new Point(18, 20);
ShopComboBox.Margin = new Padding(4, 5, 4, 5);
ShopComboBox.Name = "ShopComboBox";
ShopComboBox.Size = new Size(472, 33);
ShopComboBox.TabIndex = 0;
//
// GiftComboBox
//
GiftComboBox.FormattingEnabled = true;
GiftComboBox.Location = new Point(18, 69);
GiftComboBox.Margin = new Padding(4, 5, 4, 5);
GiftComboBox.Name = "GiftComboBox";
GiftComboBox.Size = new Size(472, 33);
GiftComboBox.TabIndex = 1;
//
// CountTextBox
//
CountTextBox.Location = new Point(18, 116);
CountTextBox.Margin = new Padding(4, 5, 4, 5);
CountTextBox.Name = "CountTextBox";
CountTextBox.Size = new Size(472, 31);
CountTextBox.TabIndex = 2;
//
// SaveButton
//
SaveButton.Location = new Point(152, 200);
SaveButton.Margin = new Padding(4, 5, 4, 5);
SaveButton.Name = "SaveButton";
SaveButton.Size = new Size(157, 52);
SaveButton.TabIndex = 3;
SaveButton.Text = "Сохранить";
SaveButton.UseVisualStyleBackColor = true;
SaveButton.Click += SaveButton_Click;
//
// CancelButton
//
CancelButton.Location = new Point(338, 200);
CancelButton.Margin = new Padding(4, 5, 4, 5);
CancelButton.Name = "CancelButton";
CancelButton.Size = new Size(157, 52);
CancelButton.TabIndex = 4;
CancelButton.Text = "Отмена";
CancelButton.UseVisualStyleBackColor = true;
CancelButton.Click += CancelButton_Click;
//
// SupplyForm
//
AutoScaleDimensions = new SizeF(10F, 25F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(508, 266);
Controls.Add(CancelButton);
Controls.Add(SaveButton);
Controls.Add(CountTextBox);
Controls.Add(GiftComboBox);
Controls.Add(ShopComboBox);
Margin = new Padding(4, 5, 4, 5);
Name = "SupplyForm";
Text = "Форма поставок";
ResumeLayout(false);
PerformLayout();
}
#endregion
private ComboBox ShopComboBox;
private ComboBox GiftComboBox;
private TextBox CountTextBox;
private Button SaveButton;
private Button CancelButton;
}
}

View File

@ -0,0 +1,153 @@
using GiftShopBusinessLogic.BusinessLogics;
using GiftShopContracts.BusinessLogicsContracts;
using GiftShopContracts.SearchModels;
using GiftShopContracts.ViewModels;
using GiftShopDataModels.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GiftShopView
{
public partial class FormSupply : Form
{
private readonly List<GiftViewModel>? _GiftList;
private readonly List<ShopViewModel>? _shopsList;
IShopLogic _shopLogic;
IGiftLogic _GiftLogic;
public int ShopId
{
get
{
return
Convert.ToInt32(ShopComboBox.SelectedValue);
}
set
{
ShopComboBox.SelectedValue = value;
}
}
public int GiftId
{
get
{
return
Convert.ToInt32(GiftComboBox.SelectedValue);
}
set
{
GiftComboBox.SelectedValue = value;
}
}
public IGiftModel? GiftModel
{
get
{
if (_GiftList == null)
{
return null;
}
foreach (var elem in _GiftList)
{
if (elem.Id == GiftId)
{
return elem;
}
}
return null;
}
}
public int Count
{
get { return Convert.ToInt32(CountTextBox.Text); }
set
{ CountTextBox.Text = value.ToString(); }
}
public FormSupply(IGiftLogic GiftLogic, IShopLogic shopLogic)
{
InitializeComponent();
_shopLogic = shopLogic;
_GiftLogic = GiftLogic;
_GiftList = GiftLogic.ReadList(null);
_shopsList = shopLogic.ReadList(null);
if (_GiftList != null)
{
GiftComboBox.DisplayMember = "GiftName";
GiftComboBox.ValueMember = "Id";
GiftComboBox.DataSource = _GiftList;
GiftComboBox.SelectedItem = null;
}
if (_shopsList != null)
{
ShopComboBox.DisplayMember = "ShopName";
ShopComboBox.ValueMember = "Id";
ShopComboBox.DataSource = _shopsList;
ShopComboBox.SelectedItem = null;
}
}
private void SaveButton_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(CountTextBox.Text))
{
MessageBox.Show("Заполните поле Количество", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (GiftComboBox.SelectedValue == null)
{
MessageBox.Show("Выберите подарок", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (ShopComboBox.SelectedValue == null)
{
MessageBox.Show("Выберите магазин", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
int count = Convert.ToInt32(CountTextBox.Text);
bool res = _shopLogic.MakeSupply(
new ShopSearchModel() { Id = Convert.ToInt32(ShopComboBox.SelectedValue) },
_GiftLogic.ReadElement(new() { Id = Convert.ToInt32(GiftComboBox.SelectedValue) }),
count
);
if (!res)
{
throw new Exception("Ошибка при пополнении. Дополнительная информация в логах");
}
MessageBox.Show("Пополнение прошло успешно");
DialogResult = DialogResult.OK;
Close();
}
catch (Exception err)
{
MessageBox.Show("Ошибка пополнения");
return;
}
}
private void CancelButton_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -19,6 +19,10 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
@ -28,6 +32,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\GiftShopBusinessLogic\GiftShopBusinessLogic.csproj" /> <ProjectReference Include="..\GiftShopBusinessLogic\GiftShopBusinessLogic.csproj" />
<ProjectReference Include="..\GiftShopContracts\GiftShopContracts.csproj" /> <ProjectReference Include="..\GiftShopContracts\GiftShopContracts.csproj" />
<ProjectReference Include="..\GiftShopDatabaseImplement\GiftShopDatabaseImplement.csproj" />
<ProjectReference Include="..\GiftShopFileImplement\GiftShopFileImplement.csproj" /> <ProjectReference Include="..\GiftShopFileImplement\GiftShopFileImplement.csproj" />
<ProjectReference Include="..\GiftShopListImplement\GiftShopListImplement.csproj" /> <ProjectReference Include="..\GiftShopListImplement\GiftShopListImplement.csproj" />
</ItemGroup> </ItemGroup>

View File

@ -1,7 +1,7 @@
using GiftShopBusinessLogic.BusinessLogics; using GiftShopBusinessLogic.BusinessLogics;
using GiftShopContracts.BusinessLogicsContracts; using GiftShopContracts.BusinessLogicsContracts;
using GiftShopContracts.StoragesContracts; using GiftShopContracts.StoragesContracts;
using GiftShopFileImplement.Implements; using GiftShopDatabaseImplement.Implements;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging; using NLog.Extensions.Logging;
@ -32,9 +32,13 @@ namespace GiftShopView
services.AddTransient<IComponentStorage, ComponentStorage>(); services.AddTransient<IComponentStorage, ComponentStorage>();
services.AddTransient<IOrderStorage, OrderStorage>(); services.AddTransient<IOrderStorage, OrderStorage>();
services.AddTransient<IGiftStorage, GiftStorage>(); services.AddTransient<IGiftStorage, GiftStorage>();
services.AddTransient<IComponentLogic, ComponentLogic>(); services.AddTransient<IShopStorage, ShopStorage>();
services.AddTransient<IComponentLogic, ComponentLogic>();
services.AddTransient<IOrderLogic, OrderLogic>(); services.AddTransient<IOrderLogic, OrderLogic>();
services.AddTransient<IGiftLogic, GiftLogic>(); services.AddTransient<IGiftLogic, GiftLogic>();
services.AddTransient<IShopLogic, ShopLogic>();
services.AddTransient<FormMain>(); services.AddTransient<FormMain>();
services.AddTransient<FormComponent>(); services.AddTransient<FormComponent>();
services.AddTransient<FormComponents>(); services.AddTransient<FormComponents>();
@ -42,6 +46,18 @@ namespace GiftShopView
services.AddTransient<FormGift>(); services.AddTransient<FormGift>();
services.AddTransient<FormGiftComponent>(); services.AddTransient<FormGiftComponent>();
services.AddTransient<FormGifts>(); services.AddTransient<FormGifts>();
}
} services.AddTransient<FormSell>();
services.AddTransient<FormShops>();
services.AddTransient<FormShop>();
services.AddTransient<FormSupply>();
services.AddTransient<FormShopReplenishment>();
}
}
} }