From e8a6cc2b178bbdf78afe88c781a6fb6a441c2913 Mon Sep 17 00:00:00 2001 From: mfnefd Date: Mon, 25 Nov 2024 18:25:04 +0400 Subject: [PATCH] =?UTF-8?q?add:=20=D0=9A=D0=BE=D0=BD=D1=82=D1=80=D0=BE?= =?UTF-8?q?=D0=BB=D0=BB=D0=B5=D1=80=D1=8B=20=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D1=82=D0=B5=D0=BB=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/Controllers/Controllers.csproj | 6 ++ .../Controllers/Controllers/AuthController.cs | 64 +++++++++++++++++++ .../Controllers/Controllers/UserController.cs | 44 +++++++++++++ .../Extensions/AddDbConnectionService.cs | 19 ++++++ .../Extensions/AddDomainServicesExt.cs | 13 ++++ back/Controllers/Extensions/AddReposExt.cs | 14 ++++ back/Controllers/Program.cs | 5 ++ 7 files changed, 165 insertions(+) create mode 100644 back/Controllers/Controllers/AuthController.cs create mode 100644 back/Controllers/Controllers/UserController.cs create mode 100644 back/Controllers/Extensions/AddDbConnectionService.cs create mode 100644 back/Controllers/Extensions/AddDomainServicesExt.cs create mode 100644 back/Controllers/Extensions/AddReposExt.cs diff --git a/back/Controllers/Controllers.csproj b/back/Controllers/Controllers.csproj index 9daa180..34f05b7 100644 --- a/back/Controllers/Controllers.csproj +++ b/back/Controllers/Controllers.csproj @@ -10,4 +10,10 @@ + + + + + + diff --git a/back/Controllers/Controllers/AuthController.cs b/back/Controllers/Controllers/AuthController.cs new file mode 100644 index 0000000..5184f89 --- /dev/null +++ b/back/Controllers/Controllers/AuthController.cs @@ -0,0 +1,64 @@ +using Contracts.DTO; +using Contracts.Services; +using Contracts.ViewModels; +using Microsoft.AspNetCore.Mvc; +using Services.Support.Exceptions; + +namespace Controllers.Controllers +{ + [ApiController] + [Route("api/[controller]")] + public class AuthController : ControllerBase + { + private readonly IAuthService _authService; + + public AuthController(IAuthService authService) + { + _authService = authService; + } + + [HttpPost] + public async Task> Login([FromBody] UserLoginDTO loginData) + { + try + { + var user = await _authService.Login(loginData); + return Ok(user); + } + catch (ArgumentException ex) + { + return BadRequest(ex.Message); + } + catch (UserNotFoundException ex) + { + return NotFound(ex.Message); + } + catch (Exception ex) + { + return StatusCode(500, ex.Message); + } + } + + [HttpPost("register")] + public async Task> Register([FromBody] UserDto user) + { + try + { + var createdUser = await _authService.Register(user); + return CreatedAtAction(nameof(Login), new { name = createdUser.Name }, createdUser); + } + catch (ArgumentException ex) + { + return BadRequest(ex.Message); + } + catch (AlreadyExistsException ex) + { + return Conflict(ex.Message); + } + catch (Exception ex) + { + return StatusCode(500, ex.Message); + } + } + } +} diff --git a/back/Controllers/Controllers/UserController.cs b/back/Controllers/Controllers/UserController.cs new file mode 100644 index 0000000..bfca307 --- /dev/null +++ b/back/Controllers/Controllers/UserController.cs @@ -0,0 +1,44 @@ +using Contracts.DTO; +using Contracts.SearchModels; +using Contracts.Services; +using Contracts.ViewModels; +using Microsoft.AspNetCore.Mvc; + +namespace Controllers.Controllers +{ + [ApiController] + [Route("api/[controller]")] + public class UserController : ControllerBase + { + private readonly IUserService _userService; + + public UserController(IUserService userService) + { + _userService = userService; + } + + [HttpGet] + public async Task> GetUser([FromQuery] UserSearch search) + { + var user = await _userService.GetDetails(search); + + return Ok(user); + } + + [HttpPut] + public async Task> UpdateUser([FromBody] UserDto user) + { + var updatedUser = await _userService.UpdateUserData(user); + + return Ok(updatedUser); + } + + [HttpDelete] + public async Task> DeleteUser([FromQuery] UserSearch search) + { + var deletedUser = await _userService.Delete(search); + + return Ok(deletedUser); + } + } +} diff --git a/back/Controllers/Extensions/AddDbConnectionService.cs b/back/Controllers/Extensions/AddDbConnectionService.cs new file mode 100644 index 0000000..e78595e --- /dev/null +++ b/back/Controllers/Extensions/AddDbConnectionService.cs @@ -0,0 +1,19 @@ +using Infrastructure; +using Microsoft.EntityFrameworkCore; + +namespace Controllers.Extensions; + +public static class DbConnectionServiceExtension +{ + public static void AddDbConnectionService(this IServiceCollection services) + { + var host = Environment.GetEnvironmentVariable("DB_HOST"); + var database = Environment.GetEnvironmentVariable("DB_NAME"); + var username = Environment.GetEnvironmentVariable("DB_USER"); + var password = Environment.GetEnvironmentVariable("DB_PASSWORD"); + var connectionString = $"Host={host};Database={database};Username={username};Password={password}"; + + services.AddDbContext(options => options.UseNpgsql(connectionString)); + services.AddSingleton, DbContextFactory>(); + } +} \ No newline at end of file diff --git a/back/Controllers/Extensions/AddDomainServicesExt.cs b/back/Controllers/Extensions/AddDomainServicesExt.cs new file mode 100644 index 0000000..f68caa1 --- /dev/null +++ b/back/Controllers/Extensions/AddDomainServicesExt.cs @@ -0,0 +1,13 @@ +using Contracts.Services; +using Services.Domain; + +namespace Controllers.Extensions; + +public static class AddDomainServicesExtension +{ + public static void AddDomainServices(this IServiceCollection services) + { + services.AddSingleton(); + services.AddSingleton(); + } +} \ No newline at end of file diff --git a/back/Controllers/Extensions/AddReposExt.cs b/back/Controllers/Extensions/AddReposExt.cs new file mode 100644 index 0000000..27d5b68 --- /dev/null +++ b/back/Controllers/Extensions/AddReposExt.cs @@ -0,0 +1,14 @@ +using Contracts.Repositories; +using Contracts.Services; +using Infrastructure.Repositories; +using Services.Domain; + +namespace Controllers.Extensions; + +public static class AddReposExtension +{ + public static void AddRepos(this IServiceCollection services) + { + services.AddSingleton(); + } +} \ No newline at end of file diff --git a/back/Controllers/Program.cs b/back/Controllers/Program.cs index 48863a6..1bb1553 100644 --- a/back/Controllers/Program.cs +++ b/back/Controllers/Program.cs @@ -1,6 +1,11 @@ +using Controllers.Extensions; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. +builder.Services.AddRepos(); +builder.Services.AddDomainServices(); +builder.Services.AddDbConnectionService(); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle