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

340 lines
9.1 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 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;
private readonly IRepairRequestLogic _repairRequestLogic;
private readonly IItemForRepairLogic _itemForRepairLogic;
private readonly IWorkInRequestLogic _workInRequestLogic;
private readonly ICustomerRoleImitationLogic _customer;
public HomeController(ILogger<HomeController> logger, IWorkLogic workLogic, IWorkerLogic workerLogic, IItemLogic itemLogic, IRepairRequestLogic repairRequestLogic, IItemForRepairLogic itemForRepairLogic, IWorkInRequestLogic workInRequestLogic, ICustomerRoleImitationLogic customer)
{
_logger = logger;
_workLogic = workLogic;
_workerLogic = workerLogic;
_itemLogic = itemLogic;
_repairRequestLogic = repairRequestLogic;
_itemForRepairLogic = itemForRepairLogic;
_workInRequestLogic = workInRequestLogic;
_customer = customer;
}
/// <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>
[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)
{
_itemForRepairLogic.Create(new() { ItemId = selecteditem, RepairRequestId = id, Count = count });
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();
}
public IActionResult GenerateRequestData()
{
_customer.GenerateRequestData();
return Redirect("~/Home/Index");
}
public IActionResult GenerateWorksInRequest()
{
_customer.GenerateWorksInRequest();
return Redirect("~/Home/Index");
}
public IActionResult GeneratePayments()
{
_customer.GeneratePayments();
return Redirect("~/Home/Index");
}
/// <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 });
}
}
}