бизнес-логика пока не доделана, в 3 этапе не забыть про неё

This commit is contained in:
Kristina 2024-05-01 14:30:55 +04:00
parent b287e4b40e
commit 90be75609b
9 changed files with 167 additions and 8 deletions

View File

@ -15,16 +15,18 @@ namespace SchoolBusinessLogic.BusinessLogic
public class ItemForStudyLogic: IItemForStudyLogic
{
private readonly ILogger _logger;
private readonly IItemForStudyStorage _itemforstudyStorage;
public ItemForStudyLogic(ILogger<ItemForStudyLogic> logger, IItemForStudyStorage itemforstudyStorage)
private readonly IItemForStudyStorage _itemForStudyStorage;
private readonly IItemStorage _itemStorage;
public ItemForStudyLogic(ILogger<ItemForStudyLogic> logger, IItemForStudyStorage itemForStudyStorage, IItemStorage itemStorage)
{
_logger = logger;
_itemforstudyStorage = itemforstudyStorage;
_itemForStudyStorage = itemForStudyStorage;
_itemStorage = itemStorage;
}
public List<ItemForStudyViewModel>? ReadList(ItemForStudySearchModel? model)
{
_logger.LogInformation("ReadList. Id: {Id}", model?.Id);
var list = model == null ? _itemforstudyStorage.GetFullList() : _itemforstudyStorage.GetFilteredList(model);
var list = model == null ? _itemForStudyStorage.GetFullList() : _itemForStudyStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
@ -40,7 +42,7 @@ namespace SchoolBusinessLogic.BusinessLogic
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id: {Id}", model.Id);
var element = _itemforstudyStorage.GetElement(model);
var element = _itemForStudyStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
@ -52,7 +54,14 @@ namespace SchoolBusinessLogic.BusinessLogic
public bool Create(ItemForStudyBindingModel model)
{
CheckModel(model);
if (_itemforstudyStorage.Insert(model) == null)
//При добавлении привязки уменьшаем количество нужной статьи затрат
var item = _itemStorage.GetElement(new() { Id = model.ItemId });
if (_itemStorage.UpdateCount(item, -model.Count) == null)
{
_logger.LogWarning("Insert operation failed while updating count");
return false;
}
if (_itemForStudyStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
@ -62,7 +71,21 @@ namespace SchoolBusinessLogic.BusinessLogic
public bool Update(ItemForStudyBindingModel model)
{
CheckModel(model);
if (_itemforstudyStorage.Update(model) == null)
//Возвращаем нужное количество старых статей затрат на склад
var oldItem = _itemStorage.GetElement(new() { Id = (_itemForStudyStorage.GetElement(new() { Id = model.ItemId })?.ItemId ?? -1) });
if (_itemStorage.UpdateCount(oldItem, model.Count) == null)
{
_logger.LogWarning("Insert operation failed while returning old intem on storage");
return false;
}
//Списываем новую статью затрат со склада в новом количестве
var newItem = _itemStorage.GetElement(new() { Id = model.ItemId });
if (_itemStorage.UpdateCount(newItem, -model.Count) == null)
{
_logger.LogWarning("Insert operation failed while updating count");
return false;
}
if (_itemForStudyStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
@ -73,7 +96,7 @@ namespace SchoolBusinessLogic.BusinessLogic
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_itemforstudyStorage.Delete(model) == null)
if (_itemForStudyStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
@ -90,6 +113,14 @@ namespace SchoolBusinessLogic.BusinessLogic
{
return;
}
//Получаем текущее число статей затрат выбранного вида на обучении
int currentItemOnStorage = _itemStorage.GetElement(new() { Id = model.ItemId })?.Count ?? -1;
//Если на складе осталось статей затрат выбранного вида меньше, чем нужно на обучение, отменяем добавление привязки
if (currentItemOnStorage < model.Count)
{
_logger.LogWarning("Not enough items {Id} on storage", model.ItemId);
throw new ArgumentException("На обучении не хватает курсов");
}
_logger.LogInformation("ItemForStudy. Id: {Id}", model.Id);
}
}

View File

@ -90,6 +90,25 @@ namespace SchoolBusinessLogic.BusinessLogic
{
return;
}
//Заполнено ли название?
if (string.IsNullOrEmpty(model.Name))
{
_logger.LogWarning("Item name is empty");
throw new ArgumentException("Не введено название");
}
//Название уникально?
var existingItem = _itemStorage.GetElement(new() { Name = model.Name });
if (existingItem != null)
{
_logger.LogWarning("Item name is not unique");
throw new ArgumentException("Курс с таким названием уже есть");
}
//Цена больше 0?
if (model.Price <= 0)
{
_logger.LogWarning("Courses Price is <= 0");
throw new ArgumentException("Цена должна быть больше 0");
}
_logger.LogInformation("Item. Id: {Id}", model.Id);
}
}

View File

@ -1,4 +1,5 @@
using SchoolContracts.BindingModels;
using SchoolContracts.Models;
using SchoolContracts.SearchModels;
using SchoolContracts.ViewModels;
using System;
@ -17,5 +18,6 @@ namespace SchoolContracts.StorageContracts
ItemViewModel? Insert(ItemBindingModel model);
ItemViewModel? Update(ItemBindingModel model);
ItemViewModel? Delete(ItemBindingModel model);
ItemViewModel? UpdateCount(IItemModel? model, int count);
}
}

