diff --git a/src/UserManager.API/Controllers/UserController.cs b/src/UserManager.API/Controllers/UserController.cs index 1b258ac..98faf35 100644 --- a/src/UserManager.API/Controllers/UserController.cs +++ b/src/UserManager.API/Controllers/UserController.cs @@ -11,19 +11,14 @@ namespace UserManager.API.Controllers [Route("api/[controller]")] public class UserController(ILogger logger, IUserService userService, IValidator createUserValidator) : ControllerBase { - private readonly ILogger _logger = logger; - private readonly IUserService _userService = userService; - - private readonly IValidator _createUserValidator = createUserValidator; - [HttpPost(Name = "CreateUser")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] public async Task> CreateUserAsync(CreateUserRequestDto request) { - _logger.LogInformation("Creating user with first name {FirstName} and last name {LastName}", request.FirstName, request.LastName); + logger.LogInformation("Creating user with first name {FirstName} and last name {LastName}", request.FirstName, request.LastName); - ValidationResult? validationResult = await _createUserValidator.ValidateAsync(request); + ValidationResult? validationResult = await createUserValidator.ValidateAsync(request); if (!validationResult.IsValid) { @@ -31,7 +26,7 @@ namespace UserManager.API.Controllers } else { - CreateUserResponseDto response = await _userService.CreateUserAsync(request); + CreateUserResponseDto response = await userService.CreateUserAsync(request); return Ok(response); } } diff --git a/src/UserManager.Application/Interfaces/IBaseRepository.cs b/src/UserManager.Application/Interfaces/IBaseRepository.cs index 46aaaaa..9ce443a 100644 --- a/src/UserManager.Application/Interfaces/IBaseRepository.cs +++ b/src/UserManager.Application/Interfaces/IBaseRepository.cs @@ -3,5 +3,8 @@ public interface IBaseRepository where TEntity : class { Task CreateAsync(TEntity entity); + void Update(TEntity entity); + void Delete(TEntity entity); + Task DeleteAsync(object?[]? id); } } diff --git a/src/UserManager.Infrastructure/BaseRepository.cs b/src/UserManager.Infrastructure/BaseRepository.cs index 679eb54..20a99e4 100644 --- a/src/UserManager.Infrastructure/BaseRepository.cs +++ b/src/UserManager.Infrastructure/BaseRepository.cs @@ -5,12 +5,38 @@ namespace UserManager.Infrastructure { public class BaseRepository(UserManagerContext context) : IBaseRepository where TEntity : class { - private readonly UserManagerContext _context = context; private readonly DbSet _dbSet = context.Set(); + public async Task GetByIdAsync(object?[]? id) + { + return await _dbSet.FindAsync(id); + } + public async Task CreateAsync(TEntity entity) { await _dbSet.AddAsync(entity); } + + public void Update(TEntity entity) + { + _dbSet.Attach(entity); + context.Entry(entity).State = EntityState.Modified; + } + + public void Delete(TEntity entity) + { + _dbSet.Attach(entity); + context.Entry(entity).State = EntityState.Deleted; + } + + public async Task DeleteAsync(object?[]? id) + { + TEntity? entity = await GetByIdAsync(id); + + if (entity != null) + { + Delete(entity); + } + } } }