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 _logger; private readonly IWorkLogic _workLogic; private readonly IWorkerLogic _workerLogic; public HomeController(ILogger logger, IWorkLogic workLogic, IWorkerLogic workerLogic) { _logger = logger; _workLogic = workLogic; _workerLogic = workerLogic; } /// /// Главная страница /// /// 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"); } /// /// Вывод ошибок /// /// [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } } }