using ImplementerApp.Models; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; using BusinessLogic.BusinessLogic; using Contracts.BusinessLogicsContracts; using Contracts.ViewModels; using DataModels.Models; using Contracts.BindingModels; using DatabaseImplement.Models; namespace ImplementerApp.Controllers { public class HomeController : Controller { private readonly ILogger _logger; private readonly ImplementerData _data; public HomeController(ILogger logger, ImplementerData data) { _logger = logger; _data = data; } private bool IsLoggedIn { get { return UserImplementer.user != null; } } private int UserId { get { return UserImplementer.user!.Id; } } [HttpPost] public JsonResult CheckLogin(string login) { try { var unique = _data.CheckLogin(login); return Json(new { isUnique = unique }); } catch (Exception) { return Json(new { isUnique = false }); } } public IActionResult IndexNonReg() { if (!IsLoggedIn) return View(); return RedirectToAction("Index"); } public IActionResult Index() { if (!IsLoggedIn) return RedirectToAction("IndexNonReg"); return View(); } [HttpGet] public IActionResult Enter() { if (!IsLoggedIn) return View(); return RedirectToAction("Index"); } [HttpPost] public void Enter(string login, string password) { try { var user = _data.Login(login, password); if (user != null) { UserImplementer.user = user; Response.Redirect("Index"); } Response.Redirect("Enter"); } catch (Exception) { Response.Redirect("IndexNonReg"); } } [HttpGet] public IActionResult Register() { return View(); } public IActionResult Logout() { UserImplementer.user = null; return RedirectToAction("IndexNonReg"); } [HttpPost] public void Register(string name, string login, string email, string password1, string password2) { try { if (password1 == password2 && _data.Register(new() { Email = email, Login = login, Name = name, Password = password1 })) { Response.Redirect("Index"); } } catch (Exception) { Response.Redirect("IndexNonReg"); } } [HttpGet] public IActionResult Privacy() { if (IsLoggedIn) return View(UserImplementer.user); return RedirectToAction("IndexNonReg"); } [HttpPost] public IActionResult Privacy(int id, string login, string email, string password, string name) { if (!IsLoggedIn) return RedirectToAction("IndexNonReg"); try { ImplementerBindingModel user = new() { Id = id, Login = login, Email = email, Password = password, Name = name }; if (_data.UpdateUser(user)) { UserImplementer.user = new ImplementerViewModel { Id = id, Login = login, Password = password, Name = name, Email = email }; } return View(user); } catch (Exception) { return RedirectToAction("IndexNonReg"); } } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(); } } }