using ComputerStoreContracts.BindingModels; using ComputerStoreContracts.BusinessLogicContracts; using ComputerStoreContracts.SearchModels; using Microsoft.AspNetCore.Mvc; using System.Text.RegularExpressions; namespace ComputerStoreSellerApp.Controllers { public class AuthController : Controller { private ISellerLogic _sellerLogic; public AuthController(ISellerLogic sellerLogic) { _sellerLogic = sellerLogic; } [HttpGet] public IActionResult Login() { return View(); } [HttpPost] public IActionResult Login(string login, string password) { if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password)) { TempData.Add("error", "Необходимо заполнить все поля"); return RedirectToAction("Login", "Auth"); } if (login.Length > 20) { TempData.Add("error", "Логин не может быть длиньше 20 символов"); return RedirectToAction("Login", "Auth"); } if (password.Length > 30) { TempData.Add("error", "Пароль не может быть длиньше 20 символов"); return RedirectToAction("Login", "Auth"); } var seller = _sellerLogic.ReadElement(new SellerSearchModel { Username = login }); if (seller is null || !seller.Password.Equals(password)) { TempData.Add("error", "Неверный логин или пароль"); return RedirectToAction("Login", "Auth"); } HttpContext.Session.SetString("login", login); return RedirectToAction("List", "Order"); } [HttpGet] public IActionResult Register() { return View(); } [HttpPost] public IActionResult Register(string login, string password, string name, string surname, string middlename) { try { _sellerLogic.Create(new SellerBindingModel() { Username = login, Password = password, FirstName = name, LastName = surname, MiddleName = middlename }); } catch (Exception e) { TempData.Add("error", Regex.Replace(e.Message, "[a-zA-Z\\(\\)']", "")); return RedirectToAction("Register", "Auth"); } return RedirectToAction("Login", "Auth"); } [HttpGet] public IActionResult Logout() { HttpContext.Session.Remove("login"); return RedirectToAction("Login", "Auth"); } } }