Coursach/Course/ImplementerApp/Controllers/HomeController.cs

128 lines
4.0 KiB
C#
Raw Normal View History

2024-04-28 17:11:40 +04:00
using ImplementerApp.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using BusinessLogic.BusinessLogic;
using Contracts.BusinessLogicsContracts;
2024-04-29 22:21:21 +04:00
using Contracts.ViewModels;
using DataModels.Models;
2024-05-23 20:02:34 +04:00
using Contracts.BindingModels;
2024-05-26 23:23:24 +04:00
using DatabaseImplement.Models;
2024-04-28 17:11:40 +04:00
namespace ImplementerApp.Controllers
{
public class HomeController : Controller
{
2024-05-28 21:41:19 +04:00
private readonly ILogger<HomeController> _logger;
private readonly ImplementerData _data;
public HomeController(ILogger<HomeController> logger, ImplementerData data)
{
_logger = logger;
_data = data;
}
private bool IsLoggedIn { get { return UserImplementer.user != null; } }
private int UserId { get { return UserImplementer.user!.Id; } }
2024-05-28 19:21:01 +04:00
2024-05-28 22:40:13 +04:00
[HttpPost]
public JsonResult CheckLogin(string login)
{
2024-05-29 15:37:04 +04:00
try
{
var unique = _data.CheckLogin(login);
return Json(new { isUnique = unique });
}
catch (Exception) {
return Json(new { isUnique = false });
}
2024-05-28 22:40:13 +04:00
}
2024-05-28 21:41:19 +04:00
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");
2024-05-26 23:23:24 +04:00
}
2024-05-28 21:41:19 +04:00
[HttpPost]
public void Enter(string login, string password)
{
2024-05-29 15:37:04 +04:00
try {
var user = _data.Login(login, password);
if (user != null)
{
UserImplementer.user = user;
Response.Redirect("Index");
}
Response.Redirect("Enter");
} catch (Exception)
2024-05-28 19:21:01 +04:00
{
2024-05-29 15:37:04 +04:00
Response.Redirect("IndexNonReg");
2024-05-28 19:21:01 +04:00
}
2024-05-29 15:37:04 +04:00
2024-05-28 21:41:19 +04:00
}
[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)
{
2024-05-29 15:37:04 +04:00
try
2024-05-28 21:41:19 +04:00
{
2024-05-29 15:37:04 +04:00
if (password1 == password2 && _data.Register(new() { Email = email, Login = login, Name = name, Password = password1 }))
{
Response.Redirect("Index");
}
} catch (Exception)
{
Response.Redirect("IndexNonReg");
2024-05-28 21:41:19 +04:00
}
}
[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");
2024-05-29 15:37:04 +04:00
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)
2024-05-28 21:41:19 +04:00
{
2024-05-29 15:37:04 +04:00
return RedirectToAction("IndexNonReg");
2024-05-28 21:41:19 +04:00
}
2024-05-28 19:21:01 +04:00
}
2024-04-28 17:11:40 +04:00
2024-05-28 21:41:19 +04:00
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View();
}
}
2024-05-28 19:21:01 +04:00
}