405 lines
14 KiB
C#
Raw Normal View History

2024-03-30 22:21:51 +04:00
using DocumentFormat.OpenXml.Office2010.Excel;
using HostrelHeadwaiterApp;
2024-03-25 01:05:49 +04:00
using HotelContracts.BindingModels;
2024-03-30 22:21:51 +04:00
using HotelContracts.SearchModels;
2024-03-30 14:49:59 +04:00
using HotelContracts.ViewModels;
2024-03-30 22:21:51 +04:00
using HotelDataBaseImplement.Models;
2024-03-25 01:05:49 +04:00
using HotelHeadwaiterApp.Models;
using Microsoft.AspNetCore.Mvc;
2024-03-30 14:49:59 +04:00
using Microsoft.IdentityModel.Tokens;
2024-03-25 01:05:49 +04:00
using System.Diagnostics;
namespace HotelHeadwaiterApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
2024-03-30 14:49:59 +04:00
if (APIClient.Headwaiter == null)
{
return Redirect("~/Home/Enter");
}
2024-03-25 01:05:49 +04:00
return View();
}
2024-03-30 14:49:59 +04:00
[HttpGet]
public IActionResult Register()
{
return View();
}
[HttpPost]
public void Register(string login, string email, string password, string surname, string name, string patronymic, string telephone)
{
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(surname) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(patronymic))
{
throw new Exception("Введите логин, пароль, фамилию, имя и отчество");
}
APIClient.PostRequest("api/headwaiter/register", new HeadwaiterBindingModel
{
HeadwaiterSurname = surname,
HeadwaiterName = name,
HeadwaiterPatronymic = patronymic,
HeadwaiterLogin = login,
HeadwaiterPassword = password,
HeadwaiterEmail = email,
HeadwaiterPhoneNumber = telephone
});
Response.Redirect("Enter");
return;
}
[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.Headwaiter = APIClient.GetRequest<HeadwaiterViewModel>($"api/headwaiter/login?login={login}&password={password}");
if (APIClient.Headwaiter == null)
{
throw new Exception("Неверный логин/пароль");
}
Response.Redirect("Index");
}
2024-03-25 01:05:49 +04:00
[HttpGet]
public IActionResult Privacy()
{
if (APIClient.Headwaiter == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.Headwaiter);
}
[HttpPost]
public void Privacy(string login, string email, string password, string surname, string name, string patronymic, string number)
{
if (APIClient.Headwaiter == null)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(surname) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(patronymic))
{
throw new Exception("Введите логин, пароль, фамилию, имя и отчество");
}
APIClient.PostRequest("api/headwaiter/updatedata", new HeadwaiterBindingModel
{
Id = APIClient.Headwaiter.Id,
HeadwaiterSurname = surname,
HeadwaiterName = name,
HeadwaiterPatronymic = patronymic,
HeadwaiterLogin = login,
HeadwaiterPassword = password,
HeadwaiterEmail = email,
HeadwaiterPhoneNumber = number
});
APIClient.Headwaiter.HeadwaiterSurname = surname;
APIClient.Headwaiter.HeadwaiterName = name;
APIClient.Headwaiter.HeadwaiterPatronymic = patronymic;
APIClient.Headwaiter.HeadwaiterLogin = login;
APIClient.Headwaiter.HeadwaiterPassword = password;
APIClient.Headwaiter.HeadwaiterEmail = email;
APIClient.Headwaiter.HeadwaiterPhoneNumber = number;
Response.Redirect("Index");
}
2024-03-30 14:49:59 +04:00
public IActionResult ListLunches()
{
if (APIClient.Headwaiter == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<LunchViewModel>>($"api/lunch/getlunchlist?headwaiterId={APIClient.Headwaiter.Id}"));
}
public IActionResult CreateLunch()
2024-03-25 01:05:49 +04:00
{
2024-03-30 14:49:59 +04:00
if (APIClient.Headwaiter == null)
{
return Redirect("~/Home/Enter");
}
2024-03-25 01:05:49 +04:00
return View();
}
[HttpPost]
2024-03-30 14:49:59 +04:00
public void CreateLunch(string lunchName, double lunchPrice)
2024-03-25 01:05:49 +04:00
{
2024-03-30 14:49:59 +04:00
if (APIClient.Headwaiter == null)
2024-03-25 01:05:49 +04:00
{
2024-03-30 14:49:59 +04:00
throw new Exception("Необходима авторизация");
2024-03-25 01:05:49 +04:00
}
2024-03-30 14:49:59 +04:00
if (string.IsNullOrEmpty(lunchName))
2024-03-25 01:05:49 +04:00
{
2024-03-30 14:49:59 +04:00
throw new Exception("Введите имя");
}
if (lunchPrice < 0)
{
throw new Exception("Цена не может быть отрицательной");
}
2024-03-30 22:21:51 +04:00
if (string.IsNullOrEmpty(lunchPrice.ToString()))
{
throw new Exception("Введите цену");
}
2024-03-30 14:49:59 +04:00
APIClient.PostRequest("api/lunch/createlunch", new LunchBindingModel
{
LunchPrice = lunchPrice,
LunchName = lunchName,
HeadwaiterId = APIClient.Headwaiter.Id,
2024-03-25 01:05:49 +04:00
});
2024-03-30 14:49:59 +04:00
Response.Redirect("ListLunches");
}
2024-03-25 01:05:49 +04:00
2024-03-30 14:49:59 +04:00
public IActionResult UpdateLunch()
{
if (APIClient.Headwaiter == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Lunches = APIClient.GetRequest<List<LunchViewModel>>($"api/lunch/getlunchlist?headwaiterId={APIClient.Headwaiter.Id}");
return View();
}
[HttpPost]
public void UpdateLunch(int lunch, string lunchName, double lunchPrice)
{
if (APIClient.Headwaiter == null)
{
throw new Exception("Необходима авторизация");
}
if (string.IsNullOrEmpty(lunchName))
{
throw new Exception("Имя не может быть пустым");
}
if (lunchPrice < 0)
{
throw new Exception("Цена не может быть отрицательной");
}
2024-03-30 22:21:51 +04:00
if (string.IsNullOrEmpty(lunchPrice.ToString()))
{
throw new Exception("Введите цену");
}
2024-03-30 14:49:59 +04:00
APIClient.PostRequest("api/lunch/updatelunch", new LunchBindingModel
{
Id = lunch,
LunchName = lunchName,
LunchPrice = lunchPrice,
HeadwaiterId = APIClient.Headwaiter.Id,
});
Response.Redirect("ListLunches");
}
public IActionResult DeleteLunch()
{
if (APIClient.Headwaiter == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Lunches = APIClient.GetRequest<List<LunchViewModel>>($"api/lunch/getlunchlist?headwaiterId={APIClient.Headwaiter.Id}");
return View();
}
[HttpPost]
public void DeleteLunch(int lunch)
{
if (APIClient.Headwaiter == null)
{
throw new Exception("Необходима авторизация");
}
APIClient.PostRequest("api/lunch/deletelunch", new LunchBindingModel
{
Id = lunch
});
Response.Redirect("ListLunches");
2024-03-25 01:05:49 +04:00
}
2024-03-30 22:21:51 +04:00
public IActionResult ListRooms()
{
if (APIClient.Headwaiter == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<RoomViewModel>>($"api/room/getroomlist?headwaiterId={APIClient.Headwaiter.Id}"));
}
public IActionResult CreateRoom()
{
if (APIClient.Headwaiter == null)
{
return Redirect("~/Home/Enter");
}
return View();
}
[HttpPost]
public void CreateRoom(string roomName, double roomPrice, string roomFrame)
{
if (APIClient.Headwaiter == null)
{
throw new Exception("Необходима авторизация");
}
if (string.IsNullOrEmpty(roomName))
{
throw new Exception("Введите название");
}
if (string.IsNullOrEmpty(roomPrice.ToString()))
{
throw new Exception("Введите цену");
}
if (roomPrice < 0)
{
throw new Exception("Цена не может быть отрицательной");
}
APIClient.PostRequest("api/room/createroom", new RoomBindingModel
{
RoomName = roomName,
RoomPrice = roomPrice,
RoomFrame = roomFrame,
HeadwaiterId = APIClient.Headwaiter.Id,
});
Response.Redirect("ListRooms");
}
public IActionResult UpdateRoom()
{
if (APIClient.Headwaiter == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Rooms = APIClient.GetRequest<List<RoomViewModel>>($"api/room/getroomlist?headwaiterId={APIClient.Headwaiter.Id}");
return View();
}
[HttpPost]
public void UpdateRoom(int room, string roomName, double roomPrice, string roomFrame)
{
if (APIClient.Headwaiter == null)
{
throw new Exception("Необходима авторизация");
}
if (string.IsNullOrEmpty(roomName))
{
throw new Exception("Введите название");
}
if (string.IsNullOrEmpty(roomPrice.ToString()))
{
throw new Exception("Введите цену");
}
if (roomPrice < 0)
{
throw new Exception("Цена не может быть отрицательной");
}
APIClient.PostRequest("api/room/updateroom", new RoomBindingModel
{
Id = room,
RoomName = roomName,
RoomPrice = roomPrice,
RoomFrame = roomFrame,
HeadwaiterId = APIClient.Headwaiter.Id
});
Response.Redirect("ListRooms");
}
[HttpGet]
public Tuple<RoomViewModel, string>? GetRoom(int roomId)
{
if (APIClient.Headwaiter == null)
{
throw new Exception("Необходима авторизация");
}
var result = APIClient.GetRequest<Tuple<RoomViewModel, List<Tuple<string, string>>>>($"api/room/getroombyid?roomId={roomId}");
if (result == null)
{
return default;
}
string table = "";
for (int i = 0; i < result.Item2.Count; i++)
{
var lunchName = result.Item2[i].Item1;
var lunchPrice = result.Item2[i].Item2;
table += "<tr style=\"height: 44px\">";
table += $"<td class=\"u-border-1 u-border-grey-30 u-table-cell\">{lunchName}</td>";
table += $"<td class=\"u-border-1 u-border-grey-30 u-table-cell\">{lunchPrice}</td>";
table += "</tr>";
}
return Tuple.Create(result.Item1, table);
}
public IActionResult AddLunchToRoom()
{
if (APIClient.Headwaiter == null)
{
return Redirect("~/Home/Enter");
}
return View(Tuple.Create(APIClient.GetRequest<List<RoomViewModel>>($"api/room/getroomlist?headwaiterId={APIClient.Headwaiter.Id}"),
APIClient.GetRequest<List<LunchViewModel>>($"api/lunch/getlunchlist?headwaiterId={APIClient.Headwaiter.Id}")));
}
[HttpPost]
public void AddLunchToRoom(int room, int[] lunch)
{
if (APIClient.Headwaiter == null)
{
throw new Exception("Необходима авторизация");
}
for (int i = 0; i < lunch.Length; i++)
{
APIClient.PostRequest("api/room/AddLunchToRoom", Tuple.Create(
new RoomSearchModel() { Id = room },
new LunchViewModel() { Id = lunch[i] }
));
}
Response.Redirect("ListRooms");
}
public IActionResult DeleteRoom()
{
if (APIClient.Headwaiter == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Rooms = APIClient.GetRequest<List<RoomViewModel>>($"api/room/getroomlist?headwaiterId={APIClient.Headwaiter.Id}");
return View();
}
[HttpPost]
public void DeleteRoom(int room)
{
if (APIClient.Headwaiter == null)
{
throw new Exception("Необходима авторизация");
}
APIClient.PostRequest("api/room/deleteroom", new RoomBindingModel
{
Id = room
});
Response.Redirect("ListRooms");
}
2024-03-25 01:05:49 +04:00
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}