Skip to content

Commit

Permalink
Refactored endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathanio123 committed Oct 25, 2024
1 parent 60278d8 commit bc90e4e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
42 changes: 37 additions & 5 deletions src/Fusion.Summary.Api/Controllers/TaskOwnerReportsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,40 @@ namespace Fusion.Summary.Api.Controllers;
[ApiVersion("1.0")]
public class TaskOwnerReportsController : BaseController
{
[HttpGet("task-owners-summary-reports/{projectId:guid}/weekly")]
[HttpGet("task-owners-summary-reports/weekly")]
[MapToApiVersion("1.0")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ODataFilter(nameof(ApiWeeklyTaskOwnerReport.PeriodStart), nameof(ApiWeeklyTaskOwnerReport.PeriodEnd))]
[ODataTop(100), ODataSkip]
public async Task<ActionResult<ApiCollection<ApiWeeklyTaskOwnerReport>>> GetWeeklyTaskOwnerReportsV1(ODataQueryParams query)
{
#region Authorization

var authResult =
await Request.RequireAuthorizationAsync(r =>
{
r.AlwaysAccessWhen().ResourcesFullControl();
r.AnyOf(or => { or.BeTrustedApplication(); });
});

if (authResult.Unauthorized)
return authResult.CreateForbiddenResponse();

#endregion

var reports = await DispatchAsync(new GetWeeklyTaskOwnerReports(query));

return Ok(ApiCollection<ApiWeeklyTaskOwnerReport>.FromQueryCollection(reports, ApiWeeklyTaskOwnerReport.FromQueryWeeklyTaskOwnerReport));
}

[HttpGet("projects/{projectId:guid}/task-owners-summary-reports/weekly")]
[MapToApiVersion("1.0")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ODataFilter(nameof(ApiWeeklyTaskOwnerReport.PeriodStart), nameof(ApiWeeklyTaskOwnerReport.PeriodEnd))]
[ODataTop(100), ODataSkip]
public async Task<ActionResult<ApiCollection<ApiWeeklyTaskOwnerReport>>> GetWeeklyTaskOwnerReportsV1(Guid projectId, ODataQueryParams query)
{
Expand All @@ -45,12 +74,12 @@ await Request.RequireAuthorizationAsync(r =>
if (project is null)
return ProjectNotFound(projectId);

var reports = await DispatchAsync(new GetWeeklyTaskOwnerReports(project.Id, query));
var reports = await DispatchAsync(new GetWeeklyTaskOwnerReports(query).WhereProjectId(project.Id));

return Ok(ApiCollection<ApiWeeklyTaskOwnerReport>.FromQueryCollection(reports, ApiWeeklyTaskOwnerReport.FromQueryWeeklyTaskOwnerReport));
}

[HttpGet("task-owners-summary-reports/{projectId:guid}/weekly/{reportId:guid}")]
[HttpGet("projects/{projectId:guid}/task-owners-summary-reports/weekly/{reportId:guid}")]
[MapToApiVersion("1.0")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
Expand All @@ -74,7 +103,10 @@ await Request.RequireAuthorizationAsync(r =>
if (project is null)
return ProjectNotFound(projectId);

var report = (await DispatchAsync(new GetWeeklyTaskOwnerReports(project.Id, new ODataQueryParams()).WhereReportId(reportId))).FirstOrDefault();
var report = (await DispatchAsync(new GetWeeklyTaskOwnerReports()
.WhereProjectId(project.Id)
.WhereReportId(reportId)))
.FirstOrDefault();

return report is null ? NotFound() : Ok(ApiWeeklyTaskOwnerReport.FromQueryWeeklyTaskOwnerReport(report));
}
Expand All @@ -83,7 +115,7 @@ await Request.RequireAuthorizationAsync(r =>
/// Summary report key is composed of the project id and the period start and end dates.
/// If a report already exists for the given project id and period then it will be replaced.
/// </summary>
[HttpPut("task-owners-summary-reports/{projectId:guid}/weekly")]
[HttpPut("projects/{projectId:guid}/task-owners-summary-reports/weekly")]
[MapToApiVersion("1.0")]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
Expand Down
26 changes: 22 additions & 4 deletions src/Fusion.Summary.Api/Domain/Queries/GetWeeklyTaskOwnerReports.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Fusion.AspNetCore.OData;
using Fusion.Summary.Api.Controllers.ApiModels;
using Fusion.Summary.Api.Database;
using Fusion.Summary.Api.Domain.Models;
using Fusion.Summary.Api.Domain.Queries.Base;
Expand All @@ -9,14 +10,19 @@ namespace Fusion.Summary.Api.Domain.Queries;

public class GetWeeklyTaskOwnerReports : IRequest<QueryCollection<QueryWeeklyTaskOwnerReport>>
{
public Guid ProjectId { get; }
public ODataQueryParams Query { get; private set; }
public Guid? ProjectId { get; private set; }
public Guid? ReportId { get; private set; }

public GetWeeklyTaskOwnerReports(Guid projectId, ODataQueryParams query)
public GetWeeklyTaskOwnerReports(ODataQueryParams? query = null)
{
Query = query ?? new ODataQueryParams();
}

public GetWeeklyTaskOwnerReports WhereProjectId(Guid projectId)
{
ProjectId = projectId;
Query = query;
return this;
}

public GetWeeklyTaskOwnerReports WhereReportId(Guid reportId)
Expand All @@ -37,15 +43,27 @@ public Handler(SummaryDbContext dbContext)
public async Task<QueryCollection<QueryWeeklyTaskOwnerReport>> Handle(GetWeeklyTaskOwnerReports request, CancellationToken cancellationToken)
{
var query = _dbContext.WeeklyTaskOwnerReports
.Where(r => r.ProjectId == request.ProjectId)
.AsQueryable();

if (request.ProjectId.HasValue)
query = query.Where(x => x.ProjectId == request.ProjectId);

if (request.ReportId.HasValue)
query = query.Where(x => x.Id == request.ReportId);

query = query.OrderByDescending(r => r.PeriodStart)
.ThenBy(r => r.Id);

if (request.Query.HasFilter)
{
query = query.ApplyODataFilters(request.Query,
m =>
{
m.MapField(nameof(ApiWeeklyTaskOwnerReport.PeriodStart), r => r.PeriodStart);
m.MapField(nameof(ApiWeeklyTaskOwnerReport.PeriodEnd), r => r.PeriodEnd);
});
}

var totalCount = await query.CountAsync(cancellationToken: cancellationToken);

var skip = request.Query.Skip.GetValueOrDefault(0);
Expand Down

0 comments on commit bc90e4e

Please sign in to comment.