Skip to content

Commit

Permalink
♻️ Remove repository object from Book controller
Browse files Browse the repository at this point in the history
  • Loading branch information
furkandeveloper committed Jun 20, 2022
1 parent 29b36a0 commit fb5c794
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions sample/EasyRepository.Sample/Controllers/BookController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,90 +8,98 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EasyRepository.EFCore.Generic;

namespace EasyRepository.Sample.Controllers
{
[ApiController]
[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<IActionResult> AddBookAsync([FromRoute] Guid authorId,[FromBody] BookRequestDto dto)
{
var entity = await repository.AddAsync<Book, Guid>(new Book
var entity = await _unitOfWork.Repository.AddAsync<Book, Guid>(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<IActionResult> AddBookAsync([FromRoute] Guid authorId, [FromBody] List<BookRequestDto> dto)
{
var entity = await repository.AddRangeAsync<Book, Guid>(dto.Select(s => new Book
var entity = await _unitOfWork.Repository.AddRangeAsync<Book, Guid>(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<IActionResult> UpdateAuthorAsync([FromRoute] Guid id,[FromRoute] Guid authorId, [FromBody] BookRequestDto dto)
{
var entity = await repository.GetByIdAsync<Book>(true, id);
var entity = await _unitOfWork.Repository.GetByIdAsync<Book>(true, id);
entity.Title = dto.Title;
entity.TotalPage = dto.TotalPage;
entity.AuthorId = authorId;
var result = await repository.UpdateAsync<Book, Guid>(entity);
var result = await _unitOfWork.Repository.UpdateAsync<Book, Guid>(entity);
await _unitOfWork.Repository.CompleteAsync();
return Ok(result);
}

[HttpDelete("{id}/hard")]
public async Task<IActionResult> HardDeleteAsync([FromRoute] Guid id)
{
var entity = await repository.GetByIdAsync<Book>(true, id);
await repository.HardDeleteAsync<Book>(entity);
var entity = await _unitOfWork.Repository.GetByIdAsync<Book>(true, id);
await _unitOfWork.Repository.HardDeleteAsync<Book>(entity);
await _unitOfWork.Repository.CompleteAsync();
return NoContent();
}

[HttpDelete("{id}/soft")]
public async Task<IActionResult> SoftDeleteAsync([FromRoute] Guid id)
{
var entity = await repository.GetByIdAsync<Book>(true, id);
await repository.SoftDeleteAsync<Book, Guid>(entity);
var entity = await _unitOfWork.Repository.GetByIdAsync<Book>(true, id);
await _unitOfWork.Repository.SoftDeleteAsync<Book, Guid>(entity);
await _unitOfWork.Repository.CompleteAsync();
return NoContent();
}

[HttpGet("{id}")]
public async Task<IActionResult> GetByIdAsync([FromRoute] Guid id)
{
var entity = await repository.GetByIdAsync<Book>(true, id);
var entity = await _unitOfWork.Repository.GetByIdAsync<Book>(true, id);
return Ok(entity);
}

[HttpGet("multiple")]
public async Task<IActionResult> GetMultipleAsync()
{
var entities = await repository.GetMultipleAsync<Book>(false);
var entities = await _unitOfWork.Repository.GetMultipleAsync<Book>(false);
return Ok(entities);
}

[HttpGet("selectable-multiple")]
public async Task<IActionResult> GetSelectableMultipleAsync()
{
var entities = await repository.GetMultipleAsync<Book, object>(false, select => new
var entities = await _unitOfWork.Repository.GetMultipleAsync<Book, object>(false, select => new
{
SelectTitle = select.Title,
SelectTotalPage = select.TotalPage
Expand All @@ -102,7 +110,7 @@ public async Task<IActionResult> GetSelectableMultipleAsync()
[HttpGet("filterable-multiple")]
public async Task<IActionResult> GetFilterableMultipleAsync([FromQuery] BookFilterDto dto)
{
var entities = await repository.GetMultipleAsync<Book, BookFilterDto, object>(false, dto, select => new
var entities = await _unitOfWork.Repository.GetMultipleAsync<Book, BookFilterDto, object>(false, dto, select => new
{
SelectTitle = select.Title,
SelectTotalPage = select.TotalPage
Expand All @@ -114,7 +122,7 @@ public async Task<IActionResult> GetFilterableMultipleAsync([FromQuery] BookFilt
public async Task<IActionResult> GetIncludeableFilterableMultipleAsync([FromQuery] BookFilterDto dto)
{
Func<IQueryable<Book>, IIncludableQueryable<Book, object>> include = a => a.Include(i => i.Author);
var entities = await repository.GetMultipleAsync<Book, BookFilterDto, object>(false, dto, select => new
var entities = await _unitOfWork.Repository.GetMultipleAsync<Book, BookFilterDto, object>(false, dto, select => new
{
SelectName = select.Title,
SelectDate = select.CreationDate,
Expand Down

0 comments on commit fb5c794

Please sign in to comment.