PIbd-22_Bazunov_AI_Hotel/Hotel/HotelView/Controllers/HomeController.cs
2023-05-19 20:55:43 +04:00

498 lines
14 KiB
C#

using HotelView.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using HotelContracts.BindingModels;
using HotelContracts.SearchModels;
using HotelDataModels.Models;
using HotelView.Utils;
namespace HotelView.Controllers;
public class HomeController : Controller
{
private readonly Api _api;
public HomeController(Api api)
{
_api = api;
}
public IActionResult Index()
{
if (_api.Maitre is null)
{
return Redirect("~/Home/Login");
}
var rooms = _api.GetRoomLogic.ReadList(null);
ViewBag.Maitre = _api.Maitre;
ViewBag.RoomIsReserved = Math.Round((double)rooms.Count(x => x.IsReserved) / rooms.Count * 100);
ViewBag.RoomIsReservedCount = rooms.Count(x => x.IsReserved);
ViewBag.RoomAll = rooms.Count;
var reservations = _api.GetReservationLogic.ReadList(null);
var guests = _api.GetGuestLogic.ReadList(null);
var cleaning = _api.GetCleaningLogic.ReadList(null).Last();
ViewBag.GuestCount = guests.Count;
ViewBag.GuestLivingCount = reservations.GroupBy(x => x.GuestId).Count();
ViewBag.GuestLiving = Math.Round((double)ViewBag.GuestLivingCount / guests.Count * 100);
ViewBag.MaxDay = reservations.Max(x => x.EndDate - x.StartDate).Days;
ViewBag.AverageDay = Math.Round(reservations.Select(x => x.EndDate - x.StartDate)
.Select(x => x.Days).Average());
ViewBag.MaxCost = reservations.Max(x => x.GetCost());
ViewBag.AverageCost = Math.Round(reservations.Select(x => x.GetCost()).Average());
ViewBag.LastReservation = reservations.Last();
ViewBag.LastReservationRooms = string.Join(",", reservations.Last().ReservationsRooms
.Select(x => x.Key));
ViewBag.LastCleaning = cleaning;
return View();
}
public IActionResult Privacy()
{
if (_api.Maitre is null)
{
return Redirect("~/Home/Login");
}
ViewBag.Maitre = _api.Maitre;
return View();
}
[HttpGet]
public IActionResult Login()
{
return View();
}
[HttpPost]
public void Login(string login, string password)
{
if (string.IsNullOrEmpty(login) ||
string.IsNullOrEmpty(password))
{
throw new Exception("Enter login and password");
}
_api.Auth(login, password);
if (_api.Maitre == null)
{
throw new Exception("Invalid login or password");
}
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Register()
{
return View();
}
[HttpPost]
public void Register(string login, string password, string passwordConfirm, string fio)
{
if (string.IsNullOrEmpty(login) ||
string.IsNullOrEmpty(password) ||
string.IsNullOrEmpty(fio) ||
string.IsNullOrEmpty(passwordConfirm))
{
throw new Exception("Enter login and password");
}
if (!password.Equals(passwordConfirm))
{
throw new Exception("Password must be equals");
}
var fioSplit = fio.Split(" ");
_api.Register(login, password, fioSplit[0], fioSplit[1], fioSplit[2]);
Response.Redirect("Login");
}
public IActionResult Rooms()
{
ViewBag.Maitre = _api.Maitre;
return View(_api.GetRoomLogic.ReadList(null));
}
[HttpGet]
public IActionResult EditRoom(int id)
{
ViewBag.Maitre = _api.Maitre;
var item = _api.GetRoomLogic.ReadElement(new RoomSearchModel
{
Id = id
});
return View(item);
}
[HttpPost]
public void EditRoom(int id, string type, double cost)
{
if (string.IsNullOrEmpty(type) || cost == 0)
{
throw new Exception("Invalid arguments");
}
_api.GetRoomLogic.Update(new RoomBindingModel
{
Id = id,
Cost = cost,
Type = type
});
Response.Redirect("Rooms");
}
[HttpPost]
public void CreateRoom(string type, double cost)
{
if (string.IsNullOrEmpty(type) || cost == 0)
{
throw new Exception("Invalid arguments");
}
_api.GetRoomLogic.Create(new RoomBindingModel
{
Cost = cost,
Type = type
});
Response.Redirect("Rooms");
}
public IActionResult Guests()
{
ViewBag.Maitre = _api.Maitre;
return View(_api.GetGuestLogic.ReadList(null));
}
[HttpPost]
public void CreateGuest(string name, string secondName, string lastName)
{
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(secondName) || string.IsNullOrEmpty(lastName))
{
throw new Exception("Invalid arguments");
}
_api.GetGuestLogic.Create(new GuestBindingModel
{
Name = name.FirstCharToUpper(),
SecondName = secondName.FirstCharToUpper(),
LastName = lastName.FirstCharToUpper()
});
Response.Redirect("Guests");
}
[HttpGet]
public IActionResult EditGuest(int id)
{
ViewBag.Maitre = _api.Maitre;
var guest = _api.GetGuestLogic.ReadElement(new GuestSearchModel
{
Id = id
});
return View(guest);
}
[HttpPost]
public void EditGuest(int id, string name, string secondName, string lastName)
{
if (string.IsNullOrEmpty(name) ||
string.IsNullOrEmpty(secondName) ||
string.IsNullOrEmpty(lastName) ||
id == 0)
{
throw new Exception("Invalid arguments");
}
_api.GetGuestLogic.Update(new GuestBindingModel
{
Id = id,
Name = name.FirstCharToUpper(),
SecondName = secondName.FirstCharToUpper(),
LastName = lastName.FirstCharToUpper()
});
Response.Redirect("Guests");
}
[SuppressMessage("ReSharper.DPA", "DPA0000: DPA issues")]
public IActionResult Reservations()
{
ViewBag.Maitre = _api.Maitre;
ViewBag.Guests = _api.GetGuestLogic.ReadList(null);
ViewBag.Rooms = _api.GetRoomLogic.ReadList(new RoomSearchModel
{
IsReserved = false
});
var list = _api.GetReservationLogic.ReadList(null);
return View(list);
}
[HttpPost]
public void CreateReservation(int clientId, DateTime dateStart, DateTime dateEnd, List<int> rooms, string cleaning)
{
if (dateEnd < dateStart || dateStart < DateTime.Now)
{
throw new Exception("Invalid arguments");
}
_api.GetReservationLogic.Create(new ReservationBindingModel
{
MaitreId = _api.Maitre.Id,
GuestId = clientId,
StartDate = dateStart,
EndDate = dateEnd,
ReservationsRooms = _api.GetRoomLogic.ReadList(null)
.Where(x => rooms.Contains(x.Id))
.ToDictionary(x => x.Id, y => y as IRoomModel)
});
if (string.Equals(cleaning, "on", StringComparison.Ordinal))
{
var clInst = _api.GetCleaningInstrumentsLogic.ReadList(null);
foreach(var id in rooms)
{
_api.GetCleaningLogic.Create(new CleaningBindingModel
{
Date = dateEnd,
RoomId = id,
CleaningInstruments = clInst
.Where(x => x.Id <= 4)
.ToDictionary(x => x.Id,
x => x as ICleaningInstrumentsModel)
});
}
}
Response.Redirect("Reservations");
}
[HttpGet]
public IActionResult EditReservation(int id)
{
ViewBag.Maitre = _api.Maitre;
var item = _api.GetReservationLogic.ReadElement(new ReservationSearchModel()
{
Id = id
});
ViewBag.Guests = _api.GetGuestLogic.ReadList(null);
ViewBag.RoomsModel = item.ReservationsRooms.Select(x => x.Key).ToList();
ViewBag.Rooms = _api.GetRoomLogic.ReadList(new RoomSearchModel
{
IsReserved = false
});
ViewBag.StartDate = item.StartDate.ToString("MM/dd/yyyy").Replace(".", "/");
ViewBag.EndDate = item.EndDate.ToString("MM/dd/yyyy").Replace(".", "/");
return View(item);
}
[HttpPost]
public void EditReservation(int id,
DateTime dateStart,
DateTime dateEnd)
{
if (dateEnd < dateStart || dateStart < DateTime.Now)
{
throw new Exception("Invalid arguments");
}
_api.GetReservationLogic.Update(new ReservationBindingModel
{
Id = id,
MaitreId = _api.Maitre.Id,
StartDate = dateStart,
EndDate = dateEnd
});
Response.Redirect("Reservations");
}
public IActionResult CleaningInstruments()
{
ViewBag.Maitre = _api.Maitre;
return View(_api.GetCleaningInstrumentsLogic.ReadList(null));
}
[HttpGet]
public IActionResult EditCleaningInstrument(int id)
{
ViewBag.Maitre = _api.Maitre;
var item = _api.GetCleaningInstrumentsLogic.ReadElement(new()
{
Id = id
});
return View(item);
}
[HttpPost]
public void EditCleaningInstrument(int id, string type)
{
if (string.IsNullOrEmpty(type))
throw new ArgumentException("Type must be not null");
_api.GetCleaningInstrumentsLogic.Update(new CleaningInstrumentsBindingModel
{
Id = id,
Type = type
});
Response.Redirect("CleaningInstruments");
}
[HttpPost]
public void CreateCleaningInstrument(string type)
{
if (string.IsNullOrEmpty(type))
throw new ArgumentException("Type must be not null");
_api.GetCleaningInstrumentsLogic.Create(new CleaningInstrumentsBindingModel
{
Type = type
});
Response.Redirect("CleaningInstruments");
}
public IActionResult Cleaning()
{
ViewBag.Maitre = _api.Maitre;
ViewBag.CleaningInstruments = _api.GetCleaningInstrumentsLogic.ReadList(null);
ViewBag.Rooms = _api.GetRoomLogic.ReadList(null);
return View(_api.GetCleaningLogic.ReadList(null));
}
[HttpPost]
public void CreateCleaning(int roomId, DateTime dateTime, List<int> cleanings)
{
if (roomId == 0)
{
throw new ArgumentException("Room id must be more then zero");
}
_api.GetCleaningLogic.Create(new CleaningBindingModel
{
RoomId = roomId,
Date = dateTime,
CleaningInstruments = _api.GetCleaningInstrumentsLogic.ReadList(null)
.Where(x => cleanings.Contains(x.Id))
.ToDictionary(x => x.Id,
x => x as ICleaningInstrumentsModel)
});
Response.Redirect("Cleaning");
}
[HttpGet]
public IActionResult EditCleaning(int id)
{
ViewBag.Maitre = _api.Maitre;
return View();
}
[HttpPost]
public void EditCleaning(int id, DateTime dateTime)
{
_api.GetCleaningLogic.Update(new CleaningBindingModel
{
Id = id,
Date = dateTime
});
Response.Redirect("Cleaning");
}
[HttpGet]
public void DeleteReservation(int id)
{
if (id == 0) return;
_api.GetReservationLogic.Delete(new ReservationBindingModel
{
Id = id
});
Response.Redirect("Reservations");
}
[HttpGet]
public void DeleteRoom(int id)
{
if (id == 0) return;
_api.GetRoomLogic.Delete(new()
{
Id = id
});
Response.Redirect("Rooms");
}
[HttpGet]
public void DeleteGuest(int id)
{
if (id == 0) return;
_api.GetGuestLogic.Delete(new()
{
Id = id
});
Response.Redirect("Guests");
}
[HttpGet]
public void DeleteClean(int id)
{
if (id == 0) return;
_api.GetCleaningLogic.Delete(new()
{
Id = id
});
Response.Redirect("Cleanings");
}
[HttpGet]
public void DeleteInstrument(int id)
{
if (id == 0) return;
_api.GetCleaningInstrumentsLogic.Delete(new()
{
Id = id
});
Response.Redirect("CleaningInstruments");
}
public IActionResult Report()
{
ViewBag.Maitre = _api.Maitre;
ViewBag.Reservations = _api.GetReservationLogic.ReadList(null);
return View();
}
[HttpPost]
public IActionResult CreateReport(List<int> reservations, string options)
{
switch (options)
{
case "word":
{
var file = _api.SendReportWord(reservations);
return new PhysicalFileResult(file,
"application/vnd.openxmlformats-officedocument.wordprocessingml.document");
}
case "excel":
{
var file = _api.SendReportExcel(reservations);
return new PhysicalFileResult(file,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
default:
return null;
}
}
[HttpPost]
public IActionResult Report(DateTime from, DateTime to)
{
_api.SendReportPdf(from, to);
ViewBag.Maitre = _api.Maitre;
ViewBag.Reservations = _api.GetReservationLogic.ReadList(null);
ViewBag.PreRender = _api.GetPreRenderReport(from, to);
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}