diff --git a/BlogPlatform/BlogPlatform/DTO/Post/Request/UpdatePostDto.cs b/BlogPlatform/BlogPlatform/DTO/Post/Request/UpdatePostDto.cs index 2d81326..8ccbf5c 100644 --- a/BlogPlatform/BlogPlatform/DTO/Post/Request/UpdatePostDto.cs +++ b/BlogPlatform/BlogPlatform/DTO/Post/Request/UpdatePostDto.cs @@ -7,7 +7,6 @@ public class UpdatePostDto public int Id { get; set; } public string? Title { get; set; } public string? Content { get; set; } - public int Likes { get; set; } public int UserId { get; set; } } \ No newline at end of file diff --git a/BlogPlatform/BlogPlatform/DTO/User/Response/GetUserDto.cs b/BlogPlatform/BlogPlatform/DTO/User/Response/GetUserDto.cs index 0a0e0b4..11ed2b8 100644 --- a/BlogPlatform/BlogPlatform/DTO/User/Response/GetUserDto.cs +++ b/BlogPlatform/BlogPlatform/DTO/User/Response/GetUserDto.cs @@ -1,3 +1,6 @@ +using BlogPlatform.DTO.Comment.Response; +using BlogPlatform.DTO.Post.Response; + namespace BlogPlatform.DTO.User.Response; public class GetUserDto @@ -7,4 +10,7 @@ public class GetUserDto public string? Email { get; set; } public string? Password { get; set; } public string? Salt { get; set; } + + public List Posts { get; set; } + public List Comments { get; set; } } \ No newline at end of file diff --git a/BlogPlatform/BlogPlatform/Endpoints/Comment/DeleteCommentEndpoint.cs b/BlogPlatform/BlogPlatform/Endpoints/Comment/DeleteCommentEndpoint.cs new file mode 100644 index 0000000..47bdfa7 --- /dev/null +++ b/BlogPlatform/BlogPlatform/Endpoints/Comment/DeleteCommentEndpoint.cs @@ -0,0 +1,35 @@ +using BlogPlatform.DTO.Comment.Response; +using FastEndpoints; + +namespace BlogPlatform.Endpoints.Comment; + +public class DeleteCommentRequest +{ + public int Id { get; set; } +} +public class DeleteCommentEndpoint(BlogPlatformDbContext db) : Endpoint +{ + public override void Configure() + { + Delete("/api/comments/{@id}", x=> new{id=x.Id}); + } + + public override async Task HandleAsync(DeleteCommentRequest req, CancellationToken ct) + { + Models.Comment? comment = await db.Comments.FindAsync(req.Id); + + + if (comment == null) + { + await Send.StringAsync("You must add a comment on a Post that exist and of a User that exists", 400); + return; + } + + db.Comments.Remove(comment); + + await db.SaveChangesAsync(ct); + + + await Send.OkAsync(ct); + } +} \ No newline at end of file diff --git a/BlogPlatform/BlogPlatform/Endpoints/Comment/GetAllCommentEndpoint.cs b/BlogPlatform/BlogPlatform/Endpoints/Comment/GetAllCommentEndpoint.cs index 10a1f9d..76ee575 100644 --- a/BlogPlatform/BlogPlatform/Endpoints/Comment/GetAllCommentEndpoint.cs +++ b/BlogPlatform/BlogPlatform/Endpoints/Comment/GetAllCommentEndpoint.cs @@ -8,7 +8,7 @@ public class GetAllCommentEndpoint(BlogPlatformDbContext db) : EndpointWithoutRe { public override void Configure() { - Get("/api/comments/"); + Get("/api/comments"); } public override async Task HandleAsync(CancellationToken ct) diff --git a/BlogPlatform/BlogPlatform/Endpoints/Comment/UpdateCommentEndpoint.cs b/BlogPlatform/BlogPlatform/Endpoints/Comment/UpdateCommentEndpoint.cs new file mode 100644 index 0000000..caf12aa --- /dev/null +++ b/BlogPlatform/BlogPlatform/Endpoints/Comment/UpdateCommentEndpoint.cs @@ -0,0 +1,50 @@ +using BlogPlatform.DTO.Comment.Request; +using BlogPlatform.DTO.Comment.Response; +using FastEndpoints; + +namespace BlogPlatform.Endpoints.Comment; + +public class UpdateCommentEndpoint(BlogPlatformDbContext db) : Endpoint +{ + public override void Configure() + { + Put("/api/comments/{@id}", x=> new{id=x.Id}); + } + + public override async Task HandleAsync(UpdateCommentDto req, CancellationToken ct) + { + Models.Comment? comment = await db.Comments.FindAsync(req.Id); + // Checking if the user and post IDs exist in our database + Models.User? checkingUser = await db.Users.FindAsync(req.UserId); + Models.Post? checkingPost = await db.Posts.FindAsync(req.PostId); + + if (checkingUser == null || checkingPost == null || comment == null) + { + await Send.StringAsync("You must add a comment on a Post that exist and of a User that exists", 400); + return; + } + + comment.Content = req.Content; + comment.PostId = checkingPost.Id; + comment.UserId = checkingUser.Id; + comment.User = checkingUser; + comment.Post = checkingPost; + + await db.SaveChangesAsync(ct); + + + // Returning the response with DTO + GetCommentDto dto = new() + { + Id = comment.Id, + Content = comment.Content, + PostId = comment.PostId, + PostTitle = checkingPost.Title, + UserId = comment.UserId, + UserUsername = checkingUser.Username + + }; + + await Send.OkAsync(dto, ct); + } +} \ No newline at end of file diff --git a/BlogPlatform/BlogPlatform/Endpoints/Post/UpdatePostEndpoint.cs b/BlogPlatform/BlogPlatform/Endpoints/Post/UpdatePostEndpoint.cs new file mode 100644 index 0000000..37488cf --- /dev/null +++ b/BlogPlatform/BlogPlatform/Endpoints/Post/UpdatePostEndpoint.cs @@ -0,0 +1,48 @@ +using BlogPlatform.DTO.Comment.Request; +using BlogPlatform.DTO.Comment.Response; +using BlogPlatform.DTO.Post.Request; +using BlogPlatform.DTO.Post.Response; +using FastEndpoints; + +namespace BlogPlatform.Endpoints.Post; + +public class UpdatePostEndpoint(BlogPlatformDbContext db) : Endpoint +{ + public override void Configure() + { + Put("/api/post/{@id}", x=> new{id=x.Id}); + } + + public override async Task HandleAsync(UpdatePostDto req, CancellationToken ct) + { + Models.Post post = await db.Posts.FindAsync(req.Id); + Models.User checkingUser = await db.Users.FindAsync(req.UserId); + + if (checkingUser == null || post == null) + { + await Send.StringAsync("You must add a comment on a Post that exist and of a User that exists", 400); + return; + } + + post.Content = req.Content; + post.Title = req.Title; + post.UserId = checkingUser.Id; + + await db.SaveChangesAsync(ct); + + + // Returning the response with DTO + GetPostDto dto = new() + { + Id = post.Id, + Title = req.Title, + Content = req.Content, + Likes = post.Likes, + UserId = req.UserId, + UserUsername = checkingUser.Username, + UserEmail = checkingUser.Email, + }; + + await Send.OkAsync(dto, ct); + } +} \ No newline at end of file diff --git a/BlogPlatform/BlogPlatform/Endpoints/User/GetAllUserEndpoint.cs b/BlogPlatform/BlogPlatform/Endpoints/User/GetAllUserEndpoint.cs new file mode 100644 index 0000000..6cc5554 --- /dev/null +++ b/BlogPlatform/BlogPlatform/Endpoints/User/GetAllUserEndpoint.cs @@ -0,0 +1,31 @@ +using BlogPlatform.DTO.Comment.Response; +using BlogPlatform.DTO.User.Response; +using FastEndpoints; +using Microsoft.EntityFrameworkCore; + +namespace BlogPlatform.Endpoints.User; + +public class GetAllUserEndpoint(BlogPlatformDbContext db) : EndpointWithoutRequest> +{ + public override void Configure() + { + Get("/api/users"); + } + + public override async Task HandleAsync(CancellationToken ct) + { + + List Response = await db.Users + .Select(user=>new GetUserDto() + { + Id = user.Id, + Username = user.Username, + Email = user.Email, + + }) + .ToListAsync(ct); + + await Send.OkAsync(Response, ct); + + } +} \ No newline at end of file diff --git a/BlogPlatform/BlogPlatform/Endpoints/User/GetUserEndpoint.cs b/BlogPlatform/BlogPlatform/Endpoints/User/GetUserEndpoint.cs new file mode 100644 index 0000000..5f4d36a --- /dev/null +++ b/BlogPlatform/BlogPlatform/Endpoints/User/GetUserEndpoint.cs @@ -0,0 +1,71 @@ +using BlogPlatform.DTO.Comment.Response; +using BlogPlatform.DTO.Post.Response; +using BlogPlatform.DTO.User.Response; +using BlogPlatform.Endpoints.Comment; +using FastEndpoints; +using Microsoft.EntityFrameworkCore; + +namespace BlogPlatform.Endpoints.User; + +public class GetUserRequest +{ + public int Id { get; set; } +} + +public class GetUserEndpoint(BlogPlatformDbContext db) : Endpoint +{ + public override void Configure() + { + Get("/api/users/{@id}", x => new{x.Id}); + } + + public override async Task HandleAsync(GetUserRequest req, CancellationToken ct) + { + Models.User? user = await db.Users.FindAsync(req.Id); + + if (user == null) + { + await Send.NotFoundAsync(ct); + return; + } + + List posts = await db.Posts + .Where(x => x.UserId == req.Id) + .Select(post=> new GetPostDto() + { + Id = post.Id, + Title = post.Title, + Content = post.Content, + Likes = post.Likes, + + }) + .ToListAsync(ct); + + List comments = await db.Comments + .Where(x => x.UserId == req.Id) + .Select(x=> new GetCommentDto() + { + Id = x.Id, + Content = x.Content, + PostId = x.PostId, + + }) + .ToListAsync(ct); + + + // Returning the response with DTO + GetUserDto dto = new() + { + Id = user.Id, + Username = user.Username, + Email = user.Email, + Password = user.Password, + Salt = user.Salt, + Comments = comments, + Posts = posts, + + }; + + await Send.OkAsync(dto, ct); + } +} \ No newline at end of file diff --git a/BlogPlatform/BlogPlatform/bin/Debug/net8.0/BlogPlatform.dll b/BlogPlatform/BlogPlatform/bin/Debug/net8.0/BlogPlatform.dll index e7cb7cd..b0fe83b 100644 Binary files a/BlogPlatform/BlogPlatform/bin/Debug/net8.0/BlogPlatform.dll and b/BlogPlatform/BlogPlatform/bin/Debug/net8.0/BlogPlatform.dll differ diff --git a/BlogPlatform/BlogPlatform/bin/Debug/net8.0/BlogPlatform.pdb b/BlogPlatform/BlogPlatform/bin/Debug/net8.0/BlogPlatform.pdb index 9d0ef38..add16ae 100644 Binary files a/BlogPlatform/BlogPlatform/bin/Debug/net8.0/BlogPlatform.pdb and b/BlogPlatform/BlogPlatform/bin/Debug/net8.0/BlogPlatform.pdb differ diff --git a/BlogPlatform/BlogPlatform/obj/Debug/net8.0/BlogPlatform.AssemblyInfo.cs b/BlogPlatform/BlogPlatform/obj/Debug/net8.0/BlogPlatform.AssemblyInfo.cs index 201bff5..01e12ef 100644 --- a/BlogPlatform/BlogPlatform/obj/Debug/net8.0/BlogPlatform.AssemblyInfo.cs +++ b/BlogPlatform/BlogPlatform/obj/Debug/net8.0/BlogPlatform.AssemblyInfo.cs @@ -13,7 +13,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("BlogPlatform")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+dde32c7ecddd8366f793b6c405117e6d98dd0ec2")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+0cd0f2621473abd0521a8a5f9a7a56184d71c3a9")] [assembly: System.Reflection.AssemblyProductAttribute("BlogPlatform")] [assembly: System.Reflection.AssemblyTitleAttribute("BlogPlatform")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/BlogPlatform/BlogPlatform/obj/Debug/net8.0/BlogPlatform.AssemblyInfoInputs.cache b/BlogPlatform/BlogPlatform/obj/Debug/net8.0/BlogPlatform.AssemblyInfoInputs.cache index 6e56f9c..2c91d32 100644 --- a/BlogPlatform/BlogPlatform/obj/Debug/net8.0/BlogPlatform.AssemblyInfoInputs.cache +++ b/BlogPlatform/BlogPlatform/obj/Debug/net8.0/BlogPlatform.AssemblyInfoInputs.cache @@ -1 +1 @@ -ff5956b39b948487fe2a2f58ad86b4e2b59aa2ab24f867972bab8f742900995e +0cb6db3941ed977bee3c221c7a3e30c6af161e8df3b0638cb67520d38f8244fe diff --git a/BlogPlatform/BlogPlatform/obj/Debug/net8.0/BlogPlatform.csproj.CoreCompileInputs.cache b/BlogPlatform/BlogPlatform/obj/Debug/net8.0/BlogPlatform.csproj.CoreCompileInputs.cache index 16b5961..f68bb45 100644 --- a/BlogPlatform/BlogPlatform/obj/Debug/net8.0/BlogPlatform.csproj.CoreCompileInputs.cache +++ b/BlogPlatform/BlogPlatform/obj/Debug/net8.0/BlogPlatform.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -937cb072cb736b42b0b4f1b7fc542cbc081668ae208dcc45a1470899c6803b7f +f46b64122534a536d218aa2aff7c4744f5f60b018646dfe5a831885f69ca9136 diff --git a/BlogPlatform/BlogPlatform/obj/Debug/net8.0/BlogPlatform.dll b/BlogPlatform/BlogPlatform/obj/Debug/net8.0/BlogPlatform.dll index e7cb7cd..b0fe83b 100644 Binary files a/BlogPlatform/BlogPlatform/obj/Debug/net8.0/BlogPlatform.dll and b/BlogPlatform/BlogPlatform/obj/Debug/net8.0/BlogPlatform.dll differ diff --git a/BlogPlatform/BlogPlatform/obj/Debug/net8.0/BlogPlatform.pdb b/BlogPlatform/BlogPlatform/obj/Debug/net8.0/BlogPlatform.pdb index 9d0ef38..add16ae 100644 Binary files a/BlogPlatform/BlogPlatform/obj/Debug/net8.0/BlogPlatform.pdb and b/BlogPlatform/BlogPlatform/obj/Debug/net8.0/BlogPlatform.pdb differ diff --git a/BlogPlatform/BlogPlatform/obj/Debug/net8.0/ref/BlogPlatform.dll b/BlogPlatform/BlogPlatform/obj/Debug/net8.0/ref/BlogPlatform.dll index 7887dc3..36a8ed2 100644 Binary files a/BlogPlatform/BlogPlatform/obj/Debug/net8.0/ref/BlogPlatform.dll and b/BlogPlatform/BlogPlatform/obj/Debug/net8.0/ref/BlogPlatform.dll differ diff --git a/BlogPlatform/BlogPlatform/obj/Debug/net8.0/refint/BlogPlatform.dll b/BlogPlatform/BlogPlatform/obj/Debug/net8.0/refint/BlogPlatform.dll index 7887dc3..36a8ed2 100644 Binary files a/BlogPlatform/BlogPlatform/obj/Debug/net8.0/refint/BlogPlatform.dll and b/BlogPlatform/BlogPlatform/obj/Debug/net8.0/refint/BlogPlatform.dll differ