CourseWork/University/UniversityRestApi/Controllers/UserController.cs
2024-05-27 23:08:44 +04:00

128 lines
3.3 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.AspNetCore.Mvc;
using UniversityContracts.BindingModels;
using UniversityContracts.BusinessLogicsContracts;
using UniversityContracts.SearchModels;
using UniversityContracts.ViewModels;
using UniversityDataModels.Enums;
namespace UniversityRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class UserController : Controller
{
private readonly ILogger _logger;
private readonly IUserLogic _logic;
public UserController(IUserLogic logic, ILogger<UserController> logger)
{
_logic = logic;
_logger = logger;
}
[HttpGet]
public UserViewModel? LoginWorker(string login, string password)
{
try
{
return _logic.ReadElement(new UserSearchModel
{
Login = login,
Password = password,
Role = UserRole.Работник
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка входа в систему");
throw;
}
}
[HttpGet]
public UserViewModel? LoginStorekeeper(string login, string password)
{
try
{
//
var x = _logic.ReadElement(new UserSearchModel
{
Login = login,
Password = password,
Role = UserRole.Кладовщик
});
//
return _logic.ReadElement(new UserSearchModel
{
Login = login,
Password = password,
Role = UserRole.Кладовщик
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка входа в систему");
throw;
}
}
[HttpGet]
public List<UserViewModel>? GetAll()
{
try
{
return _logic.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка");
throw;
}
}
[HttpPost]
public void RegisterWorker(UserBindingModel model)
{
try
{
model.Role = UserRole.Работник;
_logic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка регистрации");
throw;
}
}
[HttpPost]
public void RegisterStorekeeper(UserBindingModel model)
{
try
{
model.Role = UserRole.Кладовщик;
_logic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка регистрации");
throw;
}
}
[HttpPost]
public void UpdateData(UserBindingModel model)
{
try
{
_logic.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления данных");
throw;
}
}
}
}