mirror of
https://github.com/Shchoholiev/shopping-assistant-api.git
synced 2025-04-04 16:49:36 +00:00
fix message pages
This commit is contained in:
parent
98795719dc
commit
5abb142015
@ -12,29 +12,9 @@ public class MessagesRepository : BaseRepository<Message>, IMessagesRepository
|
||||
|
||||
public async Task<List<Message>> GetPageStartingFromEndAsync(int pageNumber, int pageSize, Expression<Func<Message, bool>> predicate, CancellationToken cancellationToken)
|
||||
{
|
||||
var messageCount = await GetCountAsync(predicate, cancellationToken);
|
||||
|
||||
pageSize = Math.Clamp(pageSize, 1, messageCount);
|
||||
var numberOfPages = messageCount / pageSize;
|
||||
|
||||
if (pageNumber > numberOfPages)
|
||||
{
|
||||
return new List<Message>();
|
||||
}
|
||||
|
||||
if (pageNumber < 1)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
if (pageSize < 1)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
return await _collection.Find(predicate)
|
||||
.SortBy(x => x.CreatedDateUtc)
|
||||
.Skip((numberOfPages - pageNumber) * pageSize)
|
||||
.SortByDescending(x => x.CreatedDateUtc)
|
||||
.Skip((pageNumber - 1) * pageSize)
|
||||
.Limit(pageSize)
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
@ -212,13 +212,14 @@ public class WishlistsTests : IClassFixture<TestingFactory<Program>>
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
var document = JsonConvert.DeserializeObject<dynamic>(responseString);
|
||||
|
||||
Console.WriteLine(document);
|
||||
|
||||
var messagesPageFromPersonalWishlist = Enumerable.ToList(document.data.messagesPageFromPersonalWishlist.items);
|
||||
var firstMessageInPage = messagesPageFromPersonalWishlist[0];
|
||||
var secondMessageInPage = messagesPageFromPersonalWishlist[1];
|
||||
|
||||
Assert.Equal("Message 5", (string) firstMessageInPage.text);
|
||||
Assert.Equal(MessageRoles.User.ToString(), (string) firstMessageInPage.role);
|
||||
Assert.Equal(user.Id, (string) firstMessageInPage.createdById);
|
||||
Assert.Equal("Message 6", (string) firstMessageInPage.text);
|
||||
Assert.Equal(MessageRoles.Application.ToString(), (string) firstMessageInPage.role);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -606,9 +607,8 @@ public class WishlistsTests : IClassFixture<TestingFactory<Program>>
|
||||
var firstMessageInPage = messagesPageFromPersonalWishlist[0];
|
||||
var secondMessageInPage = messagesPageFromPersonalWishlist[1];
|
||||
|
||||
Assert.Equal("Message 1", (string) firstMessageInPage.text);
|
||||
Assert.Equal(MessageRoles.User.ToString(), (string) firstMessageInPage.role);
|
||||
Assert.Equal(user.Id, (string) firstMessageInPage.createdById);
|
||||
Assert.Equal("Message 6", (string) firstMessageInPage.text);
|
||||
Assert.Equal(MessageRoles.Application.ToString(), (string) firstMessageInPage.role);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
Loading…
Reference in New Issue
Block a user