CourseWork_Bank/Bank/ClercApp/HomeController.cs
2023-05-21 17:01:50 +04:00

213 lines
7.7 KiB
C#

using CarCenterContracts.BindingModels;
using CarCenterContracts.BusinessLogicsContracts;
using CarCenterContracts.SearchModels;
using CarCenterDataModels.Models;
using EmployeeApp.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using System.Net.Http;
namespace EmployeeApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IPresaleLogic _presaleLogic;
private readonly ICarLogic _carLogic;
private readonly ISaleLogic _saleLogic;
private readonly IEmployeeLogic _employeeLogic;
public HomeController(ILogger<HomeController> logger, IPresaleLogic presaleLogic, ICarLogic carLogic, ISaleLogic saleLogic, IEmployeeLogic employeeLogic)
{
_logger = logger;
_presaleLogic = presaleLogic;
_carLogic = carLogic;
_saleLogic = saleLogic;
_employeeLogic = employeeLogic;
}
public IActionResult Index()
{
if (APIClient.Employee == null)
{
return Redirect("~/Home/Enter");
}
return View(_presaleLogic.ReadList(new PresaleSearchModel { EmployeeId = APIClient.Employee.Id }));
}
[HttpGet]
public IActionResult Privacy()
{
if (APIClient.Employee == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.Employee);
}
[HttpPost]
public void Privacy(string login, string password, string surname, string name, string patronymic)
{
if (APIClient.Employee == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(surname) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(patronymic))
{
throw new Exception("Введите логин, пароль и ФИО");
}
_employeeLogic.Update(new EmployeeBindingModel
{
Id = APIClient.Employee.Id,
Name = name,
Surname = surname,
Patronymic = patronymic,
Login = login,
Password = password
});
APIClient.Employee.Surname = surname;
APIClient.Employee.Name = name;
APIClient.Employee.Patronymic = patronymic;
APIClient.Employee.Login = login;
APIClient.Employee.Password = password;
Response.Redirect("Index");
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
[HttpGet]
public IActionResult Enter()
{
return View();
}
[HttpPost]
public void Enter(string login, string password)
{
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password))
{
throw new Exception("Введите логин и пароль");
}
APIClient.Employee = _employeeLogic.ReadElement(new EmployeeSearchModel { Login = login, Password = password });
if (APIClient.Employee == null)
{
throw new Exception("Неверный логин/пароль");
}
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Register()
{
return View();
}
[HttpPost]
public void Register(string login, string password, string surname, string name, string patronymic)
{
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(surname) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(patronymic))
{
throw new Exception("Введите логин, пароль и ФИО");
}
_employeeLogic.Create(new EmployeeBindingModel
{
Name = name,
Surname = surname,
Patronymic = patronymic,
Login = login,
Password = password
});
Response.Redirect("Enter");
return;
}
[HttpGet]
public IActionResult CreatePresale()
{
return View();
}
[HttpPost]
public void CreatePresale(int carid)
{
if (APIClient.Employee == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
_presaleLogic.Create(new PresaleBindingModel
{
EmployeeId = APIClient.Employee.Id,
});
Response.Redirect("Index");
}
public IActionResult Cars()
{
if (APIClient.Employee == null)
{
return Redirect("~/Home/Enter");
}
return View(_carLogic.ReadList(new CarSearchModel { EmployeeId = APIClient.Employee.Id }));
}
[HttpGet]
public IActionResult CreateCar()
{
ViewBag.Presales = _presaleLogic.ReadList(new PresaleSearchModel { EmployeeId = APIClient.Employee.Id });
return View();
}
[HttpPost]
public void CreateCar(List<int> presales)
{
if (APIClient.Employee == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
Dictionary<int, IPresaleModel> PresaleCars = new();
foreach (int id in presales)
{
var presale = _presaleLogic.ReadElement(new PresaleSearchModel { Id = id });
if (presale != null) PresaleCars.Add(presale.Id, presale);
}
_carLogic.Create(new CarBindingModel { EmployeeId = APIClient.Employee.Id, PresaleCars = PresaleCars, });
Response.Redirect("Cars");
}
[HttpGet]
public IActionResult Car(int id)
{
return View(_carLogic.ReadElement(new CarSearchModel { Id = id }));
}
[HttpGet]
public IActionResult Sales()
{
if (APIClient.Employee == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
return View(_saleLogic.ReadList(new SaleSearchModel { EmployeeId = APIClient.Employee.Id }));
}
[HttpGet]
public IActionResult CreateSale()
{
if (APIClient.Employee == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
ViewBag.Cars = _carLogic.ReadList(new CarSearchModel { EmployeeId = APIClient.Employee.Id });
return View();
}
[HttpPost]
public void CreateSale(string sum, int carid)
{
if (APIClient.Employee == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
_saleLogic.Create(new SaleBindingModel { EmployeeId = APIClient.Employee.Id, Sum = (float)Convert.ToDouble(sum), CarId = carid });
Response.Redirect("Transfers");
}
}
}