Adding last endpoint

This commit is contained in:
Cristiano
2025-10-17 11:44:43 +02:00
parent 0cd0f26214
commit 931837fb0e
17 changed files with 245 additions and 5 deletions

View File

@@ -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; }
}

View File

@@ -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<GetPostDto> Posts { get; set; }
public List<GetCommentDto> Comments { get; set; }
}

View File

@@ -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<DeleteCommentRequest>
{
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);
}
}

View File

@@ -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)

View File

@@ -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<UpdateCommentDto,GetCommentDto>
{
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);
}
}

View File

@@ -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<UpdatePostDto,GetPostDto>
{
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);
}
}

View File

@@ -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<List<GetUserDto>>
{
public override void Configure()
{
Get("/api/users");
}
public override async Task HandleAsync(CancellationToken ct)
{
List<GetUserDto> Response = await db.Users
.Select(user=>new GetUserDto()
{
Id = user.Id,
Username = user.Username,
Email = user.Email,
})
.ToListAsync(ct);
await Send.OkAsync(Response, ct);
}
}

View File

@@ -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<GetUserRequest,GetUserDto>
{
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<GetPostDto> 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<GetCommentDto> 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);
}
}

View File

@@ -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")]

View File

@@ -1 +1 @@
ff5956b39b948487fe2a2f58ad86b4e2b59aa2ab24f867972bab8f742900995e
0cb6db3941ed977bee3c221c7a3e30c6af161e8df3b0638cb67520d38f8244fe

View File

@@ -1 +1 @@
937cb072cb736b42b0b4f1b7fc542cbc081668ae208dcc45a1470899c6803b7f
f46b64122534a536d218aa2aff7c4744f5f60b018646dfe5a831885f69ca9136