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

154 lines
3.6 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;
2023-05-23 00:33:16 +04:00
public HomeController(ILogger<HomeController> logger, IWorkLogic workLogic, IWorkerLogic workerLogic)
{
_logger = logger;
2023-05-22 22:44:51 +04:00
_workLogic = workLogic;
2023-05-23 00:33:16 +04:00
_workerLogic = workerLogic;
}
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()
{
return 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]
public IActionResult Register()
{
return 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");
}
[HttpGet]
public IActionResult Work(int Id)
{
ViewBag.Work = _workLogic.ReadElement(new() { Id = Id });
return View();
}
[HttpPost]
public IActionResult UpdateWork(Work work)
{
//ViewBag.Work = _workLogic.ReadElement(new() { Id = Id });
2023-05-22 22:44:51 +04:00
return View();
}
2023-05-23 00:33:16 +04:00
[HttpPost]
public IActionResult DeleteWork(int Id)
{
ViewBag.Work = _workLogic.ReadElement(new() { Id = Id });
return View();
}
2023-05-22 22:44:51 +04:00
2023-05-23 00:33:16 +04:00
/// <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 });
}
}
}