diff --git a/sample/EasyRepository.Sample/Controllers/BookController.cs b/sample/EasyRepository.Sample/Controllers/BookController.cs index 11c5d96..956f926 100644 --- a/sample/EasyRepository.Sample/Controllers/BookController.cs +++ b/sample/EasyRepository.Sample/Controllers/BookController.cs @@ -8,6 +8,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using EasyRepository.EFCore.Generic; namespace EasyRepository.Sample.Controllers { @@ -15,83 +16,90 @@ namespace EasyRepository.Sample.Controllers [Route("[controller]")] public class BookController : ControllerBase { - private readonly IRepository repository; - public BookController(IRepository repository) + private readonly IUnitOfWork _unitOfWork; + + public BookController(IUnitOfWork unitOfWork) { - this.repository = repository; + _unitOfWork = unitOfWork; } [HttpPost("authors/{authorId}")] public async Task AddBookAsync([FromRoute] Guid authorId,[FromBody] BookRequestDto dto) { - var entity = await repository.AddAsync(new Book + var entity = await _unitOfWork.Repository.AddAsync(new Book { Title = dto.Title, TotalPage = dto.TotalPage, AuthorId = authorId }, default); + await _unitOfWork.Repository.CompleteAsync(); + return Ok(entity); } [HttpPost("authors/{authorId}/range")] public async Task AddBookAsync([FromRoute] Guid authorId, [FromBody] List dto) { - var entity = await repository.AddRangeAsync(dto.Select(s => new Book + var entity = await _unitOfWork.Repository.AddRangeAsync(dto.Select(s => new Book { Title = s.Title, TotalPage = s.TotalPage, AuthorId = authorId }).ToList(), default); + await _unitOfWork.Repository.CompleteAsync(); return Ok(entity); } [HttpPut("{id}/authors/{authorId}")] public async Task UpdateAuthorAsync([FromRoute] Guid id,[FromRoute] Guid authorId, [FromBody] BookRequestDto dto) { - var entity = await repository.GetByIdAsync(true, id); + var entity = await _unitOfWork.Repository.GetByIdAsync(true, id); entity.Title = dto.Title; entity.TotalPage = dto.TotalPage; entity.AuthorId = authorId; - var result = await repository.UpdateAsync(entity); + var result = await _unitOfWork.Repository.UpdateAsync(entity); + await _unitOfWork.Repository.CompleteAsync(); return Ok(result); } [HttpDelete("{id}/hard")] public async Task HardDeleteAsync([FromRoute] Guid id) { - var entity = await repository.GetByIdAsync(true, id); - await repository.HardDeleteAsync(entity); + var entity = await _unitOfWork.Repository.GetByIdAsync(true, id); + await _unitOfWork.Repository.HardDeleteAsync(entity); + await _unitOfWork.Repository.CompleteAsync(); return NoContent(); } [HttpDelete("{id}/soft")] public async Task SoftDeleteAsync([FromRoute] Guid id) { - var entity = await repository.GetByIdAsync(true, id); - await repository.SoftDeleteAsync(entity); + var entity = await _unitOfWork.Repository.GetByIdAsync(true, id); + await _unitOfWork.Repository.SoftDeleteAsync(entity); + await _unitOfWork.Repository.CompleteAsync(); return NoContent(); } [HttpGet("{id}")] public async Task GetByIdAsync([FromRoute] Guid id) { - var entity = await repository.GetByIdAsync(true, id); + var entity = await _unitOfWork.Repository.GetByIdAsync(true, id); return Ok(entity); } [HttpGet("multiple")] public async Task GetMultipleAsync() { - var entities = await repository.GetMultipleAsync(false); + var entities = await _unitOfWork.Repository.GetMultipleAsync(false); return Ok(entities); } [HttpGet("selectable-multiple")] public async Task GetSelectableMultipleAsync() { - var entities = await repository.GetMultipleAsync(false, select => new + var entities = await _unitOfWork.Repository.GetMultipleAsync(false, select => new { SelectTitle = select.Title, SelectTotalPage = select.TotalPage @@ -102,7 +110,7 @@ public async Task GetSelectableMultipleAsync() [HttpGet("filterable-multiple")] public async Task GetFilterableMultipleAsync([FromQuery] BookFilterDto dto) { - var entities = await repository.GetMultipleAsync(false, dto, select => new + var entities = await _unitOfWork.Repository.GetMultipleAsync(false, dto, select => new { SelectTitle = select.Title, SelectTotalPage = select.TotalPage @@ -114,7 +122,7 @@ public async Task GetFilterableMultipleAsync([FromQuery] BookFilt public async Task GetIncludeableFilterableMultipleAsync([FromQuery] BookFilterDto dto) { Func, IIncludableQueryable> include = a => a.Include(i => i.Author); - var entities = await repository.GetMultipleAsync(false, dto, select => new + var entities = await _unitOfWork.Repository.GetMultipleAsync(false, dto, select => new { SelectName = select.Title, SelectDate = select.CreationDate,