Skip to content

Commit

Permalink
Improve layout and move interaction to content store client
Browse files Browse the repository at this point in the history
* Use Dapr Client to invoke the content store
* Move logic for API integration to separate client class
* Move pending papers list to dedicated query
  • Loading branch information
wmeints committed Aug 1, 2024
1 parent aec7054 commit b0ed2dc
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 89 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using PaperBoy.ContentStore.Domain;
using PaperBoy.ContentStore.Shared;

namespace PaperBoy.ContentStore.Application.Projections;

public interface IPaperInfoRepository
{
Task<PaperInfo?> GetByIdAsync(Guid id);
Task<PagedResult<PaperInfo>> GetAllAsync(int page, int pageSize);
Task<PagedResult<PaperInfo>> GetByStatusAsync(string status, int page, int pageSize);
Task<PagedResult<PaperInfo>> GetPendingAsync(int page, int pageSize);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Marten;
using PaperBoy.ContentStore.Application.Projections;
using PaperBoy.ContentStore.Domain;
using PaperBoy.ContentStore.Shared;

namespace PaperBoy.ContentStore.Infrastructure;

Expand All @@ -9,22 +11,31 @@ public async Task<PagedResult<PaperInfo>> GetAllAsync(int page, int pageSize)
{
var session = store.LightweightSession();
var count = await session.Query<PaperInfo>().CountAsync();
var results = await session.Query<PaperInfo>().OrderBy(x => x.DateCreated).Skip(page * pageSize).Take(pageSize).ToListAsync();

var results = await session.Query<PaperInfo>()
.OrderBy(x => x.DateCreated)
.Skip(page * pageSize)
.Take(pageSize)
.ToListAsync();

return new PagedResult<PaperInfo>(results, page, pageSize, count);
}

public async Task<PagedResult<PaperInfo>> GetByStatusAsync(string status, int page, int pageSize)
public async Task<PagedResult<PaperInfo>> GetPendingAsync(int page, int pageSize)
{
var session = store.LightweightSession();
var count = await session.Query<PaperInfo>().CountAsync(x => x.Status.ToString() == status);
var results = await session.Query<PaperInfo>().Where(x => x.Status.ToString() == status).OrderBy(x => x.DateCreated).Skip(page * pageSize).Take(pageSize).ToListAsync();
var count = await session.Query<PaperInfo>().CountAsync();

return new PagedResult<PaperInfo>(results, page, pageSize, count);
}
var results = await session.Query<PaperInfo>()
.Where(x =>
x.Status == PaperStatus.Imported ||
x.Status == PaperStatus.Summarized ||
x.Status == PaperStatus.Scored)
.OrderBy(x => x.DateCreated)
.Skip(page * pageSize)
.Take(pageSize)
.ToListAsync();

public Task<PaperInfo?> GetByIdAsync(Guid id)
{
throw new NotImplementedException();
return new PagedResult<PaperInfo>(results, page, pageSize, count);
}
}
83 changes: 48 additions & 35 deletions apps/contentstore/PaperBoy.ContentStore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using PaperBoy.ContentStore.Domain.Commands;
using PaperBoy.ContentStore.Infrastructure;
using PaperBoy.ContentStore.Requests;
using PaperBoy.ContentStore.Responses;
using Weasel.Core;

var builder = WebApplication.CreateBuilder(args);
Expand Down Expand Up @@ -46,21 +45,22 @@
return Results.Accepted();
});

app.MapPut("/papers/{paperId}/summary", async (Guid paperId, SubmitPaperSummaryRequest request, SubmitSummaryCommandHandler commandHandler) =>
{
try
app.MapPut("/papers/{paperId}/summary",
async (Guid paperId, SubmitPaperSummaryRequest request, SubmitSummaryCommandHandler commandHandler) =>
{
var updateSummaryCommand = new SubmitSummaryCommand(paperId, request.Summary);
try
{
var updateSummaryCommand = new SubmitSummaryCommand(paperId, request.Summary);

await commandHandler.ExecuteAsync(updateSummaryCommand);
await commandHandler.ExecuteAsync(updateSummaryCommand);

return Results.Accepted();
}
catch (PaperNotFoundException)
{
return Results.NotFound();
}
});
return Results.Accepted();
}
catch (PaperNotFoundException)
{
return Results.NotFound();
}
});

