PIbd-22. Bulatova K.R. LabWork_03_Hard #12

Closed
bulatova_karina wants to merge 3 commits from LabWork_03_Hard into LabWork_02_Hard
9 changed files with 397 additions and 67 deletions
Showing only changes of commit 7ce7fe4c2d - Show all commits

View File

@ -24,5 +24,7 @@ namespace ComputersShopDatabaseImplement
public virtual DbSet<Computer> Computers { set; get; }
public virtual DbSet<ComputerComponent> ComputerComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Shop> Shops { set; get; }
public virtual DbSet<ShopComputer> ShopComputers { set; get; }
}
}

View File

@ -16,17 +16,22 @@ namespace ComputersShopDatabaseImplement.Implements
public List<ComponentViewModel> GetFullList()
{
using var context = new ComputersShopDatabase();
return context.Components.Select(x => x.GetViewModel).ToList();
return context.Components
.Select(x => x.GetViewModel)
.ToList();
}
public List<ComponentViewModel> GetFiltredList(ComponentSearchModel model)
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName))
{
return new();
}
using var context = new ComputersShopDatabase();
return context.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).
Select(x => x.GetViewModel).ToList();
return context.Components
.Where(x => x.ComponentName.Contains(model.ComponentName))
.Select(x => x.GetViewModel)
.ToList();
}
public ComponentViewModel? GetElement(ComponentSearchModel model)
{
@ -34,9 +39,13 @@ namespace ComputersShopDatabaseImplement.Implements
{
return null;
}
using var context = new ComputersShopDatabase();
return context.Components.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName)
&& x.ComponentName == model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
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)
{
@ -45,6 +54,7 @@ namespace ComputersShopDatabaseImplement.Implements
{
return null;
}
using var context = new ComputersShopDatabase();
context.Components.Add(newComponent);
context.SaveChanges();
@ -58,6 +68,7 @@ namespace ComputersShopDatabaseImplement.Implements
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
@ -66,13 +77,14 @@ namespace ComputersShopDatabaseImplement.Implements
{
using var context = new ComputersShopDatabase();
var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
if (element == null)
{
context.Components.Remove(element);
context.SaveChanges();
return element.GetViewModel;
return null;
}
return null;
context.Components.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
}
}

View File

@ -9,6 +9,7 @@ using ComputersShopContracts.BindingModels;
using ComputersShopContracts.ViewModels;
using ComputersShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System.Numerics;
namespace ComputersShopDatabaseImplement.Implements
{
@ -17,19 +18,28 @@ namespace ComputersShopDatabaseImplement.Implements
public List<ComputerViewModel> GetFullList()
{
using var context = new ComputersShopDatabase();
return context.Computers.Include(x => x.Components).ThenInclude(x => x.Component)
.ToList().Select(x => x.GetViewModel).ToList();
return context.Computers
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ComputerViewModel> GetFiltredList(ComputerSearchModel model)
public List<ComputerViewModel> GetFilteredList(ComputerSearchModel model)
{
if (string.IsNullOrEmpty(model.ComputerName))
{
return new();
}
using var context = new ComputersShopDatabase();
return context.Computers.Include(x => x.Components).ThenInclude(x => x.Component)
.Where(x => x.ComputerName.Contains(model.ComputerName)).ToList()
.Select(x => x.GetViewModel).ToList();
return context.Computers
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Where(x => x.ComputerName.Contains(model.ComputerName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public ComputerViewModel? GetElement(ComputerSearchModel model)
{
@ -37,11 +47,15 @@ namespace ComputersShopDatabaseImplement.Implements
{
return null;
}
using var context = new ComputersShopDatabase();
return context.Computers.Include(x => x.Components).ThenInclude(x => x.Component)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComputerName) &&
x.ComputerName == model.ComputerName) ||
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
return context.Computers
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComputerName) &&
x.ComputerName == model.ComputerName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public ComputerViewModel? Insert(ComputerBindingModel model)
{
@ -51,6 +65,7 @@ namespace ComputersShopDatabaseImplement.Implements
{
return null;
}
context.Computers.Add(newComputer);
context.SaveChanges();
return newComputer.GetViewModel;
@ -66,6 +81,7 @@ namespace ComputersShopDatabaseImplement.Implements
{
return null;
}
computer.Update(model);
context.SaveChanges();
computer.UpdateComponents(context, model);
@ -81,7 +97,9 @@ namespace ComputersShopDatabaseImplement.Implements
public ComputerViewModel? Delete(ComputerBindingModel model)
{
using var context = new ComputersShopDatabase();
var element = context.Computers.Include(x => x.Components).FirstOrDefault(rec => rec.Id == model.Id);
var element = context.Computers
.Include(x => x.Components)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Computers.Remove(element);
@ -92,3 +110,4 @@ namespace ComputersShopDatabaseImplement.Implements
}
}
}

View File

@ -14,29 +14,36 @@ namespace ComputersShopDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public List<OrderViewModel> GetFullList()
{
using var context = new ComputersShopDatabase();
return context.Orders
.Select(x => GetViewModel(x))
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new ComputersShopDatabase();
return context.Orders
.Where(x => x.Id.Equals(model.Id))
.Select(x => GetViewModel(x))
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new ComputersShopDatabase();
return context.Orders.Include(x => x.Computer).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<OrderViewModel> GetFiltredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new ComputersShopDatabase();
return context.Orders.Where(x => x.Id == model.Id).Include(x => x.Computer)
.Select(x => x.GetViewModel).ToList();
}
public List<OrderViewModel> GetFullList()
{
using var context = new ComputersShopDatabase();
return context.Orders.Include(x => x.Computer).Select(x => x.GetViewModel).ToList();
return GetViewModel(context.Orders
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id)));
}
public OrderViewModel? Insert(OrderBindingModel model)
{
@ -45,10 +52,11 @@ namespace ComputersShopDatabaseImplement.Implements
{
return null;
}
using var context = new ComputersShopDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return context.Orders.Include(x => x.Computer).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel;
return GetViewModel(newOrder);
}
public OrderViewModel? Update(OrderBindingModel model)
{
@ -58,21 +66,34 @@ namespace ComputersShopDatabaseImplement.Implements
{
return null;
}
order.Update(model);
context.SaveChanges();
return context.Orders.Include(x => x.Computer).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
return GetViewModel(order);
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new ComputersShopDatabase();
var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
var element = context.Orders.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return element.GetViewModel;
return GetViewModel(element);
}
return null;
}
private static OrderViewModel GetViewModel(Order order)
{
using var context = new ComputersShopDatabase();
var viewModel = order.GetViewModel;
var computer = context.Computers.FirstOrDefault(x => x.Id == order.ComputerId);
if (computer != null)
{
viewModel.ComputerName = computer.ComputerName;
}
return viewModel;
}
}
}

