начало
This commit is contained in:
parent
b9adeb5326
commit
1bf503cc92
@ -17,10 +17,14 @@ namespace ComputersShopBusinessLogic.BusinessLogics
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IOrderStorage _orderStorage;
|
||||
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
|
||||
private readonly IShopLogic _shopLogic;
|
||||
private readonly IComputerStorage _computerStorage;
|
||||
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopLogic shopLogic, IComputerStorage computerStorage)
|
||||
{
|
||||
_logger = logger;
|
||||
_orderStorage = orderStorage;
|
||||
_shopLogic = shopLogic;
|
||||
_computerStorage = computerStorage;
|
||||
}
|
||||
public bool CreateOrder(OrderBindingModel model)
|
||||
{
|
||||
@ -52,16 +56,29 @@ namespace ComputersShopBusinessLogic.BusinessLogics
|
||||
return false;
|
||||
}
|
||||
model.Status = newStatus;
|
||||
if (model.Status == OrderStatus.Готов) model.DateImplement = DateTime.Now;
|
||||
if (model.Status == OrderStatus.Готов)
|
||||
{
|
||||
|
||||
model.DateImplement = DateTime.Now;
|
||||
var computer = _computerStorage.GetElement(new() { Id = viewModel.ComputerId });
|
||||
if (computer == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(computer));
|
||||
}
|
||||
if (!_shopLogic.AddComputers(computer, viewModel.Count))
|
||||
{
|
||||
throw new Exception($"AddComputers operation failed - нет места");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
model.DateImplement = viewModel.DateImplement;
|
||||
}
|
||||
CheckModel(model);
|
||||
CheckModel(model, false);
|
||||
if (_orderStorage.Update(model) == null)
|
||||
{
|
||||
model.Status--;
|
||||
_logger.LogWarning("Update operation failed");
|
||||
_logger.LogWarning("Change status operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
@ -8,6 +8,7 @@ using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@ -43,7 +44,10 @@ namespace ComputersShopBusinessLogic.BusinessLogics
|
||||
return false;
|
||||
}
|
||||
|
||||
_logger.LogInformation("AddComputerInShop find. Id:{Id}", element.Id);
|
||||
if (element.Capacity - element.Computers.Select(x => x.Value.Item2).Sum() < quantity)
|
||||
{
|
||||
throw new ArgumentNullException("В магазине не хватает места", nameof(quantity));
|
||||
}
|
||||
|
||||
if (element.Computers.TryGetValue(computer.Id, out var pair))
|
||||
{
|
||||
@ -62,11 +66,63 @@ namespace ComputersShopBusinessLogic.BusinessLogics
|
||||
ShopAddress = element.ShopAddress,
|
||||
ShopName = element.ShopName,
|
||||
DateOpening = element.DateOpening,
|
||||
Computers = element.Computers
|
||||
Computers = element.Computers,
|
||||
Capacity = element.Capacity,
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool AddComputers(IComputerModel computer, int quantity)
|
||||
{
|
||||
if (computer == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(computer));
|
||||
}
|
||||
if (quantity <= 0)
|
||||
{
|
||||
throw new ArgumentException("Количество документов должно быть больше 0", nameof(quantity));
|
||||
}
|
||||
_logger.LogInformation("AddComputers. ShopName:{ShopName}. Id:{Id}", computer.ComputerName, computer.Id);
|
||||
var allFreeQuantity = _shopStorage.GetFullList().Select(x => x.Capacity - x.Computers.Select(x => x.Value.Item2).Sum()).Sum();
|
||||
if (allFreeQuantity < quantity)
|
||||
{
|
||||
_logger.LogWarning("AddComputers operation failed.");
|
||||
return false;
|
||||
}
|
||||
foreach (var shop in _shopStorage.GetFullList())
|
||||
{
|
||||
int freeQuantity = shop.Capacity - shop.Computers.Select(x => x.Value.Item2).Sum();
|
||||
if (freeQuantity <= 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (freeQuantity < quantity)
|
||||
{
|
||||
if (!AddComputer(new() { Id = shop.Id }, computer, freeQuantity))
|
||||
{
|
||||
_logger.LogWarning("AddComputers operation failed.");
|
||||
return false;
|
||||
}
|
||||
quantity -= freeQuantity;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!AddComputer(new() { Id = shop.Id }, computer, quantity))
|
||||
{
|
||||
_logger.LogWarning("AddComputers operation failed.");
|
||||
return false;
|
||||
}
|
||||
quantity = 0;
|
||||
}
|
||||
if (quantity == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
_logger.LogWarning("AddComputers operation failed.");
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Create(ShopBindingModel model)
|
||||
{
|
||||
CheckModel(model);
|
||||
@ -131,6 +187,11 @@ namespace ComputersShopBusinessLogic.BusinessLogics
|
||||
return list;
|
||||
}
|
||||
|
||||
public bool SellComputers(IComputerModel computer, int quantity)
|
||||
{
|
||||
return _shopStorage.SellComputers(computer, quantity);
|
||||
}
|
||||
|
||||
public bool Update(ShopBindingModel model)
|
||||
{
|
||||
CheckModel(model, false);
|
||||
|
@ -14,5 +14,6 @@ namespace ComputersShopContracts.BindingModels
|
||||
public string ShopAddress { get; set; } = string.Empty;
|
||||
public DateTime DateOpening { get; set; } = DateTime.Now;
|
||||
public Dictionary<int, (IComputerModel, int)> Computers { get; set; } = new();
|
||||
public int Capacity { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -18,5 +18,7 @@ namespace ComputersShopContracts.BusinessLogicContracts
|
||||
bool Update(ShopBindingModel model);
|
||||
bool Delete(ShopBindingModel model);
|
||||
bool AddComputer(ShopSearchModel model, IComputerModel computer, int quantity);
|
||||
bool AddComputers(IComputerModel computer, int quantity);
|
||||
bool SellComputers(IComputerModel computer, int quantity);
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
using ComputersShopContracts.BindingModels;
|
||||
using ComputersShopContracts.SearchModels;
|
||||
using ComputersShopContracts.ViewModels;
|
||||
using ComputersShopDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -17,5 +18,6 @@ namespace ComputersShopContracts.StoragesContracts
|
||||
ShopViewModel? Insert(ShopBindingModel model);
|
||||
ShopViewModel? Update(ShopBindingModel model);
|
||||
ShopViewModel? Delete(ShopBindingModel model);
|
||||
bool SellComputers(IComputerModel model, int quantity);
|
||||
}
|
||||
}
|
||||
|
@ -19,5 +19,7 @@ namespace ComputersShopContracts.ViewModels
|
||||
public string ShopAddress { get; set; } = string.Empty;
|
||||
[DisplayName("Дата открытия")]
|
||||
public DateTime DateOpening { get; set; } = DateTime.Now;
|
||||
[DisplayName("Вместимость магазина")]
|
||||
public int Capacity { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,8 @@ namespace ComputersShopDataModels.Models
|
||||
public string ShopName { get; }
|
||||
public string ShopAddress { get; }
|
||||
DateTime DateOpening { get; }
|
||||
|
||||
public int Capacity { get; }
|
||||
Dictionary<int, (IComputerModel, int)> Computers { get; }
|
||||
}
|
||||
}
|
||||
|
@ -14,9 +14,11 @@ namespace ComputersShopFileImplement
|
||||
private readonly string ComponentFileName = "Component.xml";
|
||||
private readonly string OrderFileName = "Order.xml";
|
||||
private readonly string ComputerFileName = "Computer.xml";
|
||||
private readonly string ShopFileName = "Shop.xml";
|
||||
public List<Component> Components { get; private set; }
|
||||
public List<Order> Orders { get; private set; }
|
||||
public List<Computer> Computers { get; private set; }
|
||||
public List<Shop> Shops { get; private set; }
|
||||
public static DataFileSingleton GetInstance()
|
||||
{
|
||||
if (instance == null)
|
||||
@ -28,11 +30,13 @@ namespace ComputersShopFileImplement
|
||||
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement);
|
||||
public void SaveComputers() => SaveData(Computers, ComputerFileName, "Computers", x => x.GetXElement);
|
||||
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
|
||||
public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement);
|
||||
private DataFileSingleton()
|
||||
{
|
||||
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
|
||||
Computers = LoadData(ComputerFileName, "Computer", x => Computer.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)
|
||||
{
|
||||
|
@ -0,0 +1,125 @@
|
||||
using ComputersShopContracts.BindingModels;
|
||||
using ComputersShopContracts.SearchModels;
|
||||
using ComputersShopContracts.StoragesContracts;
|
||||
using ComputersShopContracts.ViewModels;
|
||||
using ComputersShopDataModels.Models;
|
||||
using ComputersShopFileImplement.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ComputersShopFileImplement.Implements
|
||||
{
|
||||
public class ShopStorage : IShopStorage
|
||||
{
|
||||
private readonly DataFileSingleton source;
|
||||
|
||||
public ShopStorage()
|
||||
{
|
||||
source = DataFileSingleton.GetInstance();
|
||||
}
|
||||
public ShopViewModel? Delete(ShopBindingModel model)
|
||||
{
|
||||
var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (shop != null)
|
||||
{
|
||||
source.Shops.Remove(shop);
|
||||
source.SaveShops();
|
||||
return shop.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
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 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 List<ShopViewModel> GetFullList()
|
||||
{
|
||||
return source.Shops.Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
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 bool SellComputers(IComputerModel model, int quantity)
|
||||
{
|
||||
int availableQuantity = source.Shops.Select(x => x.Computers.FirstOrDefault(y => y.Key == model.Id).Value.Item2).Sum();
|
||||
if (availableQuantity < quantity)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var shops = source.Shops.Where(x => x.Computers.ContainsKey(model.Id));
|
||||
foreach (var shop in shops)
|
||||
{
|
||||
int countInCurrentShop = shop.Computers[model.Id].Item2;
|
||||
if (countInCurrentShop <= quantity)
|
||||
{
|
||||
shop.Computers[model.Id] = (shop.Computers[model.Id].Item1, 0);
|
||||
quantity -= countInCurrentShop;
|
||||
}
|
||||
else
|
||||
{
|
||||
shop.Computers[model.Id] = (shop.Computers[model.Id].Item1, countInCurrentShop - quantity);
|
||||
quantity = 0;
|
||||
}
|
||||
Update(new ShopBindingModel
|
||||
{
|
||||
Id = shop.Id,
|
||||
ShopName = shop.ShopName,
|
||||
ShopAddress = shop.ShopAddress,
|
||||
DateOpening = shop.DateOpening,
|
||||
Computers = shop.Computers,
|
||||
Capacity = shop.Capacity
|
||||
});
|
||||
if (quantity == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public ShopViewModel? Update(ShopBindingModel model)
|
||||
{
|
||||
var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (shop == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
shop.Update(model);
|
||||
source.SaveShops();
|
||||
return shop.GetViewModel;
|
||||
}
|
||||
}
|
||||
}
|
108
ComputersShop/ComputersShopFileImplement/Models/Shop.cs
Normal file
108
ComputersShop/ComputersShopFileImplement/Models/Shop.cs
Normal file
@ -0,0 +1,108 @@
|
||||
using ComputersShopContracts.BindingModels;
|
||||
using ComputersShopContracts.ViewModels;
|
||||
using ComputersShopDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace ComputersShopFileImplement.Models
|
||||
{
|
||||
public class Shop : IShopModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
public string ShopName { get; private set; } = string.Empty;
|
||||
public string ShopAddress { get; private set; } = string.Empty;
|
||||
public DateTime DateOpening { get; private set; }
|
||||
public int Capacity { get; private set; }
|
||||
public Dictionary<int, int> ComputersCount = new();
|
||||
public Dictionary<int, (IComputerModel, int)>? _computers = null;
|
||||
public Dictionary<int, (IComputerModel, int)> Computers
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_computers == null)
|
||||
{
|
||||
var source = DataFileSingleton.GetInstance();
|
||||
_computers = ComputersCount.ToDictionary(
|
||||
x => x.Key,
|
||||
y => ((source.Computers.FirstOrDefault(z => z.Id == y.Key) as IComputerModel)!,
|
||||
y.Value)
|
||||
);
|
||||
}
|
||||
return _computers;
|
||||
}
|
||||
}
|
||||
public static Shop? Create(ShopBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Shop()
|
||||
{
|
||||
Id = model.Id,
|
||||
ShopName = model.ShopName,
|
||||
ShopAddress = model.ShopAddress,
|
||||
DateOpening = model.DateOpening,
|
||||
Capacity = model.Capacity,
|
||||
ComputersCount = model.Computers.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,
|
||||
ShopAddress = element.Element("Address")!.Value,
|
||||
DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value),
|
||||
Capacity = Convert.ToInt32(element.Element("Capacity")!.Value),
|
||||
ComputersCount = element.Element("Computers")!.Elements("Computer")
|
||||
.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;
|
||||
ShopAddress = model.ShopAddress;
|
||||
DateOpening = model.DateOpening;
|
||||
Capacity = model.Capacity;
|
||||
ComputersCount = model.Computers.ToDictionary(x => x.Key, x => x.Value.Item2);
|
||||
_computers = null;
|
||||
|
||||
}
|
||||
public ShopViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
ShopName = ShopName,
|
||||
ShopAddress = ShopAddress,
|
||||
DateOpening = DateOpening,
|
||||
Capacity = Capacity,
|
||||
Computers = Computers
|
||||
};
|
||||
public XElement GetXElement => new("Shop",
|
||||
new XAttribute("Id", Id),
|
||||
new XElement("ShopName", ShopName),
|
||||
new XElement("Address", ShopAddress),
|
||||
new XElement("DateOpening", DateOpening.ToString()),
|
||||
new XElement("Capacity", Capacity.ToString()),
|
||||
new XElement("Documents", ComputersCount.Select(x =>
|
||||
new XElement("Document",
|
||||
new XElement("Key", x.Key),
|
||||
new XElement("Value", x.Value)))
|
||||
.ToArray()));
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
using ComputersShopContracts.SearchModels;
|
||||
using ComputersShopContracts.StoragesContracts;
|
||||
using ComputersShopContracts.ViewModels;
|
||||
using ComputersShopDataModels.Models;
|
||||
using ComputersShopListImplement.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@ -107,6 +108,11 @@ namespace ComputersShopListImplement.Implements
|
||||
return newShop.GetViewModel;
|
||||
}
|
||||
|
||||
public bool SellComputers(IComputerModel model, int quantity)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ShopViewModel? Update(ShopBindingModel model)
|
||||
{
|
||||
foreach (var Shop in _source.Shops)
|
||||
|
@ -55,5 +55,7 @@ namespace ComputersShopListImplement.Models
|
||||
DateOpening = DateOpening,
|
||||
Computers = Computers
|
||||
};
|
||||
|
||||
public int Capacity => throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user