Crear Director

This commit is contained in:
Alejandro Sarmiento
2024-02-20 12:24:02 +01:00
parent 8c11d0724b
commit ac3d3d28b5
4 changed files with 46 additions and 1 deletions

View File

@@ -0,0 +1,28 @@
using CleanArchitecture.Application.Features.Directors.Commands.CreateDirector;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace CleanArchitecture.API.Controllers
{
[Route("api/v1/[controller]")]
[ApiController]
public class DirectorController : ControllerBase
{
private IMediator mediator;
public DirectorController(IMediator mediator)
{
this.mediator = mediator;
}
[HttpPost(Name = "CreateDirector")]
[Authorize(Roles ="Administrator")]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<ActionResult<int>> Create([FromBody] CreateDirectorCommand createDirectorCommand)
{
var response = await mediator.Send(createDirectorCommand);
return Ok(new { directorId = response });
}
}
}

View File

@@ -22,6 +22,20 @@ namespace CleanArchitecture.Application.Features.Directors.Commands.CreateDirect
public Task<int> Handle(CreateDirectorCommand request, CancellationToken cancellationToken)
{
var directorEntity = mapper.Map<Director>(request);
unitOfWork.Repository<Director>().AddEntity(directorEntity);
var response = unitOfWork.Complete();
if (response.Result > 0)
{
logger.LogInformation($"Director {directorEntity.Id} is successfully created.");
return Task.FromResult(directorEntity.Id);
}
else
{
logger.LogError($"Director {directorEntity.Id} creation failed.");
throw new Exception("Director creation failed.");
}
}
}
}

View File

@@ -22,6 +22,8 @@ namespace CleanArchitecture.Infrastructure
.LogTo(Console.WriteLine, new[] { DbLoggerCategory.Database.Command.Name }, Microsoft.Extensions.Logging.LogLevel.Information)
);
services.AddScoped<IUnitOfWork, UnitOfWork>();
services.AddScoped(typeof(IAsyncRepository<>), typeof(RepositoryBase<>));
services.AddScoped<IVideoRepository, VideoRepository>();
services.AddScoped<IStreamerRepository, StreamerRepository>();

View File

@@ -1,5 +1,6 @@
using CleanArchitecture.Application.Contracts.Persistence;
using CleanArchitecture.Domain.Common;
using CleanArchitecture.Infrastructure.Repositories;
using System.Collections;
namespace CleanArchitecture.Infrastructure.Persistence
@@ -35,7 +36,7 @@ namespace CleanArchitecture.Infrastructure.Persistence
var type = typeof(T).Name;
if(!repositories.ContainsKey(type))
{
var repositoryType = typeof(IAsyncRepository<>);
var repositoryType = typeof(RepositoryBase<>);
var repositoryInstance = Activator.CreateInstance(repositoryType.MakeGenericType(typeof(T)), context);
repositories.Add(type, repositoryInstance);
}