Skip to content

Commit

Permalink
Moved Get endpoints to dedicated class and used Wolverine for them
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Aug 2, 2023
1 parent 5674764 commit cc61669
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Helpdesk.Api.Incidents.GetCustomerIncidentsSummary;
using Helpdesk.Api.Incidents.GetIncidentDetails;
using Helpdesk.Api.Incidents.GetIncidentHistory;
using Helpdesk.Api.Incidents.GetIncidentShortInfo;
using Marten;
using Marten.AspNetCore;
using Marten.Pagination;
using Microsoft.AspNetCore.Mvc;
using Wolverine.Http;

namespace Helpdesk.Api.Incidents;

public class IncidentGetEndpoints
{
[WolverineGet("/api/customers/{customerId:guid}/incidents/")]
public Task<IPagedList<IncidentShortInfo>> GetCustomerIncidents
(IQuerySession querySession, Guid customerId, [FromQuery] int? pageNumber, [FromQuery] int? pageSize,
CancellationToken ct) =>
querySession.Query<IncidentShortInfo>().Where(i => i.CustomerId == customerId)
.ToPagedListAsync(pageNumber ?? 1, pageSize ?? 10, ct);

// That for some reason doesn't work for me
// [WolverineGet("/api/incidents/{incidentId:guid}")]
// public Task GetIncidentById([FromQuery] Guid incidentId, IQuerySession querySession, HttpContext context) =>
// querySession.Json.WriteById<IncidentDetails>(incidentId, context);

[WolverineGet("/api/incidents/{incidentId:guid}")]
public Task<IncidentDetails?> GetIncidentById([FromQuery] Guid incidentId, IQuerySession querySession,
CancellationToken ct) =>
querySession.LoadAsync<IncidentDetails>(incidentId, ct);

// That for some reason doesn't work for me
// [WolverineGet("/api/incidents/{incidentId:guid}/history")]
// public Task GetIncidentHistory([FromQuery]Guid incidentId, HttpContext context, IQuerySession querySession) =>
// querySession.Query<IncidentHistory>().Where(i => i.IncidentId == incidentId).WriteArray(context);

[WolverineGet("/api/incidents/{incidentId:guid}/history")]
public Task<IReadOnlyList<IncidentHistory>> GetIncidentHistory(
[FromQuery] Guid incidentId,
IQuerySession querySession,
CancellationToken ct
) =>
querySession.Query<IncidentHistory>().Where(i => i.IncidentId == incidentId).ToListAsync(ct);

// That for some reason doesn't work for me
// [WolverineGet("/api/customers/{customerId:guid}/incidents/incidents-summary")]
// public Task GetCustomerIncidentsSummary([FromQuery] Guid customerId, HttpContext context,
// IQuerySession querySession) =>
// querySession.Json.WriteById<CustomerIncidentsSummary>(customerId, context);

[WolverineGet("/api/customers/{customerId:guid}/incidents/incidents-summary")]
public Task GetCustomerIncidentsSummary(
[FromQuery] Guid customerId,
IQuerySession querySession,
CancellationToken ct
) =>
querySession.LoadAsync<CustomerIncidentsSummary>(customerId, ct);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
using Helpdesk.Api.Core.Http;
using Helpdesk.Api.Core.Marten;
using Helpdesk.Api.Incidents.GetCustomerIncidentsSummary;
using Helpdesk.Api.Incidents.GetIncidentDetails;
using Helpdesk.Api.Incidents.GetIncidentHistory;
using Helpdesk.Api.Incidents.GetIncidentShortInfo;
using JasperFx.Core;
using Marten;
using Marten.AspNetCore;
using Marten.Pagination;
using Microsoft.AspNetCore.Mvc;
using Wolverine.Http;
using static Microsoft.AspNetCore.Http.TypedResults;
using static Helpdesk.Api.Incidents.IncidentService;
Expand Down Expand Up @@ -174,31 +167,6 @@ CancellationToken ct

return Ok();
}

public static void MapIncidentsEndpoints(this WebApplication app)
{
app.MapGet("/api/customers/{customerId:guid}/incidents/",
(IQuerySession querySession, Guid customerId, [FromQuery] int? pageNumber, [FromQuery] int? pageSize,
CancellationToken ct) =>
querySession.Query<IncidentShortInfo>().Where(i => i.CustomerId == customerId)
.ToPagedListAsync(pageNumber ?? 1, pageSize ?? 10, ct)
);

app.MapGet("/api/incidents/{incidentId:guid}",
(HttpContext context, IQuerySession querySession, Guid incidentId) =>
querySession.Json.WriteById<IncidentDetails>(incidentId, context)
);

app.MapGet("/api/incidents/{incidentId:guid}/history",
(HttpContext context, IQuerySession querySession, Guid incidentId) =>
querySession.Query<IncidentHistory>().Where(i => i.IncidentId == incidentId).WriteArray(context)
);

app.MapGet("/api/customers/{customerId:guid}/incidents/incidents-summary",
(HttpContext context, IQuerySession querySession, Guid customerId) =>
querySession.Json.WriteById<CustomerIncidentsSummary>(customerId, context)
);
}
}

public record LogIncidentRequest(
Expand Down
2 changes: 0 additions & 2 deletions Sample/Helpdesk.Wolverine/Helpdesk.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@

var app = builder.Build();

app.MapIncidentsEndpoints();

if (app.Environment.IsDevelopment())
{
app.UseSwagger()
Expand Down

0 comments on commit cc61669

Please sign in to comment.