Skip to content

Commit

Permalink
Merge pull request #332 from ITU-BDSA2024-GROUP10/update-the-page-but…
Browse files Browse the repository at this point in the history
…tons

Update the page buttons
  • Loading branch information
RasmusLarsen02 authored Nov 26, 2024
2 parents 953f6cc + ca58ce5 commit 31ad884
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 21 deletions.
12 changes: 12 additions & 0 deletions Chirp/src/Chirp.Core/CheepService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public interface ICheepService
public List<CheepDTO> GetCheepsFromAuthorByPage(string author, int page, int pageSize);
public List<CheepDTO> GetCheepsFromAuthorsByPage(IEnumerable<string> authors, int page, int pageSize);
public List<CheepDTO> GetCheepsFromAuthor(string author);
public int GetAmountOfCheepPages(int pageSize);
public int GetAmountOfCheepPagesFromAuthors(IEnumerable<String> authors, int pageSize);
public bool CreateCheep(CheepDTO cheep);
}

Expand Down Expand Up @@ -37,6 +39,16 @@ public List<CheepDTO> GetCheepsFromAuthorsByPage(IEnumerable<string> authors, in
return db.GetCheepsFromAuthorsByPage(authors, page, pageSize).Result.ToList();
}

public int GetAmountOfCheepPages(int pageSize)
{
return (int)Math.Ceiling(db.GetAmountOfCheeps().Result / (double)pageSize);
}

public int GetAmountOfCheepPagesFromAuthors(IEnumerable<String> authors, int pageSize)
{
return (int)Math.Ceiling(db.GetAmountOfCheepsFromAuthors(authors).Result / (double)pageSize);
}

public bool CreateCheep(CheepDTO cheep)
{
return db.CreateCheep(cheep).Result;
Expand Down
2 changes: 2 additions & 0 deletions Chirp/src/Chirp.Core/ICheepRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public interface ICheepRepository
public Task<IEnumerable<CheepDTO>> GetCheepsFromAuthor(String author);
public Task<IEnumerable<CheepDTO>> GetCheepsFromAuthorsByPage(IEnumerable<String> authors, int page, int pageSize);
public Task<bool> CreateCheep(CheepDTO cheep);
public Task<int> GetAmountOfCheeps();
public Task<int> GetAmountOfCheepsFromAuthors(IEnumerable<String> authors);
}
12 changes: 12 additions & 0 deletions Chirp/src/Chirp.Infrastructure/CheepRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ public async Task<IEnumerable<CheepDTO>> GetCheepsFromAuthorsByPage(IEnumerable<
new CheepDTO(cheep.UserName!, cheep.Message, new DateTimeOffset(cheep.TimeStamp).ToUnixTimeSeconds()));
}

public Task<int> GetAmountOfCheeps()
{
return context.Cheeps.CountAsync();
}

public Task<int> GetAmountOfCheepsFromAuthors(IEnumerable<String> authors)
{
authors = authors.Select(author => author.ToUpper());
return context.Cheeps
.CountAsync(cheep => authors.Contains(cheep.Author.NormalizedUserName!));
}

