Skip to content

Commit

Permalink
feat: Implement GetAllBorrowsQuery feature
Browse files Browse the repository at this point in the history
- Added GetAllBorrowsQueryRequest, GetAllBorrowsQueryRequestValidator,
  GetAllBorrowsQueryResponse, and GetAllBorrowsQueryHandler classes
- GetAllBorrowsQueryHandler retrieves all borrow lends using IUnitOfWork
  and maps them to BorrowLendDTO
- Returns response with borrow lends or appropriate error message
  • Loading branch information
ilyasbozdemir committed Apr 29, 2024
1 parent 15e5793 commit 745092b
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
using LibraryTrackingApp.Application.Features.BookStocks.Queries.Requests;
using LibraryTrackingApp.Application.Features.BookStocks.Queries.Responses;
using LibraryTrackingApp.Application.Interfaces.UnitOfWork;

namespace LibraryTrackingApp.Application.Features.BookStocks.Queries.Handlers;

public class GetAllBookStocksQueryHandler : IRequestHandler<GetAllBookStockQueryRequest, GetAllBookStockQueryResponse>
{
public Task<GetAllBookStockQueryResponse> Handle(GetAllBookStockQueryRequest request, CancellationToken cancellationToken)
private readonly IUnitOfWork<Guid> _unitOfWork;
private readonly IMediator _mediator;
private readonly IMapper _mapper;

public GetAllBookStocksQueryHandler(
IUnitOfWork<Guid> unitOfWork,
IMapper mapper,
IMediator mediator
)
{
_unitOfWork = unitOfWork;
_mediator = mediator;
_mapper = mapper;
}
public async Task<GetAllBookStockQueryResponse> Handle(GetAllBookStockQueryRequest request, CancellationToken cancellationToken)
{
throw new NotImplementedException();
try
{
return new() { };
}
catch (Exception ex)
{
return new()
{
StatusCode = 500,
Success = false,
StateMessages = new[] { $"Bir hata oluştu: {ex.Message}" }

};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,34 @@ namespace LibraryTrackingApp.Application.Features.BookStocks.Queries.Handlers;
public class GetBookStockInfoQueryHandler : IRequestHandler<GetBookStockInfoQueryRequest, GetBookStockInfoQueryResponse>
{
private readonly IUnitOfWork<Guid> _unitOfWork;
private readonly IMediator _mediator;
private readonly IMapper _mapper;

public GetBookStockInfoQueryHandler(IUnitOfWork<Guid> unitOfWork)
public GetBookStockInfoQueryHandler(
IUnitOfWork<Guid> unitOfWork,
IMapper mapper,
IMediator mediator
)
{
_unitOfWork = unitOfWork;
_mediator = mediator;
_mapper = mapper;
}
public Task<GetBookStockInfoQueryResponse> Handle(GetBookStockInfoQueryRequest request, CancellationToken cancellationToken)
public async Task<GetBookStockInfoQueryResponse> Handle(GetBookStockInfoQueryRequest request, CancellationToken cancellationToken)
{
throw new NotImplementedException();
try
{
return new() { };
}
catch (Exception ex)
{
return new()
{
StatusCode = 500,
Success = false,
StateMessages = new[] { $"Bir hata oluştu: {ex.Message}" }

};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using LibraryTrackingApp.Application.Features.BorrowLend.Queries.Requests;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibraryTrackingApp.Application.Features.BorrowLend.Behaviors.Validator;


public class GetBorrowByIdQueryRequestValidator : AbstractValidator<GetBorrowByIdQueryRequest>
{

public GetBorrowByIdQueryRequestValidator()
{
RuleFor(x => x.BorrowId).NotEmpty().WithMessage("BorrowId Boş Olamaz");
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LibraryTrackingApp.Application.Features.BorrowLend.Queries.Requests;
using LibraryTrackingApp.Application.Features.BorrowLend.Queries.Responses;
using LibraryTrackingApp.Application.Interfaces.UnitOfWork;

namespace LibraryTrackingApp.Application.Features.BorrowLend.Queries.Handlers
namespace LibraryTrackingApp.Application.Features.BorrowLend.Queries.Handlers;

public class GetAllBorrowsQueryHandler : IRequestHandler<GetAllBorrowsQueryRequest, GetAllBorrowsQueryResponse>
{
internal class GetAllBorrowsQueryHandler
private readonly IUnitOfWork<Guid> _unitOfWork;
private readonly IMediator _mediator;
private readonly IMapper _mapper;

public GetAllBorrowsQueryHandler(
IUnitOfWork<Guid> unitOfWork,
IMapper mapper,
IMediator mediator
)
{
_unitOfWork = unitOfWork;
_mediator = mediator;
_mapper = mapper;
}

public async Task<GetAllBorrowsQueryResponse> Handle(GetAllBorrowsQueryRequest request, CancellationToken cancellationToken)
{
try
{
return new() { };
}
catch (Exception ex)
{
return new()
{
StatusCode = 500,
Success = false,
StateMessages = new[] { $"Bir hata oluştu: {ex.Message}" }

};
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LibraryTrackingApp.Application.Features.BorrowLend.Queries.Requests;
using LibraryTrackingApp.Application.Features.BorrowLend.Queries.Responses;
using LibraryTrackingApp.Application.Interfaces.UnitOfWork;

namespace LibraryTrackingApp.Application.Features.BorrowLend.Queries.Handlers

namespace LibraryTrackingApp.Application.Features.BorrowLend.Queries.Handlers;

public class GetBorrowByIdQueryHandler : IRequestHandler<GetBorrowByIdQueryRequest, GetBorrowByIdQueryResponse>
{
internal class GetBorrowByIdQueryHandler
private readonly IUnitOfWork<Guid> _unitOfWork;
private readonly IMediator _mediator;
private readonly IMapper _mapper;

public GetBorrowByIdQueryHandler(
IUnitOfWork<Guid> unitOfWork,
IMapper mapper,
IMediator mediator
)
{
_unitOfWork = unitOfWork;
_mediator = mediator;
_mapper = mapper;
}

public async Task<GetBorrowByIdQueryResponse> Handle(GetBorrowByIdQueryRequest request, CancellationToken cancellationToken)
{
try
{
return new() { };
}
catch (Exception ex)
{
return new()
{
StatusCode = 500,
Success = false,
StateMessages = new[] { $"Bir hata oluştu: {ex.Message}" }

};
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LibraryTrackingApp.Application.Features.BorrowLend.Queries.Responses;

namespace LibraryTrackingApp.Application.Features.BorrowLend.Queries.Requests
namespace LibraryTrackingApp.Application.Features.BorrowLend.Queries.Requests;

public class GetAllBorrowsQueryRequest:IRequest<GetAllBorrowsQueryResponse>
{
public class GetAllBorrowsQueryRequest
{
}
}
public int PageSize { get; set; }
public int PageIndex { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LibraryTrackingApp.Application.Features.BorrowLend.Queries.Responses;

namespace LibraryTrackingApp.Application.Features.BorrowLend.Queries.Requests
namespace LibraryTrackingApp.Application.Features.BorrowLend.Queries.Requests;

public class GetBorrowByIdQueryRequest:IRequest<GetBorrowByIdQueryResponse>
{
public class GetBorrowByIdQueryRequest
{
}
public Guid BorrowId { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LibraryTrackingApp.Application.Shared.Wrappers.Results;

namespace LibraryTrackingApp.Application.Features.BorrowLend.Queries.Responses
namespace LibraryTrackingApp.Application.Features.BorrowLend.Queries.Responses;

public class GetAllBorrowsQueryResponse : PaginatedQueryResult<BorrowLendDTO>
{
internal class GetAllBorrowsQueryResponse
{
}
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LibraryTrackingApp.Application.Shared.Wrappers.Results;

namespace LibraryTrackingApp.Application.Features.BorrowLend.Queries.Responses
namespace LibraryTrackingApp.Application.Features.BorrowLend.Queries.Responses;

public class GetBorrowByIdQueryResponse:QueryResult<BorrowLendDTO>
{
internal class GetBorrowByIdQueryResponse
{
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using LibraryTrackingApp.Application.Features.BorrowLend.Commands.Requests;
using LibraryTrackingApp.Application.Features.BorrowLend.Events;
using LibraryTrackingApp.Application.Features.BorrowLend.Queries.Requests;
using LibraryTrackingApp.Infrastructure.Mvc;
using System.Net;

namespace LibraryTrackingApp.WebApi.Controllers.v1;

Expand Down Expand Up @@ -209,23 +211,57 @@ await _mediator.Publish(
[HttpGet("borrow/{borrowId}")]
public async Task<IActionResult> GetBorrowById(Guid borrowId)
{
//eklencek daha
return Ok();
var response = await _mediator.Send(new GetBorrowByIdQueryRequest { BorrowId = borrowId });
await _mediator.Publish(
new BorrowLendEvent()
{
Errors = response.Success ? null : response.StateMessages,
IsSuccessful = response.Success,
BorrowLendType = LibraryTrackingApp.Domain.Enums.BorrowLendType.FetchedSingle
}
);

var responseValue = new
{
IsSuccess = response.Success,
StatusCode = response.StatusCode,
Messages = response.StateMessages.ToArray(),
Data = response.Data,
};

return new JsonResult(responseValue) { StatusCode = response.StatusCode };
}


/// <summary>
/// Tüm ödünç işlemlerini sayfalama ile getirir.
/// </summary>
/// <param name="pageIndex">Sayfa indeksi.</param>
/// <param name="pageSize">Sayfa boyutu.</param>
/// <param name="request">Tüm ödünç kitapları getirme için temsil eden request nesnesi</param>
/// <returns>
/// 200 OK cevabı, işlem başarılı ise;
/// 500 Internal Server Error cevabı, bir hata oluştuğunda.
/// </returns>
[HttpGet("borrows")]
public async Task<IActionResult> GetAllBorrows()
public async Task<IActionResult> GetAllBorrows(GetAllBorrowsQueryRequest request)
{
//eklencek daha
return Ok();
var response = await _mediator.Send(request);
await _mediator.Publish(
new BorrowLendEvent()
{
Errors = response.Success ? null : response.StateMessages,
IsSuccessful = response.Success,
BorrowLendType = LibraryTrackingApp.Domain.Enums.BorrowLendType.FetchedAll
}
);

var responseValue = new
{
IsSuccess = response.Success,
StatusCode = response.StatusCode,
Messages = response.StateMessages.ToArray(),
Data = response.Data,
};

return new JsonResult(responseValue) { StatusCode = response.StatusCode };
}
}

0 comments on commit 745092b

Please sign in to comment.