CRUD для изделий

This commit is contained in:
Володя 2023-05-17 23:03:47 +03:00
parent 524ef743f3
commit 61758e5737
7 changed files with 246 additions and 4 deletions

View File

@ -4,6 +4,7 @@ using SchoolAgainStudyContracts.BindingModel;
using SchoolAgainStudyContracts.BusinessLogicContracts;
using SchoolAgainStudyContracts.SearchModel;
using SchoolAgainStudyContracts.ViewModel;
using SchoolAgainStudyDataBaseImplements.Models;
using SchoolAgainStudyDataModels.Models;
using StudentWebClient.Models;
using System.Diagnostics;
@ -223,7 +224,7 @@ namespace StudentWebClient.Controllers
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (string.IsNullOrEmpty(title) && string.IsNullOrEmpty(description) || string.IsNullOrEmpty(dateCreate) && task <=0 || interests.Length==0)
if (string.IsNullOrEmpty(title) && string.IsNullOrEmpty(description) || string.IsNullOrEmpty(dateCreate) || task <=0 || interests.Length==0)
{
throw new Exception("Введите название и описане");
}
@ -244,7 +245,7 @@ namespace StudentWebClient.Controllers
DiyInterests = diyInterests
}) ;
Response.Redirect("Interests");
Response.Redirect("Diyes");
}
[HttpGet]
public IActionResult DiySetting(int id)
@ -312,6 +313,109 @@ namespace StudentWebClient.Controllers
});
Response.Redirect("/Home/Diyes");
}
[HttpGet]
public IActionResult Products()
{
if (APIClient.Student == null)
{
return Redirect("~/Home/Enter");
}
return View(_product.ReadList(new ProductSearchModel { StudentId = APIClient.Student.Id }));
}
[HttpGet]
public IActionResult CreateProduct()
{
var list = _interest.ReadList(new InterestSearchModel { StudentId = APIClient.Student.Id });
var simpInterest = list.Select(x => new { InterestId = x.Id, InterestName = x.Title });
ViewBag.Interests = new MultiSelectList(simpInterest, "InterestId", "InterestName");
return View();
}
[HttpPost]
public void CreateProduct(string title, string description, string dateCreate, int[] interests)
{
if (APIClient.Student == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (string.IsNullOrEmpty(title) && string.IsNullOrEmpty(description) || string.IsNullOrEmpty(dateCreate)|| interests.Length == 0)
{
throw new Exception("Введите название и описане");
}
Dictionary<int, IInterest> productInterests = new Dictionary<int, IInterest>();
foreach (int id in interests)
{
productInterests.Add(id, _interest.ReadElement(new InterestSearchModel { Id = id }));
}
_product.Create(new ProductBindingModel
{
StudentId = APIClient.Student.Id,
StudentName = APIClient.Student.Name,
Title = title,
Description = description,
DateCreate = DateTime.SpecifyKind(DateTime.Parse(dateCreate), DateTimeKind.Utc),
ProductInterests = productInterests
});
Response.Redirect("Products");
}
[HttpGet]
public IActionResult ProductSetting(int id)
{
var product = _product.ReadElement(new ProductSearchModel { Id = id });
var interests = _interest.ReadList(new InterestSearchModel { StudentId = APIClient.Student.Id }).Select(x => new { InterestId = x.Id, InterestName = x.Title }).ToList();
var selectedInterests = product.ProductInterests.Select(x => x.Key).ToArray();
ViewBag.Interests = new MultiSelectList(interests, "InterestId", "InterestName", selectedInterests);
return View(product);
}
[HttpPost]
public void UpdateProduct(int idDiy, string title, string description, string dateCreate, int[] interests)
{
if (string.IsNullOrEmpty(title))
{
throw new Exception("Нет названия");
}
if (string.IsNullOrEmpty(description))
{
throw new Exception("Нет описания");
}
if (string.IsNullOrEmpty(dateCreate))
{
throw new Exception("Нет даты");
}
if (interests.Length == 0)
{
throw new Exception("Нет интересов");
}
Dictionary<int, IInterest> productInterests = new Dictionary<int, IInterest>();
foreach (int id in interests)
{
productInterests.Add(id, _interest.ReadElement(new InterestSearchModel { Id = id }));
}
_product.Update(new ProductBindingModel
{
Id = idDiy,
StudentId = APIClient.Student.Id,
StudentName = APIClient.Student.Name,
Title = title,
Description = description,
DateCreate = DateTime.SpecifyKind(DateTime.Parse(dateCreate), DateTimeKind.Utc),
ProductInterests = productInterests
});
Response.Redirect("/Home/Products");
}
public void DeleteProduct(int id)
{
_product.Delete(new ProductBindingModel
{
Id = id,
});
Response.Redirect("/Home/Products");
}
}
}

View File

