Skip to content

Commit

Permalink
Refactored Prioritise to AggregateHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Feb 5, 2024
1 parent f6bcdd4 commit bbdde9a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@ public static class CategoriseEndpoint
{
[AggregateHandler]
[WolverinePost("api/agents/{agentId:guid}/incidents/{incidentId:guid}/category")]
public static (IResult, Events) Post(
public static (IResult, Events) Categorise(
CategoriseIncidentRequest request,
Incident incident,
[FromRoute] Guid agentId,
[FromRoute] Guid incidentId,
DateTimeOffset now)
{
if (incident.Status == IncidentStatus.Closed)
{
throw new InvalidOperationException("Incident is already closed");
}

return (Ok(), new Events { new IncidentCategorised(incidentId, request.Category, agentId, now) });
return (Ok(), [new IncidentCategorised(incidentId, request.Category, agentId, now)]);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Helpdesk.Api.Core.Http;
using Helpdesk.Api.Core.Marten;
using Marten;
using Microsoft.AspNetCore.Mvc;
using Wolverine.Http;
using Wolverine.Marten;
using static Microsoft.AspNetCore.Http.TypedResults;
using static Helpdesk.Api.Core.Http.ETagExtensions;
using static System.DateTimeOffset;
Expand All @@ -10,42 +12,23 @@ namespace Helpdesk.Api.Incidents.Prioritising;

public static class PrioritiseEndpoint
{
[AggregateHandler]
[WolverinePost("/api/agents/{agentId:guid}/incidents/{incidentId:guid}/priority")]
public static async Task<IResult> Prioritise
(
IDocumentSession documentSession,
Guid incidentId,
Guid agentId,
[FromIfMatchHeader] string eTag,
PrioritiseIncidentRequest body,
CancellationToken ct
public static (IResult, Events) Prioritise(
PrioritiseIncidentRequest request,
Incident incident,
[FromRoute] Guid agentId,
[FromRoute] Guid incidentId,
DateTimeOffset now
)
{
await documentSession.GetAndUpdate<Incident>(incidentId, ToExpectedVersion(eTag),
state => Handle(state, new PrioritiseIncident(incidentId, body.Priority, agentId, Now)), ct);

return Ok();
}

public static IncidentPrioritised Handle(Incident current, PrioritiseIncident command)
{
if (current.Status == IncidentStatus.Closed)
if (incident.Status == IncidentStatus.Closed)
throw new InvalidOperationException("Incident is already closed");

var (incidentId, incidentPriority, prioritisedBy, now) = command;

return new IncidentPrioritised(incidentId, incidentPriority, prioritisedBy, now);
return (Ok(), [new IncidentPrioritised(incidentId, request.Priority, agentId, now)]);
}
}

public record PrioritiseIncidentRequest(
IncidentPriority Priority
);


public record PrioritiseIncident(
Guid IncidentId,
IncidentPriority Priority,
Guid PrioritisedBy,
DateTimeOffset Now
);

0 comments on commit bbdde9a

Please sign in to comment.