From 0517a6cfb02954f51d81bd0a55e4c5f1f90a22d9 Mon Sep 17 00:00:00 2001 From: mfnefd Date: Wed, 5 Jun 2024 15:14:55 +0400 Subject: [PATCH] fix role controller and add user controller --- RestAPI/Controllers/RoleController.cs | 4 +- RestAPI/Controllers/UserController.cs | 128 ++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 RestAPI/Controllers/UserController.cs diff --git a/RestAPI/Controllers/RoleController.cs b/RestAPI/Controllers/RoleController.cs index 0121b8b..9f03335 100644 --- a/RestAPI/Controllers/RoleController.cs +++ b/RestAPI/Controllers/RoleController.cs @@ -32,7 +32,7 @@ namespace RestAPI.Controllers } catch (ElementNotFoundException ex) { - _logger.LogInformation(ex, "Element not found"); + _logger.LogInformation(ex, "Role not found"); return Results.NoContent(); } catch (Exception ex) @@ -87,7 +87,7 @@ namespace RestAPI.Controllers } } - [HttpPut] + [HttpPatch] public IResult Update([FromBody] RoleBindingModel model) { try diff --git a/RestAPI/Controllers/UserController.cs b/RestAPI/Controllers/UserController.cs new file mode 100644 index 0000000..f985b4d --- /dev/null +++ b/RestAPI/Controllers/UserController.cs @@ -0,0 +1,128 @@ +using BusinessLogic.BusinessLogic; +using Contracts.BindingModels; +using Contracts.BusinessLogicContracts; +using Contracts.Exceptions; +using Contracts.SearchModels; +using Microsoft.AspNetCore.Mvc; + +namespace RestAPI.Controllers +{ + [Route("[controller]/[action]")] + [ApiController] + public class UserController + { + private readonly ILogger _logger; + private readonly IUserLogic _userLogic; + + public UserController(ILogger logger, IUserLogic userLogic) + { + _userLogic = userLogic; + _logger = logger; + } + + [HttpPost] + public IResult Login([FromBody] UserBindingModel model) + { + try + { + var res = _userLogic.Login(model); + return Results.Ok(res); + } + catch (ElementNotFoundException ex) + { + _logger.LogInformation(ex, "User not found"); + return Results.NoContent(); + } + catch (AccountException ex) + { + _logger.LogWarning(ex, "Wrong login data"); + return Results.BadRequest(ex.Message); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error get user"); + return Results.Problem(ex.Message); + } + } + + [HttpPost] + public IResult Registration([FromBody] UserBindingModel model) + { + try + { + var res = _userLogic.Login(model); + return Results.Ok(res); + } + catch (AccountException ex) + { + _logger.LogWarning(ex, "Wrong registration data"); + return Results.BadRequest(ex.Message); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error create user"); + return Results.Problem(ex.Message); + } + } + + [HttpGet] + public IResult Get([FromQuery] UserSearchModel model) + { + try + { + var res = _userLogic.ReadElement(model); + return Results.Ok(res); + } + catch (ElementNotFoundException ex) + { + _logger.LogInformation(ex, "User not found"); + return Results.NoContent(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error get user"); + return Results.Problem(ex.Message); + } + } + + [HttpPatch] + public IResult Update([FromBody] UserBindingModel model) + { + try + { + var res = _userLogic.Update(model); + return Results.Ok(res); + } + catch (AccountException ex) + { + _logger.LogWarning(ex, "Wrong update data"); + return Results.BadRequest(ex.Message); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error update user"); + return Results.Problem(ex.Message); + } + } + + [HttpDelete] + public IResult Delete(Guid id) + { + try + { + var res = _userLogic.Delete(new() { Id = id }); + return Results.Ok(res); + } + catch (ElementNotFoundException ex) + { + _logger.LogInformation(ex, "User not found"); + return Results.NoContent(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error delete user"); + return Results.Problem(ex.Message); + } + } + } +} \ No newline at end of file