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 _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 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; } /// /// Главная страница /// /// public IActionResult Index() { if (CurrentUser.UserId < 1) { return Redirect("~/Home/Enter"); } return View(); } /// /// Отображение формы для входа /// /// [HttpGet] public IActionResult Enter() => View(); /// /// Ввод данных в форму для входа /// /// /// [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(); } } /// /// Список работ /// /// 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(); } /// /// Выход из учётной записи /// /// [HttpGet] public IActionResult Logout() { CurrentUser.UserId = 0; return Redirect("~/Home/Index"); } /// /// Отображение формы для регистрации /// /// [HttpGet] public IActionResult Register() => View(); /// /// Ввод данных при регистрации пользователя /// /// [HttpPost] public IActionResult Register(WorkerBindingModel model) { try { _workerLogic.Create(model); } catch (Exception ex) { ViewBag.Exception = ex.Message; return View(); } return Redirect("~/Home/Enter"); } /// /// Отображение формы CRUD работы /// /// /// [HttpGet] public IActionResult Work(int Id) { ViewBag.Work = _workLogic.ReadElement(new() { Id = Id }); return View(); } /// /// Обновление работы /// /// /// [HttpPost] public IActionResult UpdateWork(WorkBindingModel model) { _workLogic.Update(model); return Redirect("~/Home/Works"); } /// /// Удаление работы /// /// /// [HttpPost] public IActionResult DeleteWork(WorkBindingModel model) { _workLogic.Delete(model); return Redirect("~/Home/Works"); } /// /// Отображение формы добавления работы /// /// [HttpGet] public IActionResult CreateWork() => View(); /// /// Добавление работы /// /// /// [HttpPost] public IActionResult CreateWork(WorkBindingModel model) { try { _workLogic.Create(model); } catch (Exception ex) { ViewBag.Exception = ex.Message; return View(); } return Redirect("~/Home/Works"); } /// /// Вывод формы со списком статей затрат /// /// 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(); } /// /// Отображение формы CRUD запчасти /// /// /// [HttpGet] public IActionResult Item(int Id) { ViewBag.Item = _itemLogic.ReadElement(new() { Id = Id }); return View(); } /// /// Обновление запчасти /// /// /// [HttpPost] public IActionResult UpdateItem(ItemBindingModel model) { _itemLogic.Update(model); return Redirect("~/Home/Items"); } /// /// Удаление запчасти /// /// /// [HttpPost] public IActionResult DeleteItem(ItemBindingModel model) { _itemLogic.Delete(model); return Redirect("~/Home/Items"); } /// /// Отображение формы добавления запчасти /// /// [HttpGet] public IActionResult CreateItem() => View(); /// /// Добавление запчасти /// /// /// [HttpPost] public IActionResult CreateItem(ItemBindingModel model) { try { _itemLogic.Create(model); } catch (Exception ex) { ViewBag.Exception = ex.Message; return View(); } return Redirect("~/Home/Items"); } /// /// Вывод списка заявок /// /// [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(); } 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"); } /// /// Вывод ошибок /// /// [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } } }