CourseWork_Hotel/Hotel/HostrelHeadwaiterApp/Controllers/HomeController.cs

386 lines
13 KiB
C#
Raw Normal View History

using HostrelHeadwaiterApp.Models;
using HotelContracts.BindingModels;
using HotelContracts.SearchModels;
using HotelContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
namespace HostrelHeadwaiterApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult CreateDinner()
{
if (APIClient.Headwaiter == null)
{
return Redirect("~/Home/Enter");
}
return View();
}
[HttpPost]
public void CreateDinner(double dinnerPrice, string dinnerName)
{
if (APIClient.Headwaiter == null)
{
throw new Exception("Необходима авторизация");
}
if (string.IsNullOrEmpty(dinnerName))
{
throw new Exception("Введите имя");
}
APIClient.PostRequest("api/main/createdinner", new DinnerBindingModel
{
DinnerPrice = dinnerPrice,
DinnerName = dinnerName,
HeadwaiterId = APIClient.Headwaiter.Id,
});
Response.Redirect("ListDinners");
}
public IActionResult UpdateDinner()
{
if (APIClient.Headwaiter == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Dinners = APIClient.GetRequest<List<DinnerViewModel>>($"api/main/getdinnerlist?headwaiterId={APIClient.Headwaiter.Id}");
return View();
}
[HttpPost]
public void UpdateDinner(int dinner, string dinnerName, double dinnerPrice)
{
if (APIClient.Headwaiter == null)
{
throw new Exception("Необходима авторизация");
}
if (string.IsNullOrEmpty(dinnerName))
{
throw new Exception("имя не может быть пустым");
}
APIClient.PostRequest("api/main/updatedinner", new DinnerBindingModel
{
Id = dinner,
DinnerName = dinnerName,
DinnerPrice = dinnerPrice,
HeadwaiterId = APIClient.Headwaiter.Id,
});
Response.Redirect("ListDinners");
}
public IActionResult DeleteDinner()
{
if (APIClient.Headwaiter == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Dinners = APIClient.GetRequest<List<DinnerViewModel>>($"api/main/getdinnerlist?headwaiterId={APIClient.Headwaiter.Id}");
return View();
}
[HttpPost]
public void DeleteDinner(int dinner)
{
if (APIClient.Headwaiter == null)
{
throw new Exception("Необходима авторизация");
}
APIClient.PostRequest("api/main/deletedinner", new DinnerBindingModel
{
Id = dinner
});
Response.Redirect("ListDinners");
}
[HttpGet]
public DinnerViewModel? GetDinner(int dinnerId)
{
if (APIClient.Headwaiter == null)
{
throw new Exception("Необходима авторизация");
}
var result = APIClient.GetRequest<DinnerViewModel>($"api/main/getdinner?dinnerid={dinnerId}");
if (result == null)
{
return default;
}
var dinnerPrice = result.DinnerPrice;
var dinnerName = result.DinnerName;
return result;
}
public IActionResult Index()
{
if (APIClient.Headwaiter == null)
{
return Redirect("~/Home/Enter");
}
return View();
}
public IActionResult ListDinners()
{
if (APIClient.Headwaiter == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<DinnerViewModel>>($"api/main/getdinnerlist?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("Введите цену");
}
APIClient.PostRequest("api/main/createroom", new RoomBindingModel
{
RoomName = roomName,
RoomPrice = roomPrice,
RoomFrame = roomFrame,
HeadwaiterId = APIClient.Headwaiter.Id,
});
Response.Redirect("ListRooms");
}
public IActionResult DeleteRoom()
{
if (APIClient.Headwaiter == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Rooms = APIClient.GetRequest<List<RoomViewModel>>($"api/main/getroomlist?headwaiterId={APIClient.Headwaiter.Id}");
return View();
}
[HttpPost]
public void DeleteRoom(int room)
{
if (APIClient.Headwaiter == null)
{
throw new Exception("Необходима авторизация");
}
APIClient.PostRequest("api/main/deleteroom", new RoomBindingModel
{
Id = room
});
Response.Redirect("ListRooms");
}
public IActionResult UpdateRoom()
{
if (APIClient.Headwaiter == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Rooms = APIClient.GetRequest<List<RoomViewModel>>($"api/main/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("Введите цену");
}
APIClient.PostRequest("api/main/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/main/getroom?roomId={roomId}");
if (result == null)
{
return default;
}
string table = "";
for (int i = 0; i < result.Item2.Count; i++)
{
var dinnerName = result.Item2[i].Item1;
var dinnerPrice = result.Item2[i].Item2;
table += "<tr style=\"height: 44px\">";
table += $"<td class=\"u-border-1 u-border-grey-30 u-table-cell\">{dinnerName}</td>";
table += $"<td class=\"u-border-1 u-border-grey-30 u-table-cell\">{dinnerPrice}</td>";
table += "</tr>";
}
return Tuple.Create(result.Item1, table);
}
public IActionResult AddDinnerToRoom()
{
if (APIClient.Headwaiter == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Rooms = APIClient.GetRequest<List<RoomViewModel>>($"api/main/getroomlist?headwaiterId={APIClient.Headwaiter.Id}");
ViewBag.Dinners = APIClient.GetRequest<List<DinnerViewModel>>($"api/main/getdinnerlist?headwaiterId={APIClient.Headwaiter.Id}");
return View();
}
[HttpPost]
public void AddDinnerToRoom(int room, int dinner)
{
if (APIClient.Headwaiter == null)
{
throw new Exception("Необходима авторизация");
}
APIClient.PostRequest("api/main/AddDinnerToRoom", Tuple.Create(
new RoomSearchModel() { Id = room },
new DinnerViewModel() { Id = dinner }
));
Response.Redirect("ListRooms");
}
public IActionResult ListRooms()
{
if (APIClient.Headwaiter == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<RoomViewModel>>($"api/main/getroomlist?headwaiterId={APIClient.Headwaiter.Id}"));
}
[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 fio, string telephone)
{
if (APIClient.Headwaiter == null)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio))
{
throw new Exception("Введите логин, пароль и ФИО");
}
APIClient.PostRequest("api/headwaiter/updatedata", new HeadwaiterBindingModel
{
Id = APIClient.Headwaiter.Id,
HeadwaiterFIO = fio,
HeadwaiterLogin = login,
HeadwaiterPassword = password,
HeadwaiterEmail = email,
HeadwaiterNumber = telephone
});
APIClient.Headwaiter.HeadwaiterFIO = fio;
APIClient.Headwaiter.HeadwaiterLogin = login;
APIClient.Headwaiter.HeadwaiterPassword = password;
APIClient.Headwaiter.HeadwaiterEmail = email;
APIClient.Headwaiter.HeadwaiterNumber = telephone;
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.Headwaiter = APIClient.GetRequest<HeadwaiterViewModel>($"api/headwaiter/login?login={login}&password={password}");
if (APIClient.Headwaiter == null)
{
throw new Exception("Неверный логин/пароль");
}
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Register()
{
return View();
}
[HttpPost]
public void Register(string login, string email, string password, string fio, string telephone)
{
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio))
{
throw new Exception("Введите логин, пароль и ФИО");
}
APIClient.PostRequest("api/headwaiter/register", new HeadwaiterBindingModel
{
HeadwaiterFIO = fio,
HeadwaiterLogin = login,
HeadwaiterPassword = password,
HeadwaiterEmail = email,
HeadwaiterNumber = telephone
});
Response.Redirect("Enter");
return;
}
}
}