Skip to content

Commit

Permalink
Update with correct versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnwildermuth committed Feb 8, 2024
1 parent b4521d4 commit 12f7f1d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
28 changes: 26 additions & 2 deletions src/Apis/CustomersApi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Asp.Versioning;
using Asp.Versioning.Builder;
using Mapster;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
Expand All @@ -23,10 +24,13 @@ public void Register(IEndpointRouteBuilder builder)
summary: "Customers",
description: "Customers that we can apply projects and tickets."));


group.MapGet("", GetAll)
.Produces<List<Customer>>(200, "application/json", "text/xml");
group.MapGet("{id:int}", GetOne).WithName("GetOneCustomer");
group.MapGet("{id:int}", GetOne)
.WithName("GetOneCustomer")
.MapToApiVersion(new ApiVersion(2.0));
group.MapGet("{id:int}", GetOne10)
.MapToApiVersion(new ApiVersion(1.0));
group.MapPost("", Post);
group.MapPut("{id:int}", Update);
group.MapDelete("{id:int}", Delete);
Expand Down Expand Up @@ -59,6 +63,26 @@ public static async Task<IResult> GetOne(BillingContext ctx, int id, bool includ
return Results.Ok(result);
}

// Get One
public static async Task<IResult> GetOne10(BillingContext ctx, int id)
{
var result = await ctx.Customers
.Where(c => c.Id == id)
.Select(c => new
{
c.Id,
c.CompanyName,
c.Contact,
c.PhoneNumber
})
.FirstOrDefaultAsync();

if (result is null) return Results.NotFound("No customer with that id Exists");

return Results.Ok(result);
}


// Create
public static async Task<IResult> Post(BillingContext ctx, Customer model)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Services/AcceptHeaderApiVersionReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace RestDesign.Services
public class AcceptHeaderApiVersionReader : IApiVersionReader
{
// looking for application/vnd.wilderminds.arest-v2+json
private const string Pattern = @".wilderminds.arest-v(\d+(\.\d+)?)\+\S+$";
private const string Pattern = @".design.rest-v(\d+(\.\d+)?)\+\S+$";

public void AddParameters(IApiVersionParameterDescriptionContext context)
{
Expand Down
14 changes: 4 additions & 10 deletions src/test.rest
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,14 @@ Accept: application/json

###

GET http://localhost:5000/api/customers
GET http://localhost:5000/api/customers?v=1.0
Accept: application/json

###

GET {{root}}/regions
Accept: application/json

###
GET http://localhost:5000/api/customers/9
Accept: application/vnd.design.rest-v2+json
#X-Version: 2.0

GET {{root}}/sites
Accept: application/json

###

GET {{root}}/sites/1457
Accept: application/json

0 comments on commit 12f7f1d

Please sign in to comment.