Coursach/Course/ImplementerApp/Controllers/HomeController.cs

100 lines
3.2 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 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)
{
var user = _data.Login(login, password);
if (user != null)
2024-05-28 19:21:01 +04:00
{
2024-05-28 21:41:19 +04:00
UserImplementer.user = user;
Response.Redirect("Index");
2024-05-28 19:21:01 +04:00
}
2024-05-28 21:41:19 +04:00
Response.Redirect("Enter");
}
[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)
{
if (password1 == password2 && _data.Register(new() { Email = email, Login = login, Name = name, Password = password1 }))
{
Response.Redirect("Index");
}
}
[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");
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);
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
}