From bc90e4eae58b4375992b95a18e24624ed0fb6f1a Mon Sep 17 00:00:00 2001 From: Jonathan Idland Olsnes <73334350+Jonathanio123@users.noreply.github.com> Date: Fri, 25 Oct 2024 14:37:31 +0200 Subject: [PATCH] Refactored endpoints --- .../Controllers/TaskOwnerReportsController.cs | 42 ++++++++++++++++--- .../Queries/GetWeeklyTaskOwnerReports.cs | 26 ++++++++++-- 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/Fusion.Summary.Api/Controllers/TaskOwnerReportsController.cs b/src/Fusion.Summary.Api/Controllers/TaskOwnerReportsController.cs index eac048f22..8373ca4a0 100644 --- a/src/Fusion.Summary.Api/Controllers/TaskOwnerReportsController.cs +++ b/src/Fusion.Summary.Api/Controllers/TaskOwnerReportsController.cs @@ -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>> 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.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>> GetWeeklyTaskOwnerReportsV1(Guid projectId, ODataQueryParams query) { @@ -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.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)] @@ -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)); } @@ -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. /// - [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)] diff --git a/src/Fusion.Summary.Api/Domain/Queries/GetWeeklyTaskOwnerReports.cs b/src/Fusion.Summary.Api/Domain/Queries/GetWeeklyTaskOwnerReports.cs index bb8a478f5..a2af9d3ac 100644 --- a/src/Fusion.Summary.Api/Domain/Queries/GetWeeklyTaskOwnerReports.cs +++ b/src/Fusion.Summary.Api/Domain/Queries/GetWeeklyTaskOwnerReports.cs @@ -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; @@ -9,14 +10,19 @@ namespace Fusion.Summary.Api.Domain.Queries; public class GetWeeklyTaskOwnerReports : IRequest> { - 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) @@ -37,15 +43,27 @@ public Handler(SummaryDbContext dbContext) public async Task> 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);