ISEbd-21_Melnikov_I.O._CarS.../CarService/CarServiceWebApp/Controllers/HomeController.cs

338 lines
9.0 KiB
C#
Raw Normal View History

2023-05-23 00:33:16 +04:00
using CarServiceContracts.BindingModels;
2023-05-22 22:44:51 +04:00
using CarServiceContracts.BusinessLogicsContracts;
2023-05-23 00:33:16 +04:00
using CarServiceContracts.SearchModels;
2023-05-22 22:44:51 +04:00
using CarServiceWebApp.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
namespace CarServiceWebApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
2023-05-22 22:44:51 +04:00
private readonly IWorkLogic _workLogic;
2023-05-23 00:33:16 +04:00
private readonly IWorkerLogic _workerLogic;
private readonly IItemLogic _itemLogic;
2023-05-25 13:31:55 +04:00
private readonly IRepairRequestLogic _repairRequestLogic;
private readonly IItemForRepairLogic _itemForRepairLogic;
private readonly IWorkInRequestLogic _workInRequestLogic;
public HomeController(ILogger<HomeController> logger, IWorkLogic workLogic, IWorkerLogic workerLogic, IItemLogic itemLogic, IRepairRequestLogic repairRequestLogic, IItemForRepairLogic itemForRepairLogic, IWorkInRequestLogic workInRequestLogic)
{
_logger = logger;
2023-05-22 22:44:51 +04:00
_workLogic = workLogic;
2023-05-23 00:33:16 +04:00
_workerLogic = workerLogic;
_itemLogic = itemLogic;
2023-05-25 13:31:55 +04:00
_repairRequestLogic = repairRequestLogic;
_itemForRepairLogic = itemForRepairLogic;
_workInRequestLogic = workInRequestLogic;
}
2023-05-23 00:33:16 +04:00
/// <summary>
/// Главная страница
/// </summary>
/// <returns></returns>
public IActionResult Index()
{
2023-05-22 22:44:51 +04:00
if (CurrentUser.UserId < 1)
{
return Redirect("~/Home/Enter");
}
return View();
}
2023-05-23 00:33:16 +04:00
/// <summary>
/// Отображение формы для входа
/// </summary>
/// <returns></returns>
[HttpGet]
2023-05-22 22:44:51 +04:00
public IActionResult Enter()
2023-05-23 01:18:14 +04:00
=> View();
2023-05-23 00:33:16 +04:00
/// <summary>
/// Ввод данных в форму для входа
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public IActionResult Enter(WorkerSearchModel model)
{
var existingWorker = _workerLogic.ReadElement(new() { Login = model.Login, Password = model.Password });
if (existingWorker != null)
{
CurrentUser.UserId = existingWorker.Id;
return Redirect("~/Home/Index");
}
else
{
ViewBag.Exception = "Неверный логин или пароль";
return View();
}
}
/// <summary>
/// Список работ
/// </summary>
/// <returns></returns>
2023-05-22 22:44:51 +04:00
public IActionResult Works()
{
2023-05-23 00:33:16 +04:00
if (CurrentUser.UserId < 1)
{
return Redirect("~/Home/Index");
}
var Works = _workLogic.ReadList(new() { Id = CurrentUser.UserId });
ViewBag.Works = Works;
if (Works?.Count == 0)
{
ViewBag.Exception = "Пока нет работ";
}
return View();
}
/// <summary>
/// Выход из учётной записи
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Logout()
{
CurrentUser.UserId = 0;
return Redirect("~/Home/Index");
}
/// <summary>
/// Отображение формы для регистрации
/// </summary>
/// <returns></returns>
[HttpGet]
2023-05-23 01:18:14 +04:00
public IActionResult Register() => View();
2023-05-23 00:33:16 +04:00
/// <summary>
/// Ввод данных при регистрации пользователя
/// </summary>
/// <returns></returns>
[HttpPost]
public IActionResult Register(WorkerBindingModel model)
{
try
{
_workerLogic.Create(model);
}
catch (Exception ex)
{
ViewBag.Exception = ex.Message;
return View();
}
return Redirect("~/Home/Enter");
}
2023-05-23 01:18:14 +04:00
/// <summary>
/// Отображение формы CRUD работы
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
2023-05-23 00:33:16 +04:00
[HttpGet]
public IActionResult Work(int Id)
{
ViewBag.Work = _workLogic.ReadElement(new() { Id = Id });
return View();
}
2023-05-23 01:18:14 +04:00
/// <summary>
/// Обновление работы
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
2023-05-23 00:33:16 +04:00
[HttpPost]
2023-05-23 01:18:14 +04:00
public IActionResult UpdateWork(WorkBindingModel model)
2023-05-23 00:33:16 +04:00
{
2023-05-23 01:18:14 +04:00
_workLogic.Update(model);
return Redirect("~/Home/Works");
2023-05-22 22:44:51 +04:00
}
2023-05-23 01:18:14 +04:00
/// <summary>
/// Удаление работы
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
2023-05-23 00:33:16 +04:00
[HttpPost]
2023-05-23 01:18:14 +04:00
public IActionResult DeleteWork(WorkBindingModel model)
2023-05-23 00:33:16 +04:00
{
2023-05-23 01:18:14 +04:00
_workLogic.Delete(model);
return Redirect("~/Home/Works");
}
/// <summary>
/// Отображение формы добавления работы
/// </summary>
/// <returns></returns>
[HttpGet]
2023-05-23 01:18:14 +04:00
public IActionResult CreateWork() => View();
/// <summary>
/// Добавление работы
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public IActionResult CreateWork(WorkBindingModel model)
{
try
{
_workLogic.Create(model);
}
catch (Exception ex)
{
ViewBag.Exception = ex.Message;
return View();
}
return Redirect("~/Home/Works");
2023-05-23 00:33:16 +04:00
}
/// <summary>
/// Вывод формы со списком статей затрат
/// </summary>
/// <returns></returns>
public IActionResult Items()
{
if (CurrentUser.UserId < 1)
{
return Redirect("~/Home/Index");
}
var Items = _itemLogic.ReadList(new() { Id = CurrentUser.UserId });
ViewBag.Items = Items;
if (Items?.Count == 0)
{
ViewBag.Exception = "Пока нет запчастей";
}
return View();
}
/// <summary>
/// Отображение формы CRUD запчасти
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
[HttpGet]
public IActionResult Item(int Id)
{
ViewBag.Item = _itemLogic.ReadElement(new() { Id = Id });
return View();
}
/// <summary>
/// Обновление запчасти
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public IActionResult UpdateItem(ItemBindingModel model)
{
_itemLogic.Update(model);
return Redirect("~/Home/Items");
}
/// <summary>
/// Удаление запчасти
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public IActionResult DeleteItem(ItemBindingModel model)
{
_itemLogic.Delete(model);
return Redirect("~/Home/Items");
}
/// <summary>
/// Отображение формы добавления запчасти
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult CreateItem() => View();
/// <summary>
/// Добавление запчасти
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public IActionResult CreateItem(ItemBindingModel model)
{
try
{
_itemLogic.Create(model);
}
catch (Exception ex)
{
ViewBag.Exception = ex.Message;
return View();
}
return Redirect("~/Home/Items");
}
/// <summary>
/// Вывод списка заявок
/// </summary>
/// <returns></returns>
2023-05-25 13:31:55 +04:00
[HttpGet]
public IActionResult RepairRequests()
{
if (CurrentUser.UserId < 1)
{
return Redirect("~/Home/Index");
}
var RepairRequests = _repairRequestLogic.ReadList(null);
ViewBag.RepairRequests = RepairRequests;
if (RepairRequests?.Count == 0)
{
ViewBag.Exception = "Пока нет заявок";
}
return View();
}
[HttpGet]
public IActionResult RepairRequest(int Id)
{
ViewBag.Request = _repairRequestLogic.ReadElement(new() { Id = Id });
var itemsForRepair = _itemForRepairLogic.ReadList(new() { Id = Id });
ViewBag.ItemsForRepair = itemsForRepair;
if (itemsForRepair?.Count == 0)
{
ViewBag.Exception = "Пока нет затрат";
}
return View();
}
[HttpGet]
public IActionResult Attachment(int Id)
{
//добавляем информацию о заявке
ViewBag.Request = _repairRequestLogic.ReadElement(new() { Id = Id });
//добавляем статьи затрат
ViewBag.Items = _itemLogic.ReadList(new() { Id = CurrentUser.UserId });
return View();
}
[HttpPost]
public IActionResult Attachment(int id, int selecteditem, int count)
{
var existingAttachment = _itemForRepairLogic.ReadElement(new() { ItemId = selecteditem });
if (existingAttachment == null)
{
_itemForRepairLogic.Create(new() { ItemId = selecteditem, RepairRequestId = id, Count = count });
}
else
{
existingAttachment.Count = count;
_itemForRepairLogic.Update(new()
{
Id = existingAttachment.Id,
Count = existingAttachment.Count,
ItemId = existingAttachment.ItemId,
RepairRequestId = existingAttachment.RepairRequestId
});
}
return Redirect($"~/Home/RepairRequest/{id}");
}
[HttpGet]
public IActionResult WorksInRequest(int Id)
{
ViewBag.Request = _repairRequestLogic.ReadElement(new() { Id = Id });
var worksInRequest = _workInRequestLogic.ReadList(new() { Id = Id });
ViewBag.WorksInRequest = worksInRequest;
if (worksInRequest?.Count == 0)
{
ViewBag.Exception = "Пока нет работ";
}
return View();
}
/// <summary>
2023-05-23 00:33:16 +04:00
/// Вывод ошибок
/// </summary>
/// <returns></returns>
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}