че то вышло

This commit is contained in:
Milana Ievlewa 2024-05-30 10:31:39 +04:00
parent 1a4521db91
commit 69ecd03a47
3 changed files with 94 additions and 63 deletions

View File

@ -3,6 +3,7 @@ using BeautySalonContracts.BusinessLogicContracts;
using BeautySalonContracts.SearchModels;
using BeautySalonContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
using StorekeeperWebApp;
namespace BeautySalonRestApi.Controllers
{
@ -11,11 +12,13 @@ namespace BeautySalonRestApi.Controllers
private readonly ILogger logger;
private readonly ICosmeticLogic cosmetic;
private readonly IProcedureLogic procedure;
public CosmeticController(ILogger<CosmeticController> logger, ICosmeticLogic cosmetic)
public CosmeticController(ILogger<CosmeticController> logger, ICosmeticLogic cosmetic, IProcedureLogic procedure)
{
this.logger = logger;
this.cosmetic = cosmetic;
this.procedure = procedure;
}
[HttpGet]
@ -36,89 +39,117 @@ namespace BeautySalonRestApi.Controllers
}
[HttpGet]
public List<CosmeticViewModel>? Cosmetics()
public IActionResult Cosmetics()
{
try
if (APIStorekeeper.Worker == null)
{
var cosmetics = cosmetic.ReadList(null); // Получение списка косметики
List<CosmeticViewModel> cosmeticViewModels = new List<CosmeticViewModel>();
// Преобразование данных косметики в список CosmeticViewModel
foreach (var item in cosmetics)
{
CosmeticViewModel cosmeticViewModel = new CosmeticViewModel
{
// Присваивание значений свойствам модели CosmeticViewModel
// Например: cosmeticViewModel.Property = item.Property;
};
cosmeticViewModels.Add(cosmeticViewModel);
}
return cosmeticViewModels;
}
catch (Exception ex)
{
logger.LogError(ex, "Ошибка получения списка косметики");
throw;
return Redirect("~/Home/Enter");
}
return View(cosmetic.ReadList(null));
}
[HttpGet]
public List<CosmeticViewModel>? GetCosmetics(int laborcostId)
public IActionResult CreateCosmetics()
{
try
if (APIStorekeeper.Worker == null)
{
return cosmetic.ReadList(new CosmeticSearchModel { LaborCostId = laborcostId });
return Redirect("~/Home/Enter");
}
catch (Exception ex)
ViewBag.Procedures = procedure.ReadList(new ProcedureSearchModel
{
logger.LogError(ex, "Ошибка получения списка косметики с id трудозатраты = {Id}", laborcostId);
throw;
WorkerId = APIStorekeeper.Worker.Id,
});
return View();
}
[HttpPost]
public void CreateCosmetics(string name, string brand, double price, int laborCost)
{
if (APIStorekeeper.Worker == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(name) || laborCost <= 0)
{
throw new Exception("Введены не все данные!");
}
cosmetic.Create(new CosmeticBindingModel
{
CosmeticName = name,
Brand = brand,
CosmeticPrice = price,
LaborCostId = laborCost
});
Response.Redirect("/Cosmetic/Cosmetics");
}
[HttpGet]
public IActionResult UpdateCosmetics(int id)
{
if (APIStorekeeper.Worker == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Procedures = procedure.ReadList(new ProcedureSearchModel
{
WorkerId = APIStorekeeper.Worker.Id,
});
return View(cosmetic.ReadElement(new CosmeticSearchModel
{
Id = id
}));
}
[HttpPost]
public void CreateCosmetic(CosmeticBindingModel model)
public void UpdateCosmetics(int id, string name, string brand, double price, int laborCost)
{
try
if (APIStorekeeper.Worker == null)
{
cosmetic.Create(model);
throw new Exception("Необходимо авторизоваться!");
}
catch (Exception ex)
if (string.IsNullOrEmpty(name) || laborCost <= 0)
{
logger.LogError(ex, "Ошибка создания косметики");
throw;
throw new Exception("Введены не все данные!");
}
cosmetic.Update(new CosmeticBindingModel
{
Id = id,
CosmeticName = name,
Brand = brand,
CosmeticPrice = price,
LaborCostId = laborCost
});
Response.Redirect("/Cosmetic/Cosmetics");
}
[HttpPost]
public void UpdateCosmetic(CosmeticBindingModel model)
/// <summary>
/// Удалить болезнь
/// </summary>
/// <returns></returns>
[HttpPost]
public void DeleteDisease(int id)
{
try
if (APIStorekeeper.Worker == null)
{
cosmetic.Update(model);
throw new Exception("Необходимо авторизоваться!");
}
catch (Exception ex)
{
logger.LogError(ex, "Ошибка обновления косметики");
throw;
}
}
[HttpDelete]
public void DeleteCosmetic(CosmeticBindingModel model)
{
try
cosmetic.Delete(new CosmeticBindingModel
{
cosmetic.Delete(model);
}
catch (Exception ex)
{
logger.LogError(ex, "Ошибка удаления косметики");
throw;
}
Id = id
});
Response.Redirect("/Cosmetic/Cosmetics");
}
}
}

View File

@ -19,7 +19,7 @@
}
<p>
<a asp-action="CreateDisease">Создать косметику</a>
<a asp-action="CreateCosmetic">Создать косметику</a>
</p>
<table class="table">
@ -57,9 +57,9 @@
@section scripts {
<script>
function deleteDisease(id) {
if (confirm("Вы уверены, что хотите удалить болезнь?")) {
$.post('@Url.Action("DeleteDisease", "/Disease")' + '/' + id, function () {
function deleteCosmetic(id) {
if (confirm("Вы уверены, что хотите удалить косметику?")) {
$.post('@Url.Action("DeleteCosmetic", "/Cosmetic")' + '/' + id, function () {
window.location.reload();
});
}

View File

@ -43,7 +43,7 @@
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Reports">Отчет</a>
</li>
</ul>
</ul>w
</div>
</div>
</nav>