Skip to content

Commit

Permalink
Merge pull request #240 from raffacabofrio/master
Browse files Browse the repository at this point in the history
Ordenando 'Meus pedidos' por CreationDate desc.
  • Loading branch information
walter-lopes authored Mar 27, 2019
2 parents 3350ac8 + fa1459a commit 4e93a07
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static IServiceCollection RegisterRepositoryServices(
services.AddScoped<IValidator<Book>, BookValidator>();
services.AddScoped<IValidator<Category>, CategoryValidator>();
services.AddScoped<IValidator<ContactUs>, ContactUsValidator>();
services.AddScoped<IValidator<BookUser>, BookUserValidator>();

//Auth
services.AddScoped<IApplicationSignInManager, ApplicationSignInManager>();
Expand Down
2 changes: 1 addition & 1 deletion ShareBook/ShareBook.Api/Controllers/BookController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public Result Requested(Guid bookId)
[HttpGet("MyRequests/{page}/{items}")]
public PagedList<MyBookRequestVM> MyRequests(int page, int items)
{
var donation = _bookUserService.GetRequestsByUser();
var donation = _bookUserService.GetRequestsByUser(page, items);
var myBooksRequestsVM = Mapper.Map<List<MyBookRequestVM>>(donation.Items);

return new PagedList<MyBookRequestVM>()
Expand Down
30 changes: 30 additions & 0 deletions ShareBook/ShareBook.Domain/Validators/BookUserValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using FluentValidation;
using ShareBook.Domain.Enums;

namespace ShareBook.Domain.Validators
{
public class BookUserValidator : AbstractValidator<BookUser>
{
#region Messages
public const string Book = "Livro é obrigatório";
public const string Requester = "Solicitante do livro é obrigatório";
public const string RequesterReason = "Justificativa do solicitante é obrigatória";
#endregion

public BookUserValidator()
{
RuleFor(b => b.BookId)
.NotEmpty()
.WithMessage(Book);

RuleFor(b => b.UserId)
.NotEmpty()
.WithMessage(Requester);

RuleFor(b => b.Reason)
.NotEmpty()
.WithMessage(RequesterReason);

}
}
}
16 changes: 2 additions & 14 deletions ShareBook/ShareBook.Service/Book/BookService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ private PagedList<Book> SearchBooks(Expression<Func<Book, bool>> filter, int pag

private PagedList<Book> SearchBooks<TKey>(Expression<Func<Book, bool>> filter, int page, int itemsPerPage, Expression<Func<Book, TKey>> expression)
{
var result = _repository.Get()
var query = _repository.Get()
.Where(filter)
.OrderByDescending(expression)
.Select(u => new Book
Expand Down Expand Up @@ -366,19 +366,7 @@ private PagedList<Book> SearchBooks<TKey>(Expression<Func<Book, bool>> filter, i
Category = u.Category
});

return FormatPagedList(result, page, itemsPerPage, result.Count());
}

private PagedList<Book> FormatPagedList(IQueryable<Book> list, int page, int itemsPerPage, int total)
{
var skip = (page - 1) * itemsPerPage;
return new PagedList<Book>()
{
Page = page,
ItemsPerPage = itemsPerPage,
TotalItems = total,
Items = list.Skip(skip).Take(itemsPerPage).ToList()
};
return FormatPagedList(query, page, itemsPerPage);
}

private bool BookAlreadyApproved(Guid bookId)
Expand Down
24 changes: 18 additions & 6 deletions ShareBook/ShareBook.Service/BookUser/BookUserService.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Microsoft.EntityFrameworkCore;
using FluentValidation;
using Microsoft.EntityFrameworkCore;
using ShareBook.Domain;
using ShareBook.Domain.Common;
using ShareBook.Domain.Enums;
using ShareBook.Domain.Exceptions;
using ShareBook.Repository;
using ShareBook.Repository.Repository;
using ShareBook.Repository.UoW;
using ShareBook.Service.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -14,15 +16,20 @@

namespace ShareBook.Service
{
public class BookUserService : IBookUserService
public class BookUserService : BaseService<BookUser>, IBookUserService
{

private readonly IBookUserRepository _bookUserRepository;
private readonly IBookService _bookService;
private readonly IBookUsersEmailService _bookUsersEmailService;

public BookUserService(IBookUserRepository bookUserRepository, IBookService bookService,
IBookUsersEmailService bookUsersEmailService, IUnitOfWork unitOfWork)
public BookUserService(
IBookUserRepository bookUserRepository,
IBookService bookService,
IBookUsersEmailService bookUsersEmailService,
IUnitOfWork unitOfWork,
IValidator<BookUser> validator)
: base(bookUserRepository, unitOfWork, validator)
{
_bookUserRepository = bookUserRepository;
_bookService = bookService;
Expand Down Expand Up @@ -149,10 +156,15 @@ private void CancelBookUsersAndSendNotification(Book book){
NotifyUsersBookCanceled(book);
}

public PagedList<BookUser> GetRequestsByUser()
public PagedList<BookUser> GetRequestsByUser(int page, int itemsPerPage)
{
var userId = new Guid(Thread.CurrentPrincipal?.Identity?.Name);
return _bookUserRepository.Get(x => x.UserId == userId, x => x.Book, new IncludeList<BookUser>(b => b.Book));
var query = _bookUserRepository.Get()
.Include(x => x.Book)
.Where(x => x.UserId == userId)
.OrderByDescending(x => x.CreationDate);

return FormatPagedList(query, page, itemsPerPage);
}

public async Task NotifyInterestedAboutBooksWinner(Guid bookId)
Expand Down
3 changes: 2 additions & 1 deletion ShareBook/ShareBook.Service/BookUser/IBookUserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public interface IBookUserService

void DeniedBookUsers(Guid bookId);

PagedList<BookUser> GetRequestsByUser();
PagedList<BookUser> GetRequestsByUser(int page, int items);

/// <summary>
/// Comunicar os interessados não escolhidos sobre a finalização da doação. e quem ganhou o livro
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions ShareBook/ShareBook.Service/Generic/BaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using ShareBook.Repository.Repository;
using ShareBook.Repository.UoW;
using System;
using System.Linq;
using System.Linq.Expressions;

namespace ShareBook.Service.Generic
Expand Down Expand Up @@ -56,6 +57,20 @@ public virtual PagedList<TEntity> Get<TKey>(Expression<Func<TEntity, bool>> filt

public PagedList<TEntity> Get<TKey>(Expression<Func<TEntity, bool>> filter, Expression<Func<TEntity, TKey>> order, int page, int itemsPerPage, IncludeList<TEntity> includes)
=> _repository.Get(filter, order, page, itemsPerPage, includes);

public PagedList<TEntity> FormatPagedList(IQueryable<TEntity> query, int page, int itemsPerPage)
{
var total = query.Count();
var skip = (page - 1) * itemsPerPage;
return new PagedList<TEntity>()
{
Page = page,
ItemsPerPage = itemsPerPage,
TotalItems = total,
Items = query.Skip(skip).Take(itemsPerPage).ToList()
};
}

#endregion

protected Result<TEntity> Validate(TEntity entity) => new Result<TEntity>(_validator.Validate(entity));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Moq;
using ShareBook.Domain.Validators;
using ShareBook.Repository;
using ShareBook.Repository.UoW;
using ShareBook.Service;
Expand All @@ -18,6 +19,7 @@ public class BookUserServiceTests
readonly Mock<IBooksEmailService> bookEmailService;
readonly Mock<IUnitOfWork> unitOfWorkMock;
readonly Mock<IBookUsersEmailService> bookUsersEmailService;
readonly BookUserValidator bookUserValidator;


public BookUserServiceTests()
Expand All @@ -39,7 +41,8 @@ public void RequestBook()
{
Thread.CurrentPrincipal = new UserMock().GetClaimsUser();
var service = new BookUserService(bookUserRepositoryMock.Object,
bookServiceMock.Object, bookUsersEmailService.Object, unitOfWorkMock.Object);
bookServiceMock.Object, bookUsersEmailService.Object, unitOfWorkMock.Object, bookUserValidator);

string reason = "I need this book because I'm learning a new programming language.";

service.Insert(bookId, reason);
Expand Down

0 comments on commit 4e93a07

Please sign in to comment.