Creation of all endpoint

This commit is contained in:
2025-10-17 10:45:56 +02:00
parent 5bf8970d36
commit f48710b793
9 changed files with 278 additions and 16 deletions

View File

@@ -22,8 +22,4 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2"/>
</ItemGroup>
<ItemGroup>
<Folder Include="Endpoints\Comment\" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,49 @@
using BlogPlatform.DTO.Comment.Request;
using BlogPlatform.DTO.Comment.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace BlogPlatform.Endpoints.Comment;
public class CreateCommentEndpoint(BlogPlatformDbContext database) : Endpoint<CreateCommentDto, GetCommentDto>
{
public override void Configure()
{
Post("/api/comments");
AllowAnonymous();
}
public override async Task HandleAsync(CreateCommentDto req, CancellationToken ct)
{
var checkUser = await database.Users.SingleOrDefaultAsync(x=> x.Id == req.UserId, ct);
var checkPost = await database.Posts.SingleOrDefaultAsync(x=> x.Id == req.PostId, ct);
if (checkUser == null || checkPost == null)
{
await Send.NoContentAsync(ct);
return;
}
var comment = new Models.Comment()
{
Content = req.Content,
CreatedAt = DateOnly.FromDateTime(DateTime.UtcNow),
PostId = req.PostId,
UserId = req.UserId
};
database.Comments.Add(comment);
await database.SaveChangesAsync(ct);
GetCommentDto responseDto = new()
{
Id = comment.Id,
Content = comment.Content,
CreatedAt = comment.CreatedAt,
PostId = comment.PostId,
UserId = comment.UserId
};
await Send.OkAsync(responseDto, ct);
}
}

View File

@@ -0,0 +1,31 @@
using BlogPlatform.DTO.Comment.Response;
using BlogPlatform.DTO.Post.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace BlogPlatform.Endpoints.Comment;
public class GetAllCommentsEndpoint(BlogPlatformDbContext database) : EndpointWithoutRequest<List<GetCommentDto>>
{
public override void Configure()
{
Get("/api/comments");
AllowAnonymous();
}
public override async Task HandleAsync(CancellationToken ct)
{
var comments = await database.Comments
.Select(c => new GetCommentDto()
{
Id = c.Id,
Content = c.Content,
CreatedAt = c.CreatedAt,
UserId = c.UserId,
PostId = c.Post.Id
})
.ToListAsync(ct);
await Send.OkAsync(comments, ct);
}
}

View File

@@ -0,0 +1,43 @@
using BlogPlatform.DTO.Comment.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace BlogPlatform.Endpoints.Comment;
public class GetCommentRequest
{
public int Id { get; set; }
}
public class GetCommentEndpoint(BlogPlatformDbContext database) : Endpoint<GetCommentRequest, GetCommentDto>
{
public override void Configure()
{
Get("/api/comments/{@Id}", x => new {x.Id});
AllowAnonymous();
}
public override async Task HandleAsync(GetCommentRequest req, CancellationToken ct)
{
var comment = await database.Comments.Include(c => c.Post)!
.Include(p => p.User)
.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
if (comment == null)
{
await Send.NotFoundAsync(ct);
return;
}
GetCommentDto responseDto = new()
{
Id = comment.Id,
Content = comment.Content,
CreatedAt = comment.CreatedAt,
UserId = comment.UserId,
PostId = comment.Post.Id
};
await Send.OkAsync(responseDto, ct);
}
}

View File

@@ -0,0 +1,50 @@
using BlogPlatform.DTO.Comment.Request;
using BlogPlatform.DTO.Comment.Response;
using BlogPlatform.DTO.Post.Request;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace BlogPlatform.Endpoints.Comment;
public class UpdateCommentEndpoint(BlogPlatformDbContext database) : Endpoint<UpdateCommentDto, GetCommentDto>
{
public override void Configure()
{
Put("/api/comments/{@Id}", x => new {x.Id});
AllowAnonymous();
}
public override async Task HandleAsync(UpdateCommentDto req, CancellationToken ct)
{
var checkUser = await database.Users.SingleOrDefaultAsync(x=> x.Id == req.UserId, ct);
var checkPost = await database.Posts.SingleOrDefaultAsync(x=> x.Id == req.PostId, ct);
if (checkUser == null || checkPost == null)
{
await Send.NoContentAsync(ct);
return;
}
var comment = new Models.Comment()
{
Content = req.Content,
CreatedAt = DateOnly.FromDateTime(DateTime.UtcNow),
PostId = req.PostId,
UserId = req.UserId
};
database.Comments.Add(comment);
await database.SaveChangesAsync(ct);
GetCommentDto responseDto = new()
{
Id = comment.Id,
Content = comment.Content,
CreatedAt = comment.CreatedAt,
PostId = comment.PostId,
UserId = comment.UserId
};
await Send.OkAsync(responseDto, ct);
}
}

