updating base repository

This commit is contained in:
Liam Pietralla 2024-10-28 12:48:30 +11:00
parent 30d4491108
commit b7878fcd6b
3 changed files with 33 additions and 9 deletions

View File

@ -11,19 +11,14 @@ namespace UserManager.API.Controllers
[Route("api/[controller]")] [Route("api/[controller]")]
public class UserController(ILogger<UserController> logger, IUserService userService, IValidator<CreateUserRequestDto> createUserValidator) : ControllerBase public class UserController(ILogger<UserController> logger, IUserService userService, IValidator<CreateUserRequestDto> createUserValidator) : ControllerBase
{ {
private readonly ILogger<UserController> _logger = logger;
private readonly IUserService _userService = userService;
private readonly IValidator<CreateUserRequestDto> _createUserValidator = createUserValidator;
[HttpPost(Name = "CreateUser")] [HttpPost(Name = "CreateUser")]
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<CreateUserResponseDto>> CreateUserAsync(CreateUserRequestDto request) public async Task<ActionResult<CreateUserResponseDto>> 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) if (!validationResult.IsValid)
{ {
@ -31,7 +26,7 @@ namespace UserManager.API.Controllers
} }
else else
{ {
CreateUserResponseDto response = await _userService.CreateUserAsync(request); CreateUserResponseDto response = await userService.CreateUserAsync(request);
return Ok(response); return Ok(response);
} }
} }

View File

@ -3,5 +3,8 @@
public interface IBaseRepository<TEntity> where TEntity : class public interface IBaseRepository<TEntity> where TEntity : class
{ {
Task CreateAsync(TEntity entity); Task CreateAsync(TEntity entity);
void Update(TEntity entity);
void Delete(TEntity entity);
Task DeleteAsync(object?[]? id);
} }
} }

View File

@ -5,12 +5,38 @@ namespace UserManager.Infrastructure
{ {
public class BaseRepository<TEntity>(UserManagerContext context) : IBaseRepository<TEntity> where TEntity : class public class BaseRepository<TEntity>(UserManagerContext context) : IBaseRepository<TEntity> where TEntity : class
{ {
private readonly UserManagerContext _context = context;
private readonly DbSet<TEntity> _dbSet = context.Set<TEntity>(); private readonly DbSet<TEntity> _dbSet = context.Set<TEntity>();
public async Task<TEntity?> GetByIdAsync(object?[]? id)
{
return await _dbSet.FindAsync(id);
}
public async Task CreateAsync(TEntity entity) public async Task CreateAsync(TEntity entity)
{ {
await _dbSet.AddAsync(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);
}
}
} }
} }