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

260 lines
6.4 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;
using CarServiceDatabase.Models;
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;
public HomeController(ILogger<HomeController> logger, IWorkLogic workLogic, IWorkerLogic workerLogic, IItemLogic itemLogic)
{
_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-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>
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 });
}
}
}