CourseWork_Hotel/Hotel/HotelOrganiserApp/Controllers/HomeController.cs

242 lines
7.9 KiB
C#

using HotelContracts.BindingModels;
using HotelContracts.ViewModels;
using HotelOrganiserApp.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using System.Text;
namespace HotelOrganiserApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult CreateMember()
{
if (APIClient.Organiser == null)
{
return Redirect("~/Home/Enter");
}
return View();
}
[HttpPost]
public void CreateMember(string fio, string citizenship)
{
if (APIClient.Organiser == null)
{
throw new Exception("Необходима авторизация");
}
if (string.IsNullOrEmpty(fio) || string.IsNullOrEmpty(citizenship))
{
throw new Exception("Введите фио");
}
if (string.IsNullOrEmpty(citizenship))
{
throw new Exception("Введите гражданство");
}
APIClient.PostRequest("api/main/createmember", new MemberBindingModel
{
MemberFIO = fio,
Citizenship = citizenship,
OrganiserId = APIClient.Organiser.Id,
});
Response.Redirect("ListMembers");
}
public IActionResult UpdateMember()
{
if (APIClient.Organiser == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Members = APIClient.GetRequest<List<MemberViewModel>>($"api/main/getmemberlist?organiserId={APIClient.Organiser.Id}");
return View();
}
[HttpPost]
public void UpdateMember(int member, string fio, string citizenship)
{
if (APIClient.Organiser == null)
{
throw new Exception("Необходима авторизация");
}
if (string.IsNullOrEmpty(fio))
{
throw new Exception("фио не может быть пустым");
}
if (string.IsNullOrEmpty(citizenship))
{
throw new Exception("Гражданство не может быть пустым");
}
APIClient.PostRequest("api/main/updatemember", new MemberBindingModel
{
Id = member,
MemberFIO = fio,
Citizenship = citizenship,
OrganiserId = APIClient.Organiser.Id,
});
Response.Redirect("ListMembers");
}
public IActionResult DeleteMember()
{
if (APIClient.Organiser == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Members = APIClient.GetRequest<List<MemberViewModel>>($"api/main/getmemberlist?organiserId={APIClient.Organiser.Id}");
return View();
}
[HttpPost]
public void DeleteMember(int member)
{
if (APIClient.Organiser == null)
{
throw new Exception("Необходима авторизация");
}
APIClient.PostRequest("api/main/deletemember", new MemberBindingModel
{
Id = member
});
Response.Redirect("ListMembers");
}
[HttpGet]
public MemberViewModel? GetMember(int memberId)
{
if (APIClient.Organiser == null)
{
throw new Exception("Необходима авторизация");
}
var result = APIClient.GetRequest<MemberViewModel>($"api/main/getmember?memberid={memberId}");
if (result == null)
{
return default;
}
var memberFIO = result.MemberFIO;
var citizenship = result.Citizenship;
return result;
}
public IActionResult Index()
{
if (APIClient.Organiser == null)
{
return Redirect("~/Home/Enter");
}
return View();
}
public IActionResult ListMembers()
{
if (APIClient.Organiser == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<MemberViewModel>>($"api/main/getmemberlist?organiserId={APIClient.Organiser.Id}"));
}
[HttpGet]
public IActionResult Privacy()
{
if (APIClient.Organiser == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.Organiser);
}
[HttpPost]
public void Privacy(string login, string email, string password, string fio, string telephone)
{
if (APIClient.Organiser == null)
{
throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
}
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio))
{
throw new Exception("Введите логин, пароль и ФИО");
}
APIClient.PostRequest("api/organiser/updatedata", new OrganiserBindingModel
{
Id = APIClient.Organiser.Id,
OrganiserFIO = fio,
OrganiserLogin = login,
OrganiserPassword = password,
OrganiserEmail= email,
OrganiserNumber= telephone
});
APIClient.Organiser.OrganiserFIO = fio;
APIClient.Organiser.OrganiserLogin = login;
APIClient.Organiser.OrganiserPassword = password;
APIClient.Organiser.OrganiserEmail = email;
APIClient.Organiser.OrganiserNumber = 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.Organiser = APIClient.GetRequest<OrganiserViewModel>($"api/organiser/login?login={login}&password={password}");
if (APIClient.Organiser == 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/organiser/register", new OrganiserBindingModel
{
OrganiserFIO = fio,
OrganiserLogin = login,
OrganiserPassword = password,
OrganiserEmail = email,
OrganiserNumber = telephone
});
Response.Redirect("Enter");
return;
}
}
}