Skip to content

Commit

Permalink
Merge pull request #665 from bcgov/DIAM-209-net90
Browse files Browse the repository at this point in the history
web models
  • Loading branch information
leewrigh authored Oct 9, 2024
2 parents fe7f889 + 0fb0c09 commit 5d18595
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
18 changes: 7 additions & 11 deletions backend/ApprovalFlow/Features/Approvals/ApprovalsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,43 @@ namespace ApprovalFlow.Features.Approvals;

using Common.Constants.Auth;
using Common.Models.Approval;
using DomainResults.Common;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Prometheus;

[Route("api/[controller]")]
[ApiController]
public class ApprovalsController : ControllerBase
public class ApprovalsController(IMediator mediator) : ControllerBase
{


private readonly IMediator _mediator;
private static readonly Histogram ApprovalLookupDuration = Metrics.CreateHistogram("approval_lookup_duration", "Histogram of approval searches.");

public ApprovalsController(IMediator mediator) => this._mediator = mediator;

[HttpGet("pending")]
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[Authorize(Policy = Policies.ApprovalAuthorization)]

public async Task<ActionResult<IList<ApprovalModel>>> GetPendingApprovals([FromQuery] bool pendingOnly)
public async Task<ActionResult<IList<ApprovalModel>>> GetApprovals([FromQuery] bool pending)
{
using (ApprovalLookupDuration.NewTimer())
{
var response = await this._mediator.Send(new ApprovalsQuery(pendingOnly));
var response = await mediator.Send(new ApprovalsQuery(pending));
return this.Ok(response);
}
}


[HttpPost("response")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[Authorize(Policy = Policies.ApprovalAuthorization)]

public async Task<ActionResult<ApprovalModel>> PostApprovalResponse([FromBody] ApprovalResponseInput command)
{
var user = HttpContext.User.Identities.First().Claims.FirstOrDefault( claim => claim.Type.Equals(Claims.PreferredUsername))?.Value;
command.ApproverUserId = user;
var response = this._mediator.Send(command).Result;

var response = await mediator.Send(command);

return response;
}
Expand Down
6 changes: 3 additions & 3 deletions backend/ApprovalFlow/Features/Approvals/ApprovalsQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public PendingApprovalQueryHandler(ApprovalFlowDataStoreDbContext context)
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<ApprovalRequest, ApprovalModel>();
cfg.CreateMap<ApprovalHistory, ApprovalHistoryModel>();
cfg.CreateMap<ApprovalRequestReasons, ReasonModel>();
cfg.CreateMap<PersonalIdentity, PersonalIdentityModel>();
cfg.CreateMap<Request, RequestModel>();
Expand All @@ -36,15 +37,14 @@ public PendingApprovalQueryHandler(ApprovalFlowDataStoreDbContext context)
public async Task<IList<ApprovalModel>> Handle(ApprovalsQuery request, CancellationToken cancellationToken)
{
List<ApprovalRequest> results;

if (request.PendingOnly)
{
results = this.context.ApprovalRequests.AsSplitQuery().Include(req => req.Reasons).Include(req => req.PersonalIdentities).Include(req => req.Requests).ThenInclude(req => req.History).Where(req => req.Completed == null).ToList();

}
else
{
results = this.context.ApprovalRequests.AsSplitQuery().Include(req => req.Reasons).Include(req => req.PersonalIdentities).Include(req => req.Requests).ThenInclude(req => req.History).ToList();

}


Expand All @@ -57,7 +57,7 @@ public async Task<IList<ApprovalModel>> Handle(ApprovalsQuery request, Cancellat
}
else
{
return new List<ApprovalModel>();
return [];
}
}
}

0 comments on commit 5d18595

Please sign in to comment.