Skip to content

Commit d73f5d9

Browse files
stablization
1 parent 5874667 commit d73f5d9

File tree

7 files changed

+93
-35
lines changed

7 files changed

+93
-35
lines changed

src/Extensions/MapEndpoint.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using NetAPI.Common.Api;
2+
public static class MapEndpointExtention
3+
{
4+
5+
public static IEndpointRouteBuilder MapEndpoint<TEndpoint>(this IEndpointRouteBuilder app) where TEndpoint : IEndpoint
6+
{
7+
TEndpoint.Map(app);
8+
return app;
9+
}
10+
11+
12+
public static RouteGroupBuilder MapPublicGroup(this IEndpointRouteBuilder app, string? prefix = null)
13+
{
14+
return app.MapGroup(prefix ?? string.Empty)
15+
.AllowAnonymous();
16+
}
17+
18+
public static RouteGroupBuilder MapPrivateGroup(this IEndpointRouteBuilder app, string? prefix = null)
19+
{
20+
return app.MapGroup(prefix ?? string.Empty)
21+
.RequireAuthorization();
22+
}
23+
24+
25+
}

src/Extensions/WebAppBuilderExtension.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,35 @@ public static WebApplicationBuilder ConfigureApplicationBuilder(this WebApplicat
190190
if (IsDevelopment)
191191
{
192192
builder.Services.AddEndpointsApiExplorer();
193-
builder.Services.AddSwaggerGen();
193+
builder.Services.AddSwaggerGen(options =>
194+
{
195+
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Task Manager API", Version = "v1" });
196+
197+
// 🔹 Add JWT Security Definition
198+
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
199+
{
200+
Description = "Enter 'Bearer {token}'",
201+
Name = "Authorization",
202+
In = ParameterLocation.Header,
203+
Type = SecuritySchemeType.Http,
204+
Scheme = "Bearer"
205+
});
206+
207+
options.AddSecurityRequirement(new OpenApiSecurityRequirement
208+
{
209+
{
210+
new OpenApiSecurityScheme
211+
{
212+
Reference = new OpenApiReference
213+
{
214+
Type = ReferenceType.SecurityScheme,
215+
Id = "Bearer"
216+
}
217+
},
218+
new string[] { }
219+
}
220+
});
221+
});
194222
}
195223

196224
// Enable Compression to reduce payload size

src/Extensions/WebAppExtensions.cs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using NetAPI.Features.Posts;
66
using Microsoft.OpenApi.Models;
77
using NetAPI.Common.Api;
8+
using NetAPI.Features;
89

910
[ExcludeFromCodeCoverage]
1011
public static class WebAppExtensions
@@ -61,37 +62,19 @@ private static async Task EnsureDatabaseCreated(this WebApplication app)
6162

6263
private static void AddEndpoints(this WebApplication app)
6364
{
64-
app.MapGet("/", () => "Hello, World!");
65-
// app.MapGet("/health", () => "Healthy");
65+
app.MapGet("/", () => "DotNet API Boilerplate");
66+
app.MapGet("/health", () => "Healthy");
6667

6768
// app.MapGet("/secure", () => "You are authenticated!")
6869
// .RequireAuthorization(); // Protect this endpoint
6970

7071
// app.MapGet("/admin", () => "Welcome Admin!")
7172
// .RequireAuthorization(policy => policy.RequireRole("admin"));
7273

73-
app.MapPostEndpoints();
74+
app.MapFeatureEndpoints();
7475

7576
}
7677

77-
private static void MapPostEndpoints(this IEndpointRouteBuilder app)
78-
{
79-
var endpoint = app.MapPublicGroup("/tasks");
80-
endpoint.MapEndpoint<GetPosts>();
81-
}
82-
83-
private static RouteGroupBuilder MapPublicGroup(this IEndpointRouteBuilder app, string? prefix = null)
84-
{
85-
return app.MapGroup(prefix ?? string.Empty)
86-
.AllowAnonymous();
87-
}
88-
89-
private static RouteGroupBuilder MapPrivateGroup(this IEndpointRouteBuilder app, string? prefix = null)
90-
{
91-
return app.MapGroup(prefix ?? string.Empty)
92-
.RequireAuthorization();
93-
}
94-
9578
private static void AppendHeaders(this WebApplication app)
9679
{
9780
// Prevent Cross-Site Scripting (XSS) & Clickjacking

src/Features/Endpoints.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace NetAPI.Features;
2+
3+
using NetAPI.Features.Posts;
4+
5+
public static class FeaturesEndpointsExtension {
6+
public static void MapFeatureEndpoints(this WebApplication app) {
7+
var endpoint = app.MapPublicGroup("/tasks").WithTags("Posts");
8+
endpoint
9+
.MapEndpoint<GetPosts>()
10+
.MapEndpoint<CreatePost>();
11+
}
12+
}

src/Features/Posts/CreatePost.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace NetAPI.Features.Posts;
2+
3+
using Microsoft.AspNetCore.Http.HttpResults;
4+
using System.Security.Claims;
5+
using NetAPI.Common.Api;
6+
public class CreatePost : IEndpoint
7+
{
8+
public static void Map(IEndpointRouteBuilder app) => app
9+
.MapPost("/", Handle)
10+
.WithSummary("Creates a new post");
11+
12+
public record Request(string Title, string? Content);
13+
public record CreatedResponse(int Id);
14+
15+
private static async Task<Ok<CreatedResponse>> Handle(Request request, ClaimsPrincipal claimsPrincipal, CancellationToken cancellationToken)
16+
{
17+
var response = new CreatedResponse(2);
18+
return TypedResults.Ok(response);
19+
}
20+
}

src/Features/Posts/GetPosts.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
public class GetPosts: IEndpoint
66
{
77
public record Request(string Title, string? Content);
8-
public record Response(int Id);
8+
public record PostList(int Id);
99

1010
public static void Map(IEndpointRouteBuilder app) => app
1111
.MapGet("/", Handle)
1212
.WithSummary("Gets all posts");
1313

14-
private static Response Handle([AsParameters] Request request, CancellationToken cancellationToken)
14+
private static PostList Handle([AsParameters] Request request, CancellationToken cancellationToken)
1515
{
16-
return new Response(1);
16+
return new PostList(1);
1717
}
1818

1919
}

0 commit comments

Comments
 (0)