CourseWork_Bank/Bank/BankEmployeeApp/Controllers/AuthorizationController.cs

63 lines
2.2 KiB
C#
Raw Permalink Normal View History

using BankContracts.BindingModels;
using BankContracts.BusinessLogicContracts;
using BankContracts.SearchModels;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace BankEmployeeApp.Controllers
{
public class AuthorizationController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IEmployeeLogic _employeeLogic;
public AuthorizationController(ILogger<HomeController> logger, IEmployeeLogic employeeLogic)
{
_employeeLogic = employeeLogic;
_logger = logger;
}
[HttpGet]
public IActionResult Register()
{
return View();
}
[HttpPost]
public void Register(EmployeeBindingModel model)
{
if (string.IsNullOrEmpty(model.PhoneNumber) ||
string.IsNullOrEmpty(model.FirstName) ||
string.IsNullOrEmpty(model.MiddleName) ||
string.IsNullOrEmpty(model.LastName) ||
string.IsNullOrEmpty(model.Post) ||
string.IsNullOrEmpty(model.Password))
{
throw new Exception("Все поля должны быть заполнены");
}
_employeeLogic.Create(model);
_logger.LogInformation("Зарегистрирован работник");
Response.Redirect("Enter");
}
public IActionResult Enter()
{
return View();
}
[HttpPost]
public void Enter(EmployeeSearchModel model)
{
if (string.IsNullOrEmpty(model.PhoneNumber) || string.IsNullOrEmpty(model.Password))
{
throw new Exception("Все поля должны быть заполнены");
}
var result = _employeeLogic.ReadElement(model);
HttpContext.Session.SetString(SessionKeys.EmployeePhone, model.PhoneNumber);
HttpContext.Session.SetString(SessionKeys.EmployeePassword, model.Password);
_logger.LogInformation("Был осуществел вход за сотрудника {@employee} и добавлены его данные в сессию", result);
Response.Redirect("../../Home/Index");
}
}
}