CourseWork_Hotel/Hotel/HotelOrganiserApp/Controllers/HomeController.cs

242 lines
7.9 KiB
C#
Raw Normal View History

using HotelContracts.BindingModels;
using HotelContracts.ViewModels;
using HotelOrganiserApp.Models;
2023-04-07 13:49:59 +04:00
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using System.Text;
2023-04-07 13:49:59 +04:00
namespace HotelOrganiserApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
2023-05-14 21:46:57 +04:00
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;
}
2023-04-07 13:49:59 +04:00
public IActionResult Index()
{
if (APIClient.Organiser == null)
{
return Redirect("~/Home/Enter");
}
2023-04-07 13:49:59 +04:00
return View();
}
2023-05-14 21:46:57 +04:00
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()
2023-04-07 13:49:59 +04:00
{
if (APIClient.Organiser == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.Organiser);
2023-04-07 13:49:59 +04:00
}
[HttpPost]
public void Privacy(string login, string email, string password, string fio, string telephone)
2023-04-07 13:49:59 +04:00
{
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");
2023-04-07 13:49:59 +04:00
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
2023-04-07 13:49:59 +04:00
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
2023-04-07 13:49:59 +04:00
}
[HttpGet]
public IActionResult Enter()
2023-04-07 13:49:59 +04:00
{
return View();
}
[HttpPost]
public void Enter(string login, string password)
2023-04-07 13:49:59 +04:00
{
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");
2023-04-07 13:49:59 +04:00
}
[HttpGet]
public IActionResult Register()
2023-04-07 13:49:59 +04:00
{
return View();
}
[HttpPost]
public void Register(string login, string email, string password, string fio, string telephone)
2023-04-07 13:49:59 +04:00
{
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
});
2023-04-07 13:49:59 +04:00
Response.Redirect("Enter");
return;
2023-04-07 13:49:59 +04:00
}
}
}