SE-23-Lisov-N.A-SoDM/RentalBusiness/Controllers/HomeController.cs

94 lines
2.9 KiB
C#

using Microsoft.AspNetCore.Mvc;
using RentalBusiness.AuxilaryElements.BusinessLogic;
using RentalBusiness.Models;
using RentalBusiness.Models.ViewModels;
using System.Diagnostics;
namespace RentalBusiness.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly StorageLogic _storageLogic;
private readonly ClientLogic _clientLogic;
private readonly ContractLogic _contractLogic;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
_storageLogic = new StorageLogic();
_clientLogic = new ClientLogic();
_contractLogic = new ContractLogic();
}
[HttpGet]
public IActionResult Index()
{
UpdateData();
ViewBag.Cars = _storageLogic._storageFree;
ViewBag.Clients = _clientLogic._clients;
return View();
}
[HttpPost]
public void Index(int car,int customer,string returnDate)
{
ContractViewModel contract = new()
{
CarID = car,
CarInfo = _storageLogic._storageFull.ElementAt(car).CarInfo,
CustomerID = customer,
CustomerFullName = _clientLogic._clients.ElementAt(customer).FullName,
ReturnDate = DateTime.Parse(returnDate),
Price = Calc(car, returnDate)
};
_contractLogic.PostContract(contract);
}
[HttpGet]
public IActionResult Customer()
{
ViewBag.Clients = _clientLogic._clients;
return View();
}
[HttpGet]
public IActionResult Car()
{
ViewBag.Cars = _storageLogic._storageFree;
return View();
}
[HttpGet]
public IActionResult Storage()
{
return View(_storageLogic._storageFull);
}
[HttpGet]
public IActionResult Contracts()
{
return View(_contractLogic._contracts);
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
[HttpPost]
public decimal Calc(int id, string date)
{
return _storageLogic._storageFull.ElementAt(id).CarRatio * (Convert.ToInt32((DateTime.Parse(date) - DateTime.Now).TotalHours) * 5);
}
public void UpdateData()
{
_contractLogic._contracts = _contractLogic.ReadContracts().Result;
_storageLogic._storageFree = _storageLogic.ReadStorage(true).Result;
_storageLogic._storageFull = _storageLogic.ReadStorage(false).Result;
_clientLogic._clients = _clientLogic.ReadClients().Result;
}
}
}