Реализована логика сущности "Магазин"
This commit is contained in:
parent
873df1007e
commit
ed54dc6fbe
@ -1,12 +1,129 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ConfectioneryBusinessLogic.BusinessLogics;
|
||||
using ConfectioneryContracts.BindingModels;
|
||||
using ConfectioneryContracts.BusinessLogicsContracts;
|
||||
using ConfectioneryContracts.SearchModels;
|
||||
using ConfectioneryContracts.StoragesContract;
|
||||
using ConfectioneryContracts.ViewModels;
|
||||
using ConfectioneryDataModels.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace ConfectioneryBusinessLogic
|
||||
{
|
||||
internal class ShopLogic
|
||||
public class ShopLogic : IShopLogic
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IShopStorage _shopStorage;
|
||||
public ShopLogic(ILogger<ShopLogic> logger, IShopStorage shopStorage)
|
||||
{
|
||||
_logger = logger;
|
||||
_shopStorage = shopStorage;
|
||||
}
|
||||
public List<ShopViewModel>? ReadList(ShopSearchModel? model)
|
||||
{
|
||||
_logger.LogInformation("ReadList. ShopName:{ShopName}.Id:{ Id} ",
|
||||
model?.Name, 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 ShopViewModel? ReadElement(ShopSearchModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
_logger.LogInformation("ReadElement. ShopName:{ShopName}.Id:{ Id}",
|
||||
model.Name, 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 bool Create(ShopBindingModel model)
|
||||
{
|
||||
CheckModel(model);
|
||||
if (_shopStorage.Insert(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Insert operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public bool Update(ShopBindingModel model)
|
||||
{
|
||||
CheckModel(model);
|
||||
if (_shopStorage.Update(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Update 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;
|
||||
}
|
||||
private void CheckModel(ShopBindingModel model, bool withParams = true)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
if (!withParams)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (string.IsNullOrEmpty(model.Name))
|
||||
{
|
||||
throw new ArgumentNullException("Нет названия магазина",
|
||||
nameof(model.Name));
|
||||
}
|
||||
_logger.LogInformation("Shop. ShopName:{0}.Address:{1}. Id: {2}",
|
||||
model.Name, model.Address, model.Id);
|
||||
var element = _shopStorage.GetElement(new ShopSearchModel
|
||||
{
|
||||
Name = model.Name
|
||||
});
|
||||
if (element != null && element.Id != model.Id)
|
||||
{
|
||||
throw new InvalidOperationException("Магазин с таким названием уже есть");
|
||||
}
|
||||
}
|
||||
|
||||
public bool AddPastry(ShopSearchModel model, IPastryModel pastry, int count)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
_logger.LogInformation("AddPastryInShop. ShopName:{ShopName}.Id:{ Id}",
|
||||
model.Name, model.Id);
|
||||
var element = _shopStorage.GetElement(model);
|
||||
if (element == null)
|
||||
{
|
||||
_logger.LogWarning("AddPastryInShop element not found");
|
||||
return false;
|
||||
}
|
||||
_logger.LogInformation("AddPastryInShop find. Id:{Id}", element.Id);
|
||||
|
||||
return element.Pastries.TryAdd(pastry.Id, (pastry, count));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,11 +8,13 @@ namespace ConfectioneryListImplement
|
||||
public List<Component> Components { get; set; }
|
||||
public List<Order> Orders { get; set; }
|
||||
public List<Pastry> Pastry { get; set; }
|
||||
public List<Shop> Shop { get; set; }
|
||||
private DataListSingleton()
|
||||
{
|
||||
Components = new List<Component>();
|
||||
Orders = new List<Order>();
|
||||
Pastry = new List<Pastry>();
|
||||
Shop = new List<Shop>();
|
||||
}
|
||||
public static DataListSingleton GetInstance()
|
||||
{
|
||||
|
58
ConfectionaryListImplement/Shop.cs
Normal file
58
ConfectionaryListImplement/Shop.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using ConfectioneryContracts.BindingModels;
|
||||
using ConfectioneryContracts.ViewModels;
|
||||
using ConfectioneryDataModels;
|
||||
using ConfectioneryDataModels.Models;
|
||||
|
||||
namespace ConfectioneryListImplement
|
||||
{
|
||||
public class Shop : IShopModel
|
||||
{
|
||||
public string Name { get; private set; } = string.Empty;
|
||||
|
||||
public string Address { get; private set; } = string.Empty;
|
||||
|
||||
public DateTime DateOpening { get; private set; }
|
||||
|
||||
public Dictionary<int, (IPastryModel, int)> Pastries
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
} = new();
|
||||
|
||||
public int Id { get; private set; }
|
||||
|
||||
public static Shop? Create(ShopBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Shop()
|
||||
{
|
||||
Id = model.Id,
|
||||
Name = model.Name,
|
||||
Address = model.Address,
|
||||
DateOpening = model.DateOpening,
|
||||
Pastries = model.Pastries
|
||||
};
|
||||
}
|
||||
public void Update(ShopBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Name = model.Name;
|
||||
Address = model.Address;
|
||||
DateOpening = model.DateOpening;
|
||||
Pastries = model.Pastries;
|
||||
}
|
||||
public ShopViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
Name = Name,
|
||||
Address = Address,
|
||||
Pastries = Pastries
|
||||
};
|
||||
}
|
||||
}
|
46
ConfectionaryListImplement/ShopStorage.cs
Normal file
46
ConfectionaryListImplement/ShopStorage.cs
Normal file
@ -0,0 +1,46 @@
|
||||
using ConfectioneryContracts.BindingModels;
|
||||
using ConfectioneryContracts.SearchModels;
|
||||
using ConfectioneryContracts.StoragesContract;
|
||||
using ConfectioneryContracts.ViewModels;
|
||||
|
||||
namespace ConfectioneryListImplement
|
||||
{
|
||||
public class ShopStorage : IShopStorage
|
||||
{
|
||||
private readonly DataListSingleton _source;
|
||||
public ShopStorage()
|
||||
{
|
||||
_source = DataListSingleton.GetInstance();
|
||||
}
|
||||
|
||||
public ShopViewModel? Delete(ShopBindingModel model)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ShopViewModel? GetElement(ShopSearchModel model)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public List<ShopViewModel> GetFullList()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ShopViewModel? Insert(ShopBindingModel model)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ShopViewModel? Update(ShopBindingModel model)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
@ -11,7 +11,7 @@ namespace ConfectioneryContracts.BindingModels
|
||||
|
||||
public DateTime DateOpening { get; set; } = DateTime.Now;
|
||||
|
||||
public Dictionary<int, (IShopModel, int)> Pastries
|
||||
public Dictionary<int, (IPastryModel, int)> Pastries
|
||||
{
|
||||
get;
|
||||
set;
|
||||
|
@ -1,12 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ConfectioneryContracts.BindingModels;
|
||||
using ConfectioneryContracts.SearchModels;
|
||||
using ConfectioneryContracts.ViewModels;
|
||||
using ConfectioneryDataModels.Models;
|
||||
|
||||
namespace ConfectioneryContracts.BusinessLogicsContracts
|
||||
{
|
||||
internal interface IShopLogic
|
||||
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 AddPastry(ShopSearchModel model, IPastryModel pastry, int count);
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ConfectioneryContracts.BindingModels;
|
||||
using ConfectioneryContracts.SearchModels;
|
||||
using ConfectioneryContracts.ViewModels;
|
||||
|
||||
namespace ConfectioneryContracts.StoragesContract
|
||||
{
|
||||
internal interface IShopStorage
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ namespace ConfectioneryContracts.ViewModels
|
||||
[DisplayName("Время открытия")]
|
||||
public DateTime DateOpening { get; set; } = DateTime.Now;
|
||||
|
||||
public Dictionary<int, (IShopModel, int)> Pastries
|
||||
public Dictionary<int, (IPastryModel, int)> Pastries
|
||||
{
|
||||
get;
|
||||
set;
|
||||
|
@ -7,6 +7,6 @@ namespace ConfectioneryDataModels
|
||||
string Name { get; }
|
||||
string Address { get; }
|
||||
DateTime DateOpening { get; }
|
||||
Dictionary<int, (IShopModel, int)> Pastries { get; }
|
||||
Dictionary<int, (IPastryModel, int)> Pastries { get; }
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user