View File

@ -0,0 +1,168 @@
using ComputersShopContracts.BindingModels;
using ComputersShopContracts.SearchModels;
using ComputersShopContracts.StoragesContracts;
using ComputersShopContracts.ViewModels;
using ComputersShopDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputersShopDatabaseImplement.Implements
{
public class ShopStorage : IShopStorage
{
public List<ShopViewModel> GetFullList()
{
using var context = new ComputersShopDatabase();
return context.Shops
.Include(x => x.Computers)
.ThenInclude(x => x.Computer)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName))
{
return new();
}
using var context = new ComputersShopDatabase();
return context.Shops
.Include(x => x.Computers)
.ThenInclude(x => x.Computer)
.Where(x => x.ShopName.Contains(model.ShopName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public ShopViewModel? GetElement(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue)
{
return null;
}
using var context = new ComputersShopDatabase();
return context.Shops
.Include(x => x.Computers)
.ThenInclude(x => x.Computer)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) &&
x.ShopName == model.ShopName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public ShopViewModel? Insert(ShopBindingModel model)
{
using var context = new ComputersShopDatabase();
var newShop = Shop.Create(context, model);
if (newShop == null)
{
return null;
}
context.Shops.Add(newShop);
context.SaveChanges();
return newShop.GetViewModel;
}
public ShopViewModel? Update(ShopBindingModel model)
{
using var context = new ComputersShopDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var shop = context.Shops.FirstOrDefault(rec => rec.Id == model.Id);
if (shop == null)
{
return null;
}
shop.Update(model);
context.SaveChanges();
shop.UpdateComputers(context, model);
transaction.Commit();
return shop.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public ShopViewModel? Delete(ShopBindingModel model)
{
using var context = new ComputersShopDatabase();
var element = context.Shops
.Include(x => x.Computers)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Shops.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public bool SellComputers(IComputerModel model, int count)
{
using var context = new ComputersShopDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var shops = context.Shops
.Include(x => x.Computers)
.ThenInclude(x => x.Computer)
.ToList()
.Where(x => x.ShopComputers.ContainsKey(model.Id));
foreach (var shop in shops)
{
int countInCurrentShop = shop.ShopComputers[model.Id].Item2;
if (countInCurrentShop <= count)
{
var elem = context.ShopComputers
.Where(x => x.ComputerId == model.Id)
.FirstOrDefault(x => x.ShopId == shop.Id);
context.ShopComputers.Remove(elem);
shop.ShopComputers.Remove(model.Id);
count -= countInCurrentShop;
}
else
{
shop.ShopComputers[model.Id] = (shop.ShopComputers[model.Id].Item1, countInCurrentShop - count);
count = 0;
shop.UpdateComputers(context, new ShopBindingModel
{
Id = shop.Id,
ShopName = shop.ShopName,
Address = shop.Address,
DateOpening = shop.DateOpening,
ShopComputers = shop.ShopComputers,
MaxComputers = shop.MaxComputers
});
}
if (count <= 0)
{
context.SaveChanges();
transaction.Commit();
return true;
}
}
transaction.Rollback();
return false;
}
catch
{
transaction.Rollback();
throw;
}
}
public bool CheckCount(IComputerModel model, int count)
{
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,104 @@
using ComputersShopContracts.BindingModels;
using ComputersShopContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ComputersShopDataModels.Models;
namespace ComputersShopDatabaseImplement.Models
{
public class Shop : IShopModel
{
public int Id { get; set; }
[Required]
public string ShopName { get; set; } = string.Empty;
[Required]
public string Address { get; set; } = string.Empty;
[Required]
public DateTime DateOpening { get; set; }
private Dictionary<int, (IComputerModel, int)>? _shopComputers = null;
[NotMapped]
public Dictionary<int, (IComputerModel, int)> ShopComputers
{
get
{
if (_shopComputers == null)
{
_shopComputers = Computers
.ToDictionary(recSP => recSP.ComputerId, recSP => (recSP.Computer as IComputerModel, recSP.Count));
}
return _shopComputers;
}
}
public int MaxComputers { get; set; }
[ForeignKey("ShopId")]
public List<ShopComputer> Computers { get; set; } = new();
public static Shop Create(ComputersShopDatabase context, ShopBindingModel model)
{
return new Shop
{
Id = model.Id,
ShopName = model.ShopName,
Address = model.Address,
DateOpening = model.DateOpening,
MaxComputers = model.MaxComputers,
Computers = model.ShopComputers.Select(x => new ShopComputer
{
Computer = context.Computers.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(ShopBindingModel model)
{
ShopName = model.ShopName;
Address = model.Address;
DateOpening = model.DateOpening;
MaxComputers = model.MaxComputers;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
Address = Address,
DateOpening = DateOpening,
ShopComputers = ShopComputers,
MaxComputers = MaxComputers
};
public void UpdateComputers(ComputersShopDatabase context, ShopBindingModel model)
{
var shopComputers = context.ShopComputers.Where(rec => rec.ShopId == model.Id).ToList();
if (shopComputers != null && shopComputers.Count > 0)
{
// Удаление изделий, которых нет в магазине
context.ShopComputers.RemoveRange(shopComputers.Where(rec => !model.ShopComputers.ContainsKey(rec.ComputerId)));
context.SaveChanges();
// Обновление количества у существующих записей
foreach (var updateComputers in shopComputers)
{
updateComputers.Count = model.ShopComputers[updateComputers.ComputerId].Item2;
model.ShopComputers.Remove(updateComputers.ComputerId);
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == Id);
foreach (var sp in model.ShopComputers)
{
context.ShopComputers.Add(new ShopComputer
{
Shop = shop,
Computer = context.Computers.First(x => x.Id == sp.Key),
Count = sp.Value.Item2
});
context.SaveChanges();
}
_shopComputers = null;
}
}
}

View File

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

View File

@ -86,10 +86,10 @@ namespace ComputersShopFileImplement.Implements
private OrderViewModel GetViewModel(Order order)
{
var viewModel = order.GetViewModel;
var plane = _source.Computers.FirstOrDefault(x => x.Id == order.ComputerId);
if (plane != null)
var computer = _source.Computers.FirstOrDefault(x => x.Id == order.ComputerId);
if (computer != null)
{
viewModel.ComputerName = plane.ComputerName;
viewModel.ComputerName = computer.ComputerName;
}
return viewModel;
}

View File

@ -15,21 +15,13 @@ namespace ComputersShopView
public partial class FormSell : Form
{
private readonly ILogger _logger;
/// <summary>
/// Бизнес-логика для изделий
/// </summary>
private readonly IComputerLogic _logicC;
/// <summary>
/// Бизнес-логика для магазинов
/// </summary>
private readonly IShopLogic _logicS;
public FormSell(ILogger<FormSell> logger, IComputerLogic planeLogic, IShopLogic shopLogic)
public FormSell(ILogger<FormSell> logger, IComputerLogic computerLogic, IShopLogic shopLogic)
{
InitializeComponent();
_logger = logger;
_logicC = planeLogic;
_logicC = computerLogic;
_logicS = shopLogic;
}
private void FormSell_Load(object sender, EventArgs e)
@ -52,12 +44,6 @@ namespace ComputersShopView
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Кнопка "Сохранить"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxCount.Text))
@ -92,12 +78,6 @@ namespace ComputersShopView
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Кнопка "Отмена"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;