app.MapPut("/papers/{paperId}/pages/{pageNumber}/summary", async (Guid paperId, int pageNumber,
SubmitPageSummaryRequest request, SubmitPageSummaryCommandHandler commandHandler) =>
Expand All @@ -79,23 +79,25 @@
}
});

app.MapPut("/papers/{paperId}/score", async (Guid paperId, SubmitPaperScoreRequest request, SubmitScoreCommandHandler commandHandler) =>
{
try
app.MapPut("/papers/{paperId}/score",
async (Guid paperId, SubmitPaperScoreRequest request, SubmitScoreCommandHandler commandHandler) =>
{
var submitScoreCommand = new SubmitScoreCommand(paperId, request.Score, request.Explanation);

await commandHandler.ExecuteAsync(submitScoreCommand);

return Results.Accepted();
}
catch (PaperNotFoundException)
{
return Results.NotFound();
}
});

app.MapPut("/papers/{paperId}/description", async (Guid paperId, SubmitPaperDescriptionRequest request, SubmitDescriptionCommandHandler commandHandler) =>
try
{
var submitScoreCommand = new SubmitScoreCommand(paperId, request.Score, request.Explanation);

await commandHandler.ExecuteAsync(submitScoreCommand);

return Results.Accepted();
}
catch (PaperNotFoundException)
{
return Results.NotFound();
}
});