View File

@@ -1,6 +1,7 @@
using BlogPlatform.DTO.Post.Request;
using BlogPlatform.DTO.Post.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace BlogPlatform.Endpoints.Post;
@@ -14,6 +15,14 @@ public class CreatePostEndpoint(BlogPlatformDbContext database) : Endpoint<Creat
public override async Task HandleAsync(CreatePostDto req, CancellationToken ct)
{
var checkUser = await database.Users.SingleOrDefaultAsync(x => x.Id == req.UserId, ct);
if (checkUser == null)
{
await Send.NoContentAsync(ct);
return;
}
var post = new Models.Post()
{
Title = req.Title,

View File

@@ -1,6 +1,42 @@
using BlogPlatform.DTO.Comment.Response;
using BlogPlatform.DTO.Post.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace BlogPlatform.Endpoints.Post;
public class GetAllPostsEndpoint
public class GetAllPostsEndpoint(BlogPlatformDbContext database) : EndpointWithoutRequest<List<GetPostDto>>
{
public override void Configure()
{
Get("/api/posts");
AllowAnonymous();
}
public override async Task HandleAsync(CancellationToken ct)
{
var posts = await database.Posts
.Include(p => p.Comments)!
.ThenInclude(c => c.User)
.Include(p => p.User)
.Select(post => new GetPostDto()
{
Id = post.Id,
Title = post.Title,
Content = post.Content,
Likes = post.Likes,
CreatedAt = post.CreatedAt,
UserId = post.UserId,
Comments = post.Comments.Select(c => new GetCommentDto()
{
Id = c.Id,
Content = c.Content,
CreatedAt = c.CreatedAt,
UserId = c.UserId
}).ToList()
})
.ToListAsync(ct);
await Send.OkAsync(posts, ct);
}
}

View File

@@ -1,6 +1,53 @@
using BlogPlatform.DTO.Comment.Response;
using BlogPlatform.DTO.Post.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace BlogPlatform.Endpoints.Post;
public class GetPostEndpoint
public class GetPostRequest
{
public int Id { get; set; }
}
public class GetPostEndpoint(BlogPlatformDbContext database) : Endpoint<GetPostRequest, GetPostDto>
{
public override void Configure()
{
Get("/api/posts/{@Id}", x => new {x.Id});
AllowAnonymous();
}
public override async Task HandleAsync(GetPostRequest req, CancellationToken ct)
{
var post = await database.Posts.Include(p => p.Comments)!
.ThenInclude(c => c.User)
.Include(p => p.User)
.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
if (post == null)
{
await Send.NotFoundAsync(ct);
return;
}
GetPostDto responseDto = new()
{
Id = post.Id,
Title = post.Title,
Content = post.Content,
Likes = post.Likes,
CreatedAt = post.CreatedAt,
UserId = post.UserId,
Comments = post.Comments.Select(c => new GetCommentDto()
{
Id = c.Id,
Content = c.Content,
CreatedAt = c.CreatedAt,
UserId = c.UserId
}).ToList()
};
await Send.OkAsync(responseDto, ct);
}
}

View File

@@ -1,3 +1,4 @@
using BlogPlatform.DTO.Comment.Response;
using BlogPlatform.DTO.Post.Request;
using BlogPlatform.DTO.Post.Response;
using FastEndpoints;
@@ -16,16 +17,9 @@ public class UpdatePostEndpoint(BlogPlatformDbContext database) : Endpoint<Updat
public override async Task HandleAsync(UpdatePostDto req, CancellationToken ct)
{
var post = await database.Posts.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
if (post == null)
{
await Send.NotFoundAsync(ct);
return;
}
var checkUser = await database.Users.SingleOrDefaultAsync(x => x.Id == req.UserId, ct);
if (checkUser == null)
if (checkUser == null || post == null)
{
await Send.NotFoundAsync(ct);
return;
@@ -45,7 +39,14 @@ public class UpdatePostEndpoint(BlogPlatformDbContext database) : Endpoint<Updat
Content = post.Content,
Likes = post.Likes,
CreatedAt = post.CreatedAt,
UserId = post.UserId
UserId = post.UserId,
Comments = post.Comments.Select(c => new GetCommentDto()
{
Id = c.Id,
Content = c.Content,
CreatedAt = c.CreatedAt,
UserId = c.UserId
}).ToList()
};
await Send.OkAsync(responseDto, ct);