Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web models #665

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 [];
}
}
}