@ -0,0 +1,32 @@
@{
ViewData["Title"] = "CreateProduct";
}
<div class="text-center">
<h2 class="display-4">Создание изделия</h2>
</div>
<form method="post">
<div class="row">
<div class="col-4">Название:</div>
<div class="col-8"><input type="text" name="title" id="title" /></div>
</div>
<div class="row m-3">
<div class="col-4">Описание:</div>
<div class="col-8"><textarea cols="50" id="description" name="description" rows="5"></textarea>:</div>
</div>
<div class="row">
<div class="col-4">Дата создания:</div>
<div class="col-8">@Html.TextBox("dateCreate" ,"" ,new {type="Date"})</div>
</div>
<div class="row">
<div class="col-4">Интересы:</div>
<div class="col-8">
@Html.ListBox("interests", (MultiSelectList)ViewBag.Interests)
</div>
</div>
<div class="row">
<div class="col-8"></div>
<div class="col-4"><input type="submit" value="Создать" class="btn btn-primary" /></div>
</div>
</form>

View File

@ -19,11 +19,11 @@
</div>
<div class="row m-3">
<div class="col-4">Описание:</div>
<div class="col-8"><textarea cols="50" id="description" name="description" rows="5">@Model.Description</textarea>:</div>
<div class="col-8"><textarea cols="50" id="description" name="description" rows="5">@Model.Description</textarea></div>
</div>
<div class="row">
<div class="col-4">Дата создания:</div>
<div class="col-8">@Html.TextBox("dateCreate" ,"" ,new {type="Date"})</div>
<div class="col-8">@Html.TextBox("dateCreate", Model.DateCreate.Date.ToString("yyyy-MM-dd"),new {type="Date"})</div>
</div>
<div class="row">
<div class="col-4">Задание:</div>

View File

@ -34,6 +34,9 @@
<th>
Задание
</th>
<th>
Интересы
</th>
</tr>
</thead>
@ -50,6 +53,9 @@
<td>
@Html.DisplayFor(modelItem => item.TaskName)
</td>
<td>
<select asp-items="@(new SelectList(item.DiyInterests,"Key", "Value.Title"))"></select>
</td>
<td>
<a href="@Url.Action("DiySetting","Home", new {id=item.Id })"
class="btn btn-primary btn-lg">U</a>

View File

@ -0,0 +1,35 @@
@using SchoolAgainStudyContracts.ViewModel;
@model ProductViewModel
@{
ViewData["Title"] = "Setting";
}
<div class="text-center">
<h2 class="display-4">@Model.Title</h2>
</div>
<form method="post" action="@Url.Action("UpdateProduct", "Home", new{idDiy=Model.Id,title="#title",description="#description", dateCreate="#dateCreate",interests="#interests"})">
<div class="row m-3">
<div class="col-8">
<input type="submit" value="Обновить" class="col-md-4 btn btn-primary" />
<input type="button" class="col-md-4 ms-auto btn btn-danger" value="Удалить" onclick="location.href='@Url.Action("DeleteProduct","Home", new {id=Model.Id })'" />
</div>
</div>
<div class="row m-3">
<div class="col-4">Название:</div>
<div class="col-8"><input type="text" name="title" id="title" value="@Model.Title" /></div>
</div>
<div class="row m-3">
<div class="col-4">Описание:</div>
<div class="col-8"><textarea cols="50" id="description" name="description" rows="5">@Model.Description</textarea>:</div>
</div>
<div class="row">
<div class="col-4">Дата создания:</div>
<div class="col-8">@Html.TextBox("dateCreate" ,Model.DateCreate.Date.ToString("yyyy-MM-dd") ,new {type="Date"})</div>
</div>
<div class="row">
<div class="col-4">Интересы:</div>
<div class="col-8">
@Html.ListBox("interests", (MultiSelectList)ViewBag.Interests)
</div>
</div>
</form>

View File

@ -0,0 +1,62 @@
@using SchoolAgainStudyContracts.ViewModel;
@model List<ProductViewModel>
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">Изделия</h1>
</div>
<div class="text-center">
@{
if (Model == null)
{
<h3 class="display-4">Авторизируйтесь</h3>
return;
}
<p>
<a asp-action="CreateProduct">Создать изделие</a>
</p>
<table class="table">
<thead>
<tr>
<th>
Название
</th>
<th>
Дата создания
</th>
<th>
Интересы
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateCreate)
</td>
<td>
<select asp-items="@(new SelectList(item.ProductInterests,"Key", "Value.Title"))"></select>
</td>
<td>
<a href="@Url.Action("ProductSetting","Home", new {id=item.Id })"
class="btn btn-primary btn-lg">U</a>
</td>
</tr>
}
</tbody>
</table>
}
</div>

View File

@ -38,6 +38,9 @@
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Diyes">Поделки</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Products">Изделия</a>
</li>
</ul>
</div>