Merge branch 'Sec_16_UnitOfWork/69_Ejecutando_UnitOfWork' into dev
This commit is contained in:
@@ -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 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,6 +22,20 @@ namespace CleanArchitecture.Application.Features.Directors.Commands.CreateDirect
|
|||||||
public Task<int> Handle(CreateDirectorCommand request, CancellationToken cancellationToken)
|
public Task<int> Handle(CreateDirectorCommand request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var directorEntity = mapper.Map<Director>(request);
|
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.");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ namespace CleanArchitecture.Infrastructure
|
|||||||
.LogTo(Console.WriteLine, new[] { DbLoggerCategory.Database.Command.Name }, Microsoft.Extensions.Logging.LogLevel.Information)
|
.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(typeof(IAsyncRepository<>), typeof(RepositoryBase<>));
|
||||||
services.AddScoped<IVideoRepository, VideoRepository>();
|
services.AddScoped<IVideoRepository, VideoRepository>();
|
||||||
services.AddScoped<IStreamerRepository, StreamerRepository>();
|
services.AddScoped<IStreamerRepository, StreamerRepository>();
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using CleanArchitecture.Application.Contracts.Persistence;
|
using CleanArchitecture.Application.Contracts.Persistence;
|
||||||
using CleanArchitecture.Domain.Common;
|
using CleanArchitecture.Domain.Common;
|
||||||
|
using CleanArchitecture.Infrastructure.Repositories;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
|
|
||||||
namespace CleanArchitecture.Infrastructure.Persistence
|
namespace CleanArchitecture.Infrastructure.Persistence
|
||||||
@@ -35,7 +36,7 @@ namespace CleanArchitecture.Infrastructure.Persistence
|
|||||||
var type = typeof(T).Name;
|
var type = typeof(T).Name;
|
||||||
if(!repositories.ContainsKey(type))
|
if(!repositories.ContainsKey(type))
|
||||||
{
|
{
|
||||||
var repositoryType = typeof(IAsyncRepository<>);
|
var repositoryType = typeof(RepositoryBase<>);
|
||||||
var repositoryInstance = Activator.CreateInstance(repositoryType.MakeGenericType(typeof(T)), context);
|
var repositoryInstance = Activator.CreateInstance(repositoryType.MakeGenericType(typeof(T)), context);
|
||||||
repositories.Add(type, repositoryInstance);
|
repositories.Add(type, repositoryInstance);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user