Skip to content

Commit

Permalink
Refactored Prioritise, AssignAgent 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 969041c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Helpdesk.Api.Incidents.AssigningAgent;
using Helpdesk.Api.Tests.Incidents.Fixtures;
using Xunit;
using static Ogooreck.API.ApiSpecification;
Expand All @@ -15,7 +16,8 @@ await API
.When(
POST,
URI($"/api/agents/{agentId}/incidents/{API.Incident.Id}/assign"),
HEADERS(IF_MATCH(1))
HEADERS(IF_MATCH(1)),
BODY(new AssignAgentToIncidentRequest(API.Incident.Id))
)
.Then(OK)
.And()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ await API
.When(
POST,
URI($"/api/agents/{agentId}/incidents/{API.Incident.Id}/priority"),
BODY(new PrioritiseIncidentRequest(priority)),
BODY(new PrioritiseIncidentRequest(API.Incident.Id, priority)),
HEADERS(IF_MATCH(1))
)
.Then(OK);
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,35 +12,23 @@ namespace Helpdesk.Api.Incidents.AssigningAgent;

public static class AssignAgentEndpoint
{
[AggregateHandler]
[WolverinePost("/api/agents/{agentId:guid}/incidents/{incidentId:guid}/assign")]
public static async Task<IResult> AssignAgent
(
IDocumentSession documentSession,
Guid incidentId,
Guid agentId,
[FromIfMatchHeader] string eTag,
CancellationToken ct
)
public static (IResult, Events) AssignAgent(
AssignAgentToIncidentRequest toIncidentRequest,
Incident incident,
[FromRoute] Guid agentId,
[FromRoute] Guid incidentId,
//TODO: [FromIfMatchHeader] string eTag,
DateTimeOffset now)
{
await documentSession.GetAndUpdate<Incident>(incidentId, ToExpectedVersion(eTag),
state => Handle(state, new AssignAgentToIncident(incidentId, agentId, Now)), ct);

return Ok();
}

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

var (incidentId, agentId, now) = command;

return new AgentAssignedToIncident(incidentId, agentId, now);
return (Ok(), [new AgentAssignedToIncident(incidentId, agentId, now)]);
}
}

public record AssignAgentToIncident(
Guid IncidentId,
Guid AgentId,
DateTimeOffset Now
public record AssignAgentToIncidentRequest(
Guid IncidentId // TODO: meh
);
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@ 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,
//TODO: [FromIfMatchHeader] string eTag,
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)]);
}
}

public record CategoriseIncidentRequest(
Guid IncidentId,
Guid IncidentId, // TODO: meh
IncidentCategory Category
);
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,25 @@ 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,
//TODO: [FromIfMatchHeader] string eTag,
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(
Guid IncidentId, // TODO: meh
IncidentPriority Priority
);


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

0 comments on commit 969041c

Please sign in to comment.