View File

@ -89,5 +89,21 @@ namespace SchoolDatabase.Implements
context.SaveChanges();
return item.GetViewModel;
}
public ItemViewModel? UpdateCount(IItemModel? model, int count)
{
if (model == null)
{
return null;
}
using var context = new SchoolDbContext();
var item = context.Items.FirstOrDefault(x => x.Id == model.Id);
if (item == null)
{
return null;
}
item.UpdateCount(model, count);
return item.GetViewModel;
}
}
}

View File

@ -13,6 +13,18 @@ builder.Services.AddTransient<ICoursesLogic, CoursesLogic>();
builder.Services.AddTransient<ICoursesStorage, CoursesStorage>();
builder.Services.AddTransient<IWorkerLogic, WorkerLogic>();
builder.Services.AddTransient<IWorkerStorage, WorkerStorage>();
builder.Services.AddTransient<ICustomerLogic, CustomerLogic>();
builder.Services.AddTransient<ICustumerStorage, CustumerStorage>();
builder.Services.AddTransient<IStudyLogic, StudyLogic>();
builder.Services.AddTransient<IStudyStorage, StudyStorage>();
builder.Services.AddTransient<IItemLogic, ItemLogic>();
builder.Services.AddTransient<IItemStorage, ItemStorage>();
builder.Services.AddTransient<IItemForStudyLogic, ItemForStudyLogic>();
builder.Services.AddTransient<IItemForStudyStorage, ItemForStudyStorage>();
builder.Services.AddTransient<ICoursesForStudyLogic, CoursesForStudyLogic>();
builder.Services.AddTransient<ICoursesForStudyStorage, CoursesForStudyStorage>();
builder.Services.AddTransient<IPaymentLogic, PaymentLogic>();
builder.Services.AddTransient<IPaymentStorage, PaymentStorage>();
// Add services to the container.
builder.Services.AddControllersWithViews();

View File

@ -0,0 +1,14 @@
@{
ViewData["Title"] = "Занятие для обучения";
}
<h1>ОБучение № @ViewBag.Study.Id</h1>
<form method="post">
<div hidden><input name="id" value="@ViewBag.Study.Id" /></div>
@foreach (var item in ViewBag.Items)
{
<p><input type="radio" id="@item.Id" name="selecteditem" value="@item.Id">@item.Name</p>
}
<p><input name="count" type="number">Количество</p>
<div><center><input type="submit" value="Добавить занятие" class="btn btn-primary" /></center></div>
</form>

View File

@ -0,0 +1,14 @@
<div>
<h1>Обучение № @ViewBag.Study.Id</h1>
<p>Курс @ViewBag.Study.CoursesId</p>
<p>Клиент @ViewBag.Study.CustumerId</p>
<p>Курсы:</p>
@if (ViewBag.CoursesForStudy.Count != 0)
{
@foreach (var courses in ViewBag.CoursesForStudy)
{
<p><div>@(courses.CoursesId + " " + courses.Count + "шт.")</div></p>
}
}
<div>@ViewBag.Exception</div>
</div>

View File

@ -0,0 +1,17 @@
<div>
<h1>Заявка № @ViewBag.Study.Id</h1>
<p>Курс @ViewBag.Study.CoursesId</p>
<p>Клиент @ViewBag.Study.CustumerId</p>
<p>Затраты:</p>
@if (ViewBag.ItemsForStudy.Count != 0)
{
@foreach (var item in ViewBag.ItemsForStudy)
{
<p><div>@(item.ItemName + " " + item.Count + "шт.")</div></p>
}
}
<div>@ViewBag.Exception</div>
<form>
<a href="/Home/Attachment/@ViewBag.Study.Id" class="btn btn-primary">Добавить</a>
</form>
</div>

View File

@ -0,0 +1,34 @@
{
ViewData["Title"] = "Обучения на курсы";
}
<div class="text-center">
<h1 class="display-4">Обучения</h1>
@if (ViewBag.Studys.Count != 0)
{
<center>
<table>
<thead>
<tr>
<th>Дата оформления</th>
<th>Дата прохождения</th>
<th>Клиент</th>
</tr>
</thead>
<tbody>
@foreach (var study in ViewBag.Studys)
{
<tr>
<td>@study.DateCreated</td>
<td>@study.DateComplination</td>
<td>@study.CustomerId</td>
<td><a href="/Home/Study/@study.Id">Затраты</a></td>
<td><a href="/Home/CoursesForStudy/@study.Id">Курсы</a></td>
</tr>
}
</tbody>
</table>
</center>
}
<div>@ViewBag.Exception</div>
</div>