app.MapPut("/papers/{paperId}/description", async (Guid paperId, SubmitPaperDescriptionRequest request,
SubmitDescriptionCommandHandler commandHandler) =>
{
try
{
Expand Down Expand Up @@ -161,12 +163,23 @@
return Results.Ok(paper);
});

app.MapGet("/papers", async (IPaperInfoRepository paperInfoRepository, [FromQuery] int page = 0) =>
{
var papers = await paperInfoRepository.GetAllAsync(page, 20);
return Results.Ok(papers);
});
app.MapGet("/papers/all",
async (IPaperInfoRepository paperInfoRepository, [FromQuery] int page = 0) =>
{
var papers = await paperInfoRepository.GetAllAsync(page, 20);

return Results.Ok(papers);
});

app.MapGet("/papers/pending",
async (IPaperInfoRepository paperInfoRepository, [FromQuery] int page = 0) =>
{
var papers = await paperInfoRepository.GetPendingAsync(page, 20);

return Results.Ok(papers);
});


app.MapDefaultEndpoints();

app.Run();
app.Run();
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace PaperBoy.ContentStore.Application.Projections;
namespace PaperBoy.ContentStore.Shared;

public record PagedResult<T>(IEnumerable<T> Items, int Page, int PageSize, int TotalCount);
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Dapr.Client;

namespace PaperBoy.Dashboard.Clients.ContentStore;

public interface IContentStoreClient
{
Task<PagedResult<PaperInfo>> GetPendingPapersAsync(PaperStatus[] statuses, int pageIndex);
}

public class ContentStoreClient(DaprClient daprClient) : IContentStoreClient
{
public async Task<PagedResult<PaperInfo>> GetPendingPapersAsync(PaperStatus[] statuses, int pageIndex)
{
var statusFilter = string.Join(",", statuses.Select(x => x.ToString()));

return await daprClient.InvokeMethodAsync<PagedResult<PaperInfo>>(HttpMethod.Get, "contentstore",
"/papers/pending");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace PaperBoy.Dashboard.Clients.ContentStore;

public record PagedResult<T>(IEnumerable<T> Items, int Page, int PageSize, int TotalCount)
{
public bool HasPreviousPage => Page > 0;
public bool HasNextPage => (Page + 1) * PageSize < TotalCount;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace PaperBoy.Dashboard.Models
namespace PaperBoy.Dashboard.Clients.ContentStore
{
public class PaperInfo
{
Expand Down
86 changes: 49 additions & 37 deletions apps/dashboard/PaperBoy.Dashboard/Components/Pages/Home.razor
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
@page "/"
@using PaperBoy.Dashboard.Clients.ContentStore
@attribute [Authorize]
@inject HttpClient Http
@using PaperBoy.ContentStore.Application.Projections
@using PaperBoy.ContentStore.Domain
@inject IContentStoreClient ContentStore

<h1>Submitted Papers</h1>
<div class="container mx-auto">
<h1 class="text-4xl font-bold mb-4">Pending papers</h1>

<button class="btn btn-primary" @onclick="SubmitNewPaper">Submit New Paper</button>
<div class="mb-4">
<button class="btn btn-primary" @onclick="SubmitNewPaper">Submit New Paper</button>
</div>

@if (papers == null)
{
<p>Loading...</p>
}
else
{
<table class="table w-full">
<thead>
@if (_papers == null)
{
<p>Loading...</p>
}
else
{
<table class="table w-full">
<thead>
<tr>
<th>Title</th>
<th>Submitter</th>
Expand All @@ -25,39 +27,46 @@ else
<th>Total Sections</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var paper in papers.Items)
</thead>
<tbody>
@foreach (var paper in _papers.Items)
{
<tr>
<td>@paper.Title</td>
<td>@paper.Submitter.Name</td>
<td><a href="@paper.Url" target="_blank">@paper.Url</a></td>
<td><span class="badge">@paper.Status</span></td>
<td>
<a class="underline hover:no-underline text-blue-600" href="mailto:@paper.Submitter?.EmailAddress">@paper.Submitter?.Name</a>
</td>
<td>
<a class="underline hover:no-underline text-blue-600" href="@paper.Url" target="_blank">@paper.Url</a>
</td>
<td>
<span class="badge">@paper.Status</span>
</td>
<td>@paper.SectionsSummarized</td>
<td>@paper.TotalSections</td>
<td>
@if (paper.Status == PaperStatus.Scored)
{
<button class="btn btn-success" @onclick="() => ApprovePaper(paper.Id)">Approve</button>
<button class="btn btn-danger" @onclick="() => DeclinePaper(paper.Id)">Decline</button>
<button class="btn btn-ghost btn-sm" @onclick="() => ApprovePaper(paper.Id)">Approve</button>
<button class="btn btn-ghost btn-sm" @onclick="() => DeclinePaper(paper.Id)">Decline</button>
}
</td>
</tr>
}
</tbody>
</table>
</tbody>
</table>

<div class="flex justify-center mt-4">
<button class="btn" @onclick="PreviousPage" disabled="@(!papers.HasPreviousPage)">Previous</button>
<button class="btn" @onclick="NextPage" disabled="@(!papers.HasNextPage)">Next</button>
</div>
}
<div class="flex justify-center mt-4 space-x-1">
<button class="btn btn-ghost" @onclick="PreviousPage" disabled="@(!_papers.HasPreviousPage)">Previous</button>
<button class="btn btn-ghost" @onclick="NextPage" disabled="@(!_papers.HasNextPage)">Next</button>
</div>
}
</div>

@code {
private PagedResult<PaperInfo>? papers;
private int currentPage = 1;
private const int pageSize = 20;
private PagedResult<PaperInfo>? _papers;
private int _currentPage = 1;
private const int PageSize = 20;

protected override async Task OnInitializedAsync()
{
Expand All @@ -66,8 +75,10 @@ else

private async Task LoadPapers()
{
var response = await Http.GetFromJsonAsync<PagedResult<PaperInfo>>($"/papers?page={currentPage}&pageSize={pageSize}&status=imported,summarized,scored,approved");
papers = response;
PaperStatus[] paperStatusArray = [PaperStatus.Imported, PaperStatus.Summarized, PaperStatus.Scored];
var response = await ContentStore.GetPendingPapersAsync(paperStatusArray, _currentPage);

_papers = response;
}

private async Task SubmitNewPaper()
Expand All @@ -87,19 +98,20 @@ else

private async Task PreviousPage()
{
if (papers?.HasPreviousPage == true)
if (_papers?.HasPreviousPage == true)
{
currentPage--;
_currentPage--;
await LoadPapers();
}
}

private async Task NextPage()
{
if (papers?.HasNextPage == true)
if (_papers?.HasNextPage == true)
{
currentPage++;
_currentPage++;
await LoadPapers();
}
}

}
5 changes: 5 additions & 0 deletions apps/dashboard/PaperBoy.Dashboard/PaperBoy.Dashboard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
</ItemGroup>

<ItemGroup>
<Folder Include="Models\" />
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\shared\Paperboy.ServiceDefaults\Paperboy.ServiceDefaults.csproj" />
</ItemGroup>
</Project>
Loading

0 comments on commit b0ed2dc

Please sign in to comment.