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

260 lines
6.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using CarServiceContracts.BindingModels;
using CarServiceContracts.BusinessLogicsContracts;
using CarServiceContracts.SearchModels;
using CarServiceDatabase.Models;
using CarServiceWebApp.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
namespace CarServiceWebApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IWorkLogic _workLogic;
private readonly IWorkerLogic _workerLogic;
private readonly IItemLogic _itemLogic;
public HomeController(ILogger<HomeController> logger, IWorkLogic workLogic, IWorkerLogic workerLogic, IItemLogic itemLogic)
{
_logger = logger;
_workLogic = workLogic;
_workerLogic = workerLogic;
_itemLogic = itemLogic;
}
/// <summary>
/// Главная страница
/// </summary>
/// <returns></returns>
public IActionResult Index()
{
if (CurrentUser.UserId < 1)
{
return Redirect("~/Home/Enter");
}
return View();
}
/// <summary>
/// Отображение формы для входа
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Enter()
=> View();
/// <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>
public IActionResult Works()
{
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]
public IActionResult Register() => View();
/// <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");
}
/// <summary>
/// Отображение формы CRUD работы
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
[HttpGet]
public IActionResult Work(int Id)
{
ViewBag.Work = _workLogic.ReadElement(new() { Id = Id });
return View();
}
/// <summary>
/// Обновление работы
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public IActionResult UpdateWork(WorkBindingModel model)
{
_workLogic.Update(model);
return Redirect("~/Home/Works");
}
/// <summary>
/// Удаление работы
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public IActionResult DeleteWork(WorkBindingModel model)
{
_workLogic.Delete(model);
return Redirect("~/Home/Works");
}
/// <summary>
/// Отображение формы добавления работы
/// </summary>
/// <returns></returns>
[HttpGet]
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");
}
/// <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>
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}