CourseWork_Hotel/Hotel/HostrelHeadwaiterApp/Controllers/HomeController.cs

232 lines
7.6 KiB
C#
Raw Normal View History

using HostrelHeadwaiterApp.Models;
using HotelContracts.BindingModels;
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}"));
}
[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;
}
}
}