Вроде как последние фиксы
This commit is contained in:
parent
23c8471c77
commit
10ee9301d1
@ -75,13 +75,9 @@ namespace DiningRoomBusinessLogic.BusinessLogics
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool ConnectCardDrink(CardBindingModel model)
|
||||
{
|
||||
_logger.LogInformation("Connect Card {rId} with drink {aId}", model.Id, model.DrinkId);
|
||||
return _CardStorage.ConnectCardDrink(model);
|
||||
}
|
||||
|
||||
|
||||
public bool Delete(CardBindingModel model)
|
||||
public bool Delete(CardBindingModel model)
|
||||
{
|
||||
CheckModel(model, false);
|
||||
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||||
|
113
DiningRoom/DiningRoomBusinessLogic/BusinessLogics/DrinkLogic.cs
Normal file
113
DiningRoom/DiningRoomBusinessLogic/BusinessLogics/DrinkLogic.cs
Normal file
@ -0,0 +1,113 @@
|
||||
using DiningRoomContracts.BindingModels;
|
||||
using DiningRoomContracts.BusinessLogicContracts;
|
||||
using DiningRoomContracts.SearchModels;
|
||||
using DiningRoomContracts.StorageContracts;
|
||||
using DiningRoomContracts.ViewModels;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
|
||||
namespace DiningRoomBusinessLogic.BusinessLogics
|
||||
{
|
||||
public class DrinkLogic : IDrinkLogic
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IDrinkStorage _DrinkStorage;
|
||||
public DrinkLogic(ILogger<DrinkLogic> logger, IDrinkStorage DrinkStorage)
|
||||
{
|
||||
_logger = logger;
|
||||
_DrinkStorage = DrinkStorage;
|
||||
}
|
||||
public List<DrinkViewModel>? ReadList(DrinkSearchModel? model)
|
||||
{
|
||||
_logger.LogInformation("ReadList. DrinkName:{DrinkName}. Id:{ Id}", model?.DrinkName, model?.Id);
|
||||
var list = model == null ? _DrinkStorage.GetFullList() : _DrinkStorage.GetFilteredList(model);
|
||||
if (list == null)
|
||||
{
|
||||
_logger.LogWarning("ReadList return null list");
|
||||
return null;
|
||||
}
|
||||
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
||||
return list;
|
||||
}
|
||||
public DrinkViewModel? ReadElement(DrinkSearchModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
_logger.LogInformation("ReadElement. DrinkName:{DrinkName}. Id:{ Id}", model.DrinkName, model.Id);
|
||||
var element = _DrinkStorage.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(DrinkBindingModel model)
|
||||
{
|
||||
CheckModel(model);
|
||||
if (_DrinkStorage.Insert(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Insert operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public bool Update(DrinkBindingModel model)
|
||||
{
|
||||
CheckModel(model);
|
||||
if (_DrinkStorage.Update(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Update operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public bool Delete(DrinkBindingModel model)
|
||||
{
|
||||
CheckModel(model, false);
|
||||
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||||
if (_DrinkStorage.Delete(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Delete operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private void CheckModel(DrinkBindingModel model, bool withParams = true)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
if (!withParams)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (string.IsNullOrEmpty(model.DrinkName))
|
||||
{
|
||||
throw new ArgumentNullException("Нет названия напитка", nameof(model.DrinkName));
|
||||
}
|
||||
if (model.Cost <= 0)
|
||||
{
|
||||
throw new ArgumentNullException("Цена напитка должна быть больше 0", nameof(model.Cost));
|
||||
}
|
||||
_logger.LogInformation("Drink. DrinkName:{DrinkName}. Cost:{ Cost}. Id: { Id}", model.DrinkName, model.Cost, model.Id);
|
||||
var element = _DrinkStorage.GetElement(new DrinkSearchModel
|
||||
{
|
||||
DrinkName = model.DrinkName
|
||||
});
|
||||
if (element != null && element.Id != model.Id)
|
||||
{
|
||||
throw new InvalidOperationException("Напиток с таким названием уже есть");
|
||||
}
|
||||
}
|
||||
public bool ConnectCardDrink(DrinkBindingModel model)
|
||||
{
|
||||
_logger.LogInformation("Connect Card {rId} with drink {aId}", model.Id, model.CardId);
|
||||
return _DrinkStorage.ConnectCardDrink(model);
|
||||
}
|
||||
}
|
||||
}
|
@ -16,6 +16,5 @@ namespace DiningRoomContracts.BusinessLogicContracts
|
||||
bool Create(CardBindingModel model);
|
||||
bool Update(CardBindingModel model);
|
||||
bool Delete(CardBindingModel model);
|
||||
bool ConnectCardDrink(CardBindingModel model);
|
||||
}
|
||||
}
|
||||
|
@ -15,5 +15,6 @@ namespace DiningRoomContracts.BusinessLogicContracts
|
||||
bool Update(DrinkBindingModel Model);
|
||||
|
||||
bool Delete(DrinkBindingModel Model);
|
||||
}
|
||||
bool ConnectCardDrink(DrinkBindingModel model);
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,9 @@
|
||||
|
||||
public int? UserId { get; set; }
|
||||
public DateTime? DateCardCreate { get; set; }
|
||||
public int? DrinkId { get; set; }
|
||||
public DateTime? DateFrom { get; set; }
|
||||
public DateTime? DateTo { get; set; }
|
||||
public int? DrinkId { get; set; }
|
||||
|
||||
public string? Cardname { get; set; }
|
||||
}
|
||||
|
@ -17,6 +17,5 @@ namespace DiningRoomContracts.StorageContracts
|
||||
CardViewModel? Update(CardBindingModel Model);
|
||||
|
||||
CardViewModel? Delete(CardBindingModel Model);
|
||||
bool ConnectCardDrink(CardBindingModel model);
|
||||
}
|
||||
}
|
||||
|
@ -17,5 +17,6 @@ namespace DiningRoomContracts.StorageContracts
|
||||
DrinkViewModel? Update(DrinkBindingModel Model);
|
||||
|
||||
DrinkViewModel? Delete(DrinkBindingModel Model);
|
||||
}
|
||||
bool ConnectCardDrink(DrinkBindingModel model);
|
||||
}
|
||||
}
|
||||
|
113
DiningRoom/DiningRoomDatabaseImplement/Implements/CardStorage.cs
Normal file
113
DiningRoom/DiningRoomDatabaseImplement/Implements/CardStorage.cs
Normal file
@ -0,0 +1,113 @@
|
||||
using DiningRoomContracts.BindingModels;
|
||||
using DiningRoomContracts.SearchModels;
|
||||
using DiningRoomContracts.StorageContracts;
|
||||
using DiningRoomContracts.ViewModels;
|
||||
using DiningRoomDatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DiningRoomDatabaseImplement.Implements
|
||||
{
|
||||
public class CardStorage : ICardStorage
|
||||
{
|
||||
//id пользователя учитывается в GetFilteredList
|
||||
public List<CardViewModel> GetFullList()
|
||||
{
|
||||
using var context = new DiningRoomDatabase();
|
||||
return context.Cards
|
||||
.Include(x => x.Drinks)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<CardViewModel> GetFilteredList(CardSearchModel model) {
|
||||
using var context = new DiningRoomDatabase();
|
||||
|
||||
//сортировка по дате создания заявки
|
||||
if (model.DateFrom.HasValue && model.DateTo.HasValue)
|
||||
{
|
||||
return context.Cards
|
||||
.Where(x => (x.UserId == model.UserId) && (x.DateCardCreate >= model.DateFrom && x.DateCardCreate <= model.DateTo))
|
||||
.Include(x => x.Drinks)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
//возвращение просто всех заявок пользователя (замена GetFullList)
|
||||
return context.Cards
|
||||
.Where(x => x.UserId == model.UserId)
|
||||
.Include(x => x.Drinks)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
//Поиск только по id, потому что другие поля не уникальные - нет смысла искать 1 элемент
|
||||
public CardViewModel? GetElement(CardSearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new DiningRoomDatabase();
|
||||
return context.Cards
|
||||
.Include(x => x.Drinks)
|
||||
.FirstOrDefault(x => x.Id == model.Id)
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
public CardViewModel? Insert(CardBindingModel model)
|
||||
{
|
||||
using var context = new DiningRoomDatabase();
|
||||
var newCard = Card.Create(context, model);
|
||||
if (newCard == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Cards.Add(newCard);
|
||||
context.SaveChanges();
|
||||
return newCard.GetViewModel;
|
||||
}
|
||||
|
||||
public CardViewModel? Update(CardBindingModel model)
|
||||
{
|
||||
using var context = new DiningRoomDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var Card = context.Cards.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (Card == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
Card.Update(model);
|
||||
context.SaveChanges();
|
||||
transaction.Commit();
|
||||
return Card.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public CardViewModel? Delete(CardBindingModel model)
|
||||
{
|
||||
using var context = new DiningRoomDatabase();
|
||||
var Card = context.Cards
|
||||
.Include(x => x.Drinks)
|
||||
.FirstOrDefault(y => y.Id == model.Id);
|
||||
if (Card != null)
|
||||
{
|
||||
context.Cards.Remove(Card);
|
||||
context.SaveChanges();
|
||||
return Card.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -102,5 +102,18 @@ namespace DiningRoomDatabaseImplement.Implements
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public bool ConnectCardDrink(DrinkBindingModel model)
|
||||
{
|
||||
using var context = new DiningRoomDatabase();
|
||||
var Card = context.Cards.FirstOrDefault(x => x.Id == model.Id);
|
||||
var Drink = context.Drinks.FirstOrDefault(x => x.Id == model.CardId);
|
||||
if (Card == null || Drink == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
Drink.ConnectDrink(context, model);
|
||||
context.SaveChanges();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -12,7 +12,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
namespace DiningRoomDatabaseImplement.Migrations
|
||||
{
|
||||
[DbContext(typeof(DiningRoomDatabase))]
|
||||
[Migration("20240503210711_InitialCreate")]
|
||||
[Migration("20240504170055_InitialCreate")]
|
||||
partial class InitialCreate
|
||||
{
|
||||
/// <inheritdoc />
|
@ -44,5 +44,6 @@ namespace DiningRoomDatabaseImplement.Models
|
||||
UserId = UserId,
|
||||
DateCardCreate = DateCardCreate,
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -112,5 +112,10 @@ namespace DiningRoomDatabaseImplement.Models
|
||||
}
|
||||
_drinkComponents = null;
|
||||
}
|
||||
public void ConnectDrink(DiningRoomDatabase context, DrinkBindingModel model)
|
||||
{
|
||||
CardId = model.CardId;
|
||||
Card = context.Cards.First(x => x.Id == model.CardId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user