fix message pages

This commit is contained in:
cuqmbr 2023-10-15 16:26:00 +03:00
parent 98795719dc
commit 5abb142015
Signed by: cuqmbr
GPG Key ID: 2D72ED98B6CB200F
2 changed files with 8 additions and 28 deletions

View File

@ -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);
}

View File

@ -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]