diff --git a/BlogPlatform/BlogPlatform.csproj b/BlogPlatform/BlogPlatform.csproj index eefd7b6..f3bf423 100644 --- a/BlogPlatform/BlogPlatform.csproj +++ b/BlogPlatform/BlogPlatform.csproj @@ -22,8 +22,4 @@ - - - - diff --git a/BlogPlatform/Endpoints/Comment/CreateCommentEndpoint.cs b/BlogPlatform/Endpoints/Comment/CreateCommentEndpoint.cs new file mode 100644 index 0000000..38e240a --- /dev/null +++ b/BlogPlatform/Endpoints/Comment/CreateCommentEndpoint.cs @@ -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 +{ + 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); + } +} \ No newline at end of file diff --git a/BlogPlatform/Endpoints/Comment/GetAllCommentsEndpoint.cs b/BlogPlatform/Endpoints/Comment/GetAllCommentsEndpoint.cs new file mode 100644 index 0000000..663554e --- /dev/null +++ b/BlogPlatform/Endpoints/Comment/GetAllCommentsEndpoint.cs @@ -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> +{ + 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); + } +} \ No newline at end of file diff --git a/BlogPlatform/Endpoints/Comment/GetCommentEndpoint.cs b/BlogPlatform/Endpoints/Comment/GetCommentEndpoint.cs new file mode 100644 index 0000000..b2535d8 --- /dev/null +++ b/BlogPlatform/Endpoints/Comment/GetCommentEndpoint.cs @@ -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 +{ + 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); + } +} \ No newline at end of file diff --git a/BlogPlatform/Endpoints/Comment/UpdateCommentEndpoint.cs b/BlogPlatform/Endpoints/Comment/UpdateCommentEndpoint.cs new file mode 100644 index 0000000..679b548 --- /dev/null +++ b/BlogPlatform/Endpoints/Comment/UpdateCommentEndpoint.cs @@ -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 +{ + 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); + } +} \ No newline at end of file diff --git a/BlogPlatform/Endpoints/Post/CreatePostEndpoint.cs b/BlogPlatform/Endpoints/Post/CreatePostEndpoint.cs index b64f71c..6b06d63 100644 --- a/BlogPlatform/Endpoints/Post/CreatePostEndpoint.cs +++ b/BlogPlatform/Endpoints/Post/CreatePostEndpoint.cs @@ -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 x.Id == req.UserId, ct); + + if (checkUser == null) + { + await Send.NoContentAsync(ct); + return; + } + var post = new Models.Post() { Title = req.Title, diff --git a/BlogPlatform/Endpoints/Post/GetAllPostsEndpoint.cs b/BlogPlatform/Endpoints/Post/GetAllPostsEndpoint.cs index 6f48d54..31ded5e 100644 --- a/BlogPlatform/Endpoints/Post/GetAllPostsEndpoint.cs +++ b/BlogPlatform/Endpoints/Post/GetAllPostsEndpoint.cs @@ -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> { + 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); + } } \ No newline at end of file diff --git a/BlogPlatform/Endpoints/Post/GetPostEndpoint.cs b/BlogPlatform/Endpoints/Post/GetPostEndpoint.cs index e514dbb..87d38b5 100644 --- a/BlogPlatform/Endpoints/Post/GetPostEndpoint.cs +++ b/BlogPlatform/Endpoints/Post/GetPostEndpoint.cs @@ -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 +{ + 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); + } } \ No newline at end of file diff --git a/BlogPlatform/Endpoints/Post/UpdatePostEndpoint.cs b/BlogPlatform/Endpoints/Post/UpdatePostEndpoint.cs index 45be386..dc9ac5d 100644 --- a/BlogPlatform/Endpoints/Post/UpdatePostEndpoint.cs +++ b/BlogPlatform/Endpoints/Post/UpdatePostEndpoint.cs @@ -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 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 new GetCommentDto() + { + Id = c.Id, + Content = c.Content, + CreatedAt = c.CreatedAt, + UserId = c.UserId + }).ToList() }; await Send.OkAsync(responseDto, ct);