add: Контроллеры пользователя
This commit is contained in:
parent
f7de7ab197
commit
e8a6cc2b17
@ -10,4 +10,10 @@
|
|||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Contracts\Contracts.csproj" />
|
||||||
|
<ProjectReference Include="..\Services\Services.csproj" />
|
||||||
|
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
64
back/Controllers/Controllers/AuthController.cs
Normal file
64
back/Controllers/Controllers/AuthController.cs
Normal file
@ -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<ActionResult<UserViewModel>> 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<ActionResult<UserViewModel>> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
44
back/Controllers/Controllers/UserController.cs
Normal file
44
back/Controllers/Controllers/UserController.cs
Normal file
@ -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<ActionResult<UserViewModel>> GetUser([FromQuery] UserSearch search)
|
||||||
|
{
|
||||||
|
var user = await _userService.GetDetails(search);
|
||||||
|
|
||||||
|
return Ok(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPut]
|
||||||
|
public async Task<ActionResult<UserViewModel>> UpdateUser([FromBody] UserDto user)
|
||||||
|
{
|
||||||
|
var updatedUser = await _userService.UpdateUserData(user);
|
||||||
|
|
||||||
|
return Ok(updatedUser);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpDelete]
|
||||||
|
public async Task<ActionResult<UserViewModel>> DeleteUser([FromQuery] UserSearch search)
|
||||||
|
{
|
||||||
|
var deletedUser = await _userService.Delete(search);
|
||||||
|
|
||||||
|
return Ok(deletedUser);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
19
back/Controllers/Extensions/AddDbConnectionService.cs
Normal file
19
back/Controllers/Extensions/AddDbConnectionService.cs
Normal file
@ -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<DatabaseContext>(options => options.UseNpgsql(connectionString));
|
||||||
|
services.AddSingleton<IDbContextFactory<DatabaseContext>, DbContextFactory>();
|
||||||
|
}
|
||||||
|
}
|
13
back/Controllers/Extensions/AddDomainServicesExt.cs
Normal file
13
back/Controllers/Extensions/AddDomainServicesExt.cs
Normal file
@ -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<IAuthService, AuthService>();
|
||||||
|
services.AddSingleton<IUserService, UserService>();
|
||||||
|
}
|
||||||
|
}
|
14
back/Controllers/Extensions/AddReposExt.cs
Normal file
14
back/Controllers/Extensions/AddReposExt.cs
Normal file
@ -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<IUserRepo, UserRepo>();
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,11 @@
|
|||||||
|
using Controllers.Extensions;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
|
builder.Services.AddRepos();
|
||||||
|
builder.Services.AddDomainServices();
|
||||||
|
builder.Services.AddDbConnectionService();
|
||||||
|
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||||
|
Loading…
Reference in New Issue
Block a user