ISEbd-21 Melnikov I. O. Lab Work 05 Advanced #31

Closed
Igor-Melnikov wants to merge 23 commits from lab5adv into lab5
15 changed files with 169 additions and 14 deletions
Showing only changes of commit e87910f41c - Show all commits

View File

@ -212,5 +212,17 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics
{
return _storeStorage.SellManufacture(manufacture, count);
}
public List<ManufactureInStoreViewModel> ReadManufacturesInStore(StoreSearchModel? model)
{
if (model != null)
{
var result = _storeStorage.GetManufacturesInStore(model);
return result;
}
else
{
return new();
}
}
}
}

View File

@ -0,0 +1,9 @@
namespace BlacksmithWorkshopContracts.BindingModels
{
public class FillingStoreBindingModel
{
public int Store { get; set; }
public int Manufacture { get; set; }
public int Count { get; set; }
}
}

View File

@ -23,5 +23,6 @@ namespace BlacksmithWorkshopContracts.BusinessLogicsContracts
int GetFreePlace(int count);
bool FillStore(IManufactureModel manufacture, int count);
public bool SellManufactures(IManufactureModel manufacture, int needCount);
public List<ManufactureInStoreViewModel> ReadManufacturesInStore(StoreSearchModel? model);
}
}

View File

@ -28,5 +28,6 @@ namespace BlacksmithWorkshopContracts.StoragesContracts
/// <returns></returns>
bool SellManufacture(IManufactureModel manufacture, int count);
int GetPlaceLeft(IStoreModel store);
List<ManufactureInStoreViewModel> GetManufacturesInStore(StoreSearchModel model);
}
}

View File

@ -0,0 +1,8 @@
namespace BlacksmithWorkshopContracts.ViewModels
{
public class ManufactureInStoreViewModel
{
public string ManufactureName { get; set; } = string.Empty;
public int Count { get; set; }
}
}

View File

@ -99,7 +99,10 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
return null;
}
store.Update(model);
store.UpdateManufactures(context, model);
if (model.Manufactures.Count != 0)
{
store.UpdateManufactures(context, model);
}
context.SaveChanges();
return store.GetViewModel;
}
@ -149,5 +152,12 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
}
return selectedStore.MaxManufactures - selectedStore.Manufactures.Select(x => x.Value.Item2).Sum();
}
public List<ManufactureInStoreViewModel> GetManufacturesInStore(StoreSearchModel model)
{
var store = GetElement(model);
List<ManufactureInStoreViewModel> result = store?.Manufactures.Select(x => new ManufactureInStoreViewModel() { ManufactureName = x.Value.Item1.ManufactureName, Count = x.Value.Item2}).ToList() ?? new();
return result;
}
}
}

View File

@ -122,5 +122,11 @@ namespace BlacksmithWorkshopFileImplement.Implements
}
return store.MaxManufactures - store.Manufactures.Select(x => x.Value.Item2).Sum();
}
public List<ManufactureInStoreViewModel> GetManufacturesInStore(StoreSearchModel model)
{
var store = GetElement(model);
List<ManufactureInStoreViewModel> result = store?.Manufactures.Select(x => new ManufactureInStoreViewModel() { ManufactureName = x.Value.Item1.ManufactureName, Count = x.Value.Item2 }).ToList() ?? new();
return result;
}
}
}

View File

@ -113,5 +113,11 @@ namespace BlacksmithWorkshopListImplement.Implements
{
throw new NotImplementedException("Не применяется в данной реализации");
}
public List<ManufactureInStoreViewModel> GetManufacturesInStore(StoreSearchModel model)
{
var store = GetElement(model);
List<ManufactureInStoreViewModel> result = store?.Manufactures.Select(x => new ManufactureInStoreViewModel() { ManufactureName = x.Value.Item1.ManufactureName, Count = x.Value.Item2 }).ToList() ?? new();
return result;
}
}
}

View File

@ -29,7 +29,7 @@ namespace BlacksmithWorkshopRestAPI.Controllers
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка продуктов");
_logger.LogError(ex, "Ошибка получения списка изделий");
throw;
}
}

View File

@ -1,6 +1,8 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.BusinessLogicsContracts;
using BlacksmithWorkshopContracts.SearchModels;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopDataModels.Models;
using Microsoft.AspNetCore.Mvc;
namespace BlacksmithWorkshopRestAPI.Controllers
@ -23,6 +25,12 @@ namespace BlacksmithWorkshopRestAPI.Controllers
var result = _logic.ReadList(null);
return result ?? new();
}
[HttpGet]
public StoreViewModel GetStore(int id)
{
StoreViewModel? result = _logic.ReadElement(new() { Id = id });
return result ?? new();
}
[HttpPost]
public void CreateStore(StoreBindingModel model)
{
@ -63,16 +71,30 @@ namespace BlacksmithWorkshopRestAPI.Controllers
}
}
[HttpPost]
public void FillStore(ManufactureBindingModel model, int count)
public void AddManufacture(FillingStoreBindingModel model)
{
try
{
_logic.FillStore(model, count);
_logic.AddManufacture(new ManufactureBindingModel() { Id = model.Manufacture }, new() { Id = model.Store }, model.Count);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при пополнении магазина");
}
}
[HttpGet]
public List<ManufactureInStoreViewModel> GetManufacturesInStore(int id)
{
try
{
var result = _logic.ReadManufacturesInStore(new() { Id = id });
return result;
}
catch(Exception ex)
{
_logger.LogError(ex, "Ошибка при поиске изделий");
throw;
}
}
}
}