public async Task<bool> CreateCheep(CheepDTO cheep)
{
var author = await context.Authors
Expand Down
4 changes: 2 additions & 2 deletions Chirp/src/Chirp.Web/Pages/Public.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

@if (Model.Cheeps.Any())
{
@await Component.InvokeAsync("PageButtons", new { pageNumber = Model.PageNumber, targetPage = "/", isLastPage = Model.Cheeps.Count < 32 })
@await Component.InvokeAsync("PageButtons", new { currentPageNumber = Model.PageNumber, lastPageNumber = Model.LastPageNumber, targetPageUrl = $"/" })
@await Component.InvokeAsync("CheepList", new {cheeps = Model.Cheeps, targetPage = $"/"})
}
else
{
<em>There are no cheeps so far.</em>
}

@await Component.InvokeAsync("PageButtons", new { pageNumber = Model.PageNumber, targetPage = "/", isLastPage = Model.Cheeps.Count < 32 })
@await Component.InvokeAsync("PageButtons", new { currentPageNumber = Model.PageNumber, lastPageNumber = Model.LastPageNumber, targetPageUrl = $"/" })

</div>
3 changes: 2 additions & 1 deletion Chirp/src/Chirp.Web/Pages/Public.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public ActionResult OnGet([FromQuery] int page)

protected override void LoadCheeps(int page)
{
Cheeps = CheepService.GetCheepsByPage(page, 32);
LastPageNumber = CheepService.GetAmountOfCheepPages(PageSize);
Cheeps = CheepService.GetCheepsByPage(page, PageSize);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
@{
var targetPage = ViewBag.TargetPage as string ?? "/";
var pageNumber = ViewBag.PageNumber as int? ?? 1;
var isLastPage = ViewBag.IsLastPage as bool? ?? false;
var targetPageUrl = ViewBag.TargetPageUrl as string ?? "/";
var currentPageNumber = ViewBag.CurrentPageNumber as int? ?? 1;
var lastPageNumber = ViewBag.LastPageNumber as int? ?? 1;
}

<div style="display: flex; align-items: center;">
@if (pageNumber > 1)
<div style="display: flex; align-items: baseline;" class="page-buttons">
@if (currentPageNumber > 1)
{
<a href="@targetPage?page=@(pageNumber - 1)">Previous</a>
<a style="background-color: #cc6e6e; color: white; padding: 8px 16px; text-align: center; text-decoration: none; margin-right: 8px;" href="@targetPageUrl?page=@(currentPageNumber - 1)">&lt; Prev</a>
}
<p style="margin: 10px">@pageNumber</p>
@if (!isLastPage)
@if (currentPageNumber != 1)
{
<a href="@targetPage?page=@(pageNumber + 1)">Next</a>
<a style="margin: 5px" href="@targetPageUrl?page=1">1</a>
@if (currentPageNumber != 2)
{
<p style="margin: 5px">..</p>
<a style="margin: 5px" href="@targetPageUrl?page=@(currentPageNumber - 1)">@(currentPageNumber - 1)</a>
}
}
<p style="margin: 5px">@currentPageNumber</p>
@if (currentPageNumber != lastPageNumber)
{
@if (currentPageNumber != lastPageNumber-1)
{
<a style="margin: 5px" href="@targetPageUrl?page=@(currentPageNumber + 1)">@(currentPageNumber + 1)</a>
<p style="margin: 5px">..</p>
}
<a style="margin: 5px" href="@targetPageUrl?page=@lastPageNumber">@lastPageNumber</a>
}
@if (currentPageNumber < lastPageNumber)
{
<a style="background-color: #cc6e6e; color: white; padding: 8px 16px; text-align: center; text-decoration: none; margin-left: 8px;" href="@targetPageUrl?page=@(currentPageNumber + 1)">Next &gt;</a>
}
</div>
2 changes: 2 additions & 0 deletions Chirp/src/Chirp.Web/Pages/Shared/TimeLinePageModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public abstract class TimeLinePageModel(ICheepService cheepService) : PageModel
protected readonly ICheepService CheepService = cheepService;

public int PageNumber = 1;
public int LastPageNumber = 1;
protected const int PageSize = 32;

[BindProperty]
public MessageModel MessageModel { get; set; } = new MessageModel();
Expand Down
5 changes: 2 additions & 3 deletions Chirp/src/Chirp.Web/Pages/UserTimeline.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@

@if (Model.Cheeps.Any())
{
@await Component.InvokeAsync("PageButtons", new { pageNumber = Model.PageNumber, targetPage = $"/{routeName}", isLastPage = Model.Cheeps.Count < 32 })
@await Component.InvokeAsync("CheepList", new { cheeps = Model.Cheeps, targetPage = $"/{routeName}" })
@await Component.InvokeAsync("PageButtons", new { currentPageNumber = Model.PageNumber, lastPageNumber = Model.LastPageNumber, targetPageUrl = $"/{routeName}" })
}
else
{
<em>There are no cheeps so far.</em>
}

@await Component.InvokeAsync("PageButtons", new { pageNumber = Model.PageNumber, targetPage = $"/{routeName}", isLastPage = Model.Cheeps.Count < 32 })

@await Component.InvokeAsync("PageButtons", new { currentPageNumber = Model.PageNumber, lastPageNumber = Model.LastPageNumber, targetPageUrl = $"/{routeName}" })
</div>
6 changes: 4 additions & 2 deletions Chirp/src/Chirp.Web/Pages/UserTimeline.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ protected override void LoadCheeps(int page)
throw new ArgumentNullException(nameof(author), "Author cannot be null, failed to get the route value");
}

var authors = AuthorService.GetFollows(author).Select(a => a.Name)
var authors = AuthorService.GetFollows(author)
.Select(a => a.Name)
.Append(author);
Cheeps = CheepService.GetCheepsFromAuthorsByPage(authors, page, 32);
LastPageNumber = CheepService.GetAmountOfCheepPagesFromAuthors(authors, PageSize);
Cheeps = CheepService.GetCheepsFromAuthorsByPage(authors, page, PageSize);
}

public string NormalizeForDisplay(string author)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ namespace Chirp.Web.ViewComponents;

public class PageButtonsViewComponent : ViewComponent
{
public IViewComponentResult Invoke(int pageNumber, string targetPage, bool isLastPage)
public IViewComponentResult Invoke(int currentPageNumber, int lastPageNumber, string targetPageUrl)
{
ViewBag.TargetPage = targetPage;
ViewBag.PageNumber = pageNumber;
ViewBag.IsLastPage = isLastPage;
ViewBag.TargetPageUrl = targetPageUrl;
ViewBag.CurrentPageNumber = currentPageNumber;
ViewBag.LastPageNumber = lastPageNumber;
return View("Default");
}
}
55 changes: 55 additions & 0 deletions Chirp/test/PlaywrightTests/UITests/PageButtonsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Microsoft.Playwright;
using PlaywrightTests.Utils;
using PlaywrightTests.Utils.PageTests;

namespace PlaywrightTests.UITests;

public class PageButtonsTests : PageTestWithRazorPlaywrightWebApplicationFactory
{
[Test]
public async Task FormatingTest()
{
//arrange
var testAuthor = new TestAuthorBuilder(RazorFactory.GetUserManager())
.WithDefault()
.Create();
await GenerateCheeps(testAuthor.author, 32*5);

//first
await Page.GotoAsync("/?page=1");
await Expect(Page.Locator("body")).ToContainTextAsync("1 2 .. 5 Next >");

//second
await Page.GotoAsync("/?page=2");
await Expect(Page.Locator("body")).ToContainTextAsync("< Prev 1 2 3 .. 5 Next >");

//middle
await Page.GotoAsync("/?page=3");
await Expect(Page.Locator("body")).ToContainTextAsync("< Prev 1 .. 2 3 4 .. 5 Next >");

//second to last
await Page.GotoAsync("/?page=4");
await Expect(Page.Locator("body")).ToContainTextAsync("< Prev 1 .. 3 4 5 Next >");

//end
await Page.GotoAsync("/?page=5");
await Expect(Page.Locator("body")).ToContainTextAsync("< Prev 1 .. 4 5");
}

[Test]
public async Task PageNumbersAreClickable()
{
//arrange
var testAuthor = new TestAuthorBuilder(RazorFactory.GetUserManager())
.WithDefault()
.Create();
await GenerateCheeps(testAuthor.author, 32*5);

//first
await Page.GotoAsync("/?page=1");
await Page.GetByRole(AriaRole.Link, new() { Name = "2" }).First.ClickAsync();
Assert.That(Page.Url, Is.EqualTo($"{RazorBaseUrl}/?page=2"));
await Page.GetByRole(AriaRole.Link, new() { Name = "5" }).First.ClickAsync();
Assert.That(Page.Url, Is.EqualTo($"{RazorBaseUrl}/?page=5"));
}
}

0 comments on commit 31ad884

Please sign in to comment.