View File

@ -29,7 +29,7 @@ namespace BlacksmithWorkshopStoreApp
throw new Exception(result);
}
}
public static void PostRequest<T>(string requestUrl, T model)
public static void PostRequest<T>(string requestUrl, T? model)
{
var json = JsonConvert.SerializeObject(model);
var data = new StringContent(json, Encoding.UTF8, "application/json");

View File

@ -1,5 +1,6 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopDataModels.Models;
using BlacksmithWorkshopStoreApp.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
@ -26,6 +27,7 @@ namespace BlacksmithWorkshopStoreApp.Controllers
[HttpGet]
public IActionResult Enter()
{
APIClient.AccessGranted = false;
return View();
}
[HttpPost]
@ -35,15 +37,15 @@ namespace BlacksmithWorkshopStoreApp.Controllers
{
APIClient.AccessGranted = true;
}
else
{
APIClient.AccessGranted = false;
}
return Redirect("~/Home/Index");
}
[HttpGet]
public IActionResult Create()
{
if (!APIClient.AccessGranted)
{
return Redirect("~/Home/Enter");
}
return View();
}
[HttpPost]
@ -52,6 +54,41 @@ namespace BlacksmithWorkshopStoreApp.Controllers
APIClient.PostRequest("/api/store/createstore", model);
return Redirect("~/Home/Index");
}
[HttpGet]
public IActionResult Update(int id)
{
StoreViewModel? store = APIClient.GetRequest<StoreViewModel>($"/api/store/getstore?id={id}");
List<ManufactureInStoreViewModel>? man = APIClient.GetRequest<List<ManufactureInStoreViewModel>>($"/api/store/getmanufacturesinstore?id={id}");
ViewBag.Store = store;
ViewBag.Manufactures = man;
return View();
}
[HttpPost]
public IActionResult Update(StoreViewModel model)
{
APIClient.PostRequest("/api/store/updatestore", model);
return Redirect("~/Home/Index");
}
public IActionResult Delete(int id)
{
APIClient.PostRequest($"/api/store/deletestore", new StoreBindingModel { Id = id });
return Redirect("~/Home/Index");
}
[HttpGet]
public IActionResult FillStore(int id)
{
ViewBag.Store = id;
var manufactures = APIClient.GetRequest<List<ManufactureViewModel>>("/api/main/getmanufacturelist");
ViewBag.Manufactures = manufactures;
return View();
}
[HttpPost]
public IActionResult FillStore(int store, int manufacture, int count)
{
FillingStoreBindingModel model = new() { Store = store, Manufacture = manufacture, Count = count };
APIClient.PostRequest($"/api/store/addmanufacture", model);
return Redirect($"~/Home/Update/{store}");
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{

View File

@ -0,0 +1,12 @@
@{
ViewData["Title"] = "Создание магазина";
}
<form method="post">
<div hidden><input name="store" value="@ViewBag.Store" /></div>
@foreach (var manufacture in ViewBag.Manufactures)
{
<p><input type="radio" id="@manufacture.Id" name="manufacture" value="@manufacture.Id">@manufacture.ManufactureName</p>
}
<p><input name="count" type="number">Количество</p>
<div><center><input type="submit" value="Добавить изделие" class="btn btn-primary" /></center></div>
</form>

View File

@ -20,13 +20,13 @@
@foreach (var store in ViewBag.Stores)
{
<tr>
<td>@store.StoreName</td>
<td name="StoreName">@store.StoreName</td>
<td>@store.Address</td>
<td>@store.OpeningDate</td>
<td>@store.OpeningDate.ToShortDateString()</td>
<td>@store.MaxManufactures</td>
<td><a href="">Изменить</a></td>
<td><a href="">Удалить</a></td>
<td><a href="">Поставить изделие</a></td>
<td><a href="/home/update/@store.Id">Изменить</a></td>
<td><a href="/home/delete/@store.Id">Удалить</a></td>
<td><a href="/home/fillstore/@store.Id">Поставить изделие</a></td>
</tr>
}
</tbody>

View File

@ -0,0 +1,31 @@
@{
ViewData["Title"] = "Изменение магазина";
}
<form method="post">
<div class="row">
<div class="col-4">Название магазина:</div>
<div class="col-8"><input value="@ViewBag.Store.StoreName" type="text" name="StoreName" /></div>
</div>
<div class="row">
<div class="col-4">Адрес:</div>
<div class="col-8"><input value="@ViewBag.Store.Address" type="text" name="Address" /></div>
</div>
<div class="row">
<div class="col-4">Дата открытия:</div>
<div class="col-8"><input value="@ViewBag.Store.OpeningDate.ToShortDateString()" type="date" name="OpeningDate" /></div>
</div>
<div hidden class="row">
<div class="col-4">Макс. изделий:</div>
<div class="col-8"><input value="@ViewBag.Store.MaxManufactures" type="text" name="MaxManufactures" /></div>
</div>
<hr />
@foreach (var manufacture in ViewBag.Manufactures)
{
<p>Изделие: @manufacture.ManufactureName</p>
<p>Количество: @manufacture.Count</p>
<hr />
}
<div class="row">
<div><button asp-controller="Home" asp-action="Update" class="btn btn-primary">Сохранить</button></div>
</div>
</form>