Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user image and username to PostDto #8

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using FluentValidation;
using Microsoft.EntityFrameworkCore;
using PostService.Application.DTOs;
using PostService.Application.Validators;
using PostService.Domain.Constants;
using PostService.Domain.Entities;
using PostService.Domain.Enums;
using PostService.Persistence;
Expand Down Expand Up @@ -30,6 +32,13 @@ public async Task<IResult<PostDto, Error>> HandleAsync(AddPostCommand command)
return Result<PostDto>.Failure(new Error(errorMessage));
}

var user = await _context.Users.FirstOrDefaultAsync(u => u.Id == command.UserId);

if (user == null)
{
return Result<PostDto>.Failure(new Error(ResponseMessages.UserNotFound));
}

var post = CreatePostForSpecifiedType(command);

_context.Posts.Add(post);
Expand All @@ -43,7 +52,9 @@ public async Task<IResult<PostDto, Error>> HandleAsync(AddPostCommand command)
post.ContentType,
post.LikeCount,
post.CommentCount,
post.CreatedAt);
post.CreatedAt,
user.ImageUrl,
user.Username);

return Result<PostDto>.Success(postDto);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public async Task<IResult<PostDto, Error>> HandleAsync(UpdatePostCommand command
{
return Result<PostDto>.Failure(new Error(ResponseMessages.YouDoNotHavePermissionToUpdateThisPost));
}

var user = await _context.Users.FirstAsync(u => u.Id == post.UserId);

post.Update(command.UpdatePost.Title, command.UpdatePost.Description);
await _context.SaveChangesAsync();
Expand All @@ -41,7 +43,9 @@ public async Task<IResult<PostDto, Error>> HandleAsync(UpdatePostCommand command
post.ContentType,
post.LikeCount,
post.CommentCount,
post.CreatedAt
post.CreatedAt,
user.ImageUrl,
user.Username
);

return Result<PostDto>.Success(postDto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public record PostDto(
ContentType ContentType,
uint LikeCount,
uint CommentCount,
DateTime CreatedAt);
DateTime CreatedAt,
string UserImageUrl,
string Username);
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using PostService.Application.DTOs;
using PostService.Domain.Constants;
using PostService.Domain.Entities;
using PostService.Persistence;
Expand All @@ -7,7 +8,7 @@

namespace PostService.Application.Queries.GetPost;

public class GetPostQueryHandler : IQueryHandler<GetPostQuery, Post>
public class GetPostQueryHandler : IQueryHandler<GetPostQuery, PostDto>
{
private readonly PostDbContext _context;

Expand All @@ -16,15 +17,30 @@ public GetPostQueryHandler(PostDbContext context)
_context = context;
}

public async Task<IResult<Post, Error>> HandleAsync(GetPostQuery query)
public async Task<IResult<PostDto, Error>> HandleAsync(GetPostQuery query)
{
var post = await _context.Posts.FirstOrDefaultAsync(p => p.Id == query.Id);

if (post == null)
{
return Result<Post>.Failure(new Error(ResponseMessages.PostNotFound));
return Result<PostDto>.Failure(new Error(ResponseMessages.PostNotFound));
}

return Result<Post>.Success(post);
var user = await _context.Users.FirstAsync(u => u.Id == post.UserId);

var postDto = new PostDto(
post.Id,
post.Title,
post.Description,
post.ContentUrl,
post.ContentType,
post.LikeCount,
post.CommentCount,
post.CreatedAt,
user.ImageUrl,
user.Username
);

return Result<PostDto>.Success(postDto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static class ResponseMessages
public const string PostNotFound = "Post not found.";
public const string CommentNotFound = "Comment not found.";
public const string LikeNotFound = "Like not found.";
public const string UserNotFound = "User not found";
public const string UserHasNotLikedThisPostYet = "User has not liked this post yet.";
public const string UserHasAlreadyLikedThisPost = "User has already liked this post.";
public const string YouDoNotHavePermissionToUpdateThisPost = "You do not have permission to update this post.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public async Task HandleAsync_ShouldCommentPost_WhenDataIsValid()
var contentType = ContentType.Image;

var createPost = new CreatePostDto(title, description, contentUrl, contentType);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

var commandPost = new AddPostCommand(createPost, userId);

Expand Down Expand Up @@ -54,7 +54,7 @@ public async Task HandleAsync_ShouldFail_WhenPostDoesNotExist()
{
// Arrange
var postId = Guid.Empty;
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;
var content = "This is a comment";
var createComment = new CreateCommentDto(postId, content);

Expand All @@ -79,7 +79,7 @@ public async Task HandleAsync_ShouldFail_WhenCommentTooLong()
var contentType = ContentType.Image;

var createPost = new CreatePostDto(title, description, contentUrl, contentType);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

var commandPost = new AddPostCommand(createPost, userId);

Expand Down Expand Up @@ -112,7 +112,7 @@ public async Task HandleAsync_ShouldAllowMultipleCommentsFromUser()
var contentType = ContentType.Image;

var createPost = new CreatePostDto(title, description, contentUrl, contentType);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

var commandPost = new AddPostCommand(createPost, userId);
var post = await Fixture.AddPostCommandHandler.HandleAsync(commandPost);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task HandleAsync_ShouldDeleteComment_WhenUserHasPermission()
var contentType = ContentType.Image;

var createPost = new CreatePostDto(title, description, contentUrl, contentType);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

var commandPost = new AddPostCommand(createPost, userId);
var post = await Fixture.AddPostCommandHandler.HandleAsync(commandPost);
Expand Down Expand Up @@ -65,7 +65,7 @@ public async Task HandleAsync_ShouldFail_WhenCommentNotFound()
var contentType = ContentType.Image;

var createPost = new CreatePostDto(title, description, contentUrl, contentType);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

var commandPost = new AddPostCommand(createPost, userId);
var post = await Fixture.AddPostCommandHandler.HandleAsync(commandPost);
Expand Down Expand Up @@ -95,7 +95,7 @@ public async Task HandleAsync_ShouldFail_WhenUserDoesNotHavePermissionToDeleteCo
var contentType = ContentType.Image;

var createPost = new CreatePostDto(title, description, contentUrl, contentType);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;
var anotherUserId = Guid.NewGuid();

var commandPost = new AddPostCommand(createPost, userId);
Expand Down Expand Up @@ -128,7 +128,7 @@ public async Task HandleAsync_ShouldFail_WhenPostNotFound()
// Arrange
var id = Guid.NewGuid();
var postId = Guid.Empty;
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

var command = new DeleteCommentCommand(id, postId, userId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public async Task HandleAsync_ShouldLikePost_WhenDateIsValid()
var contentType = ContentType.Image;

var createPost = new CreatePostDto(title, description, contentUrl, contentType);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

var commandPost = new AddPostCommand(createPost, userId);

Expand Down Expand Up @@ -50,7 +50,7 @@ public async Task HandleAsync_ShouldFail_WhenPostDoesNotExit()
{
// Arrange
var postId = Guid.Empty;
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

var command = new AddLikeCommand(postId, userId);

Expand All @@ -74,7 +74,7 @@ public async Task HandleAsync_ShouldFail_WhenUserTriesToLikePostTwice()
var contentType = ContentType.Image;

var createPost = new CreatePostDto(title, description, contentUrl, contentType);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

var commandPost = new AddPostCommand(createPost, userId);
var post = await Fixture.AddPostCommandHandler.HandleAsync(commandPost);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task HandleAsync_ShouldDeleteLikeFromPost_WhenDataIsValid()
var contentType = ContentType.Image;

var createPost = new CreatePostDto(title, description, contentUrl, contentType);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

var commandPost = new AddPostCommand(createPost, userId);

Expand Down Expand Up @@ -56,7 +56,7 @@ public async Task HandleAsync_ShouldFail_WhenLikeNotFound()
// Arrange
var nonExistentLikeId = Guid.Empty;
var postId = Guid.NewGuid();
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

var command = new DeleteLikeCommand(nonExistentLikeId, postId, userId);

Expand All @@ -80,7 +80,7 @@ public async Task HandleAsync_ShouldFail_WhenUserHasNotLikedPost()
var contentType = ContentType.Image;

var createPost = new CreatePostDto(title, description, contentUrl, contentType);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;
var anotherUserId = Guid.NewGuid();

var commandPost = new AddPostCommand(createPost, userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task HandleAsync_ShouldAddPost_WhenDataIsValid()
var contentType = ContentType.Image;

var createPost = new CreatePostDto(title, description, contentUrl, contentType);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

var command = new AddPostCommand(createPost, userId);

Expand All @@ -46,7 +46,7 @@ public async Task HandleAsync_ShouldFail_WhenTitleIsEmpty_ForTextContentType()
var contentType = ContentType.Text;

var createPost = new CreatePostDto(title, description, string.Empty, contentType);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

var command = new AddPostCommand(createPost, userId);

Expand All @@ -68,7 +68,7 @@ public async Task HandleAsync_ShouldFail_WhenDescriptionIsEmpty_ForTextContentTy
var contentType = ContentType.Text;

var createPost = new CreatePostDto(title, description, string.Empty, contentType);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

var command = new AddPostCommand(createPost, userId);

Expand All @@ -90,7 +90,7 @@ public async Task HandleAsync_ShouldFail_WhenTitleAndDescriptionAreEmpty_ForText
var contentType = ContentType.Text;

var createPost = new CreatePostDto(title, description, string.Empty, contentType);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

var command = new AddPostCommand(createPost, userId);

Expand All @@ -113,7 +113,7 @@ public async Task HandleAsync_ShouldFail_WhenContentUrlIsEmpty_ForImageOrVideoCo
var contentType = ContentType.Video;

var createPost = new CreatePostDto(title, description, contentUrl, contentType);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

var command = new AddPostCommand(createPost, userId);

Expand All @@ -137,7 +137,7 @@ public async Task HandleAsync_ShouldFail_WhenContentTypeIsInvalid()
var contentType = (ContentType)23;

var createPost = new CreatePostDto(title, description, contentUrl, contentType);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

var command = new AddPostCommand(createPost, userId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public async Task HandleAsync_ShouldDeletePost_WhenPostExists()
var contentType = ContentType.Image;

var createPost = new CreatePostDto(title, description, contentUrl, contentType);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

var postCommand = new AddPostCommand(createPost, userId);

Expand All @@ -45,7 +45,7 @@ public async Task HandleAsync_ShouldFail_WhenPostDoesNotExist()
{
// Arrange
var id = Guid.Empty;
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

// Act
var command = new DeletePostCommand(id, userId);
Expand All @@ -70,7 +70,7 @@ public async Task HandleAsync_ShouldFail_WhenUserIdDoesNotMatch()
var contentType = ContentType.Image;

var createPost = new CreatePostDto(title, description, contentUrl, contentType);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;
var anotherUserId = Guid.NewGuid();

var postCommand = new AddPostCommand(createPost, userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async Task HandleAsync_ShouldUpdatePost_WhenDataIsValid()
var contentType = ContentType.Image;

var createPost = new CreatePostDto(title, description, contentUrl, contentType);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

var postCommand = new AddPostCommand(createPost, userId);

Expand Down Expand Up @@ -53,7 +53,7 @@ [Fact] public async Task HandleAsync_ShouldFail_WhenPostDoesNotExist()
var newDescription = "New Description";

var updatePost = new UpdatePostDto(id, newTitle, newDescription);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

var command = new UpdatePostCommand(updatePost, userId);

Expand All @@ -77,7 +77,7 @@ public async Task HandleAsync_ShouldFail_WhenUserIdDoesNotMatch()
var contentType = ContentType.Image;

var createPost = new CreatePostDto(title, description, contentUrl, contentType);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;
var anotherUserId = Guid.NewGuid();

var postCommand = new AddPostCommand(createPost, userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public async Task HandleAsync_ShouldReturnComments_ForSpecifiedPost()
var contentType = ContentType.Image;

var createPost = new CreatePostDto(title, description, contentUrl, contentType);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

var commandPost = new AddPostCommand(createPost, userId);

Expand Down Expand Up @@ -75,7 +75,7 @@ public async Task HandleAsync_ShouldLimitResults_ToSpecifiedFirstParameter()
var contentType = ContentType.Image;

var createPost = new CreatePostDto(title, description, contentUrl, contentType);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

var commandPost = new AddPostCommand(createPost, userId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public async Task HandleAsync_ShouldReturnLikes_ForSpecifiedPost()
var contentType = ContentType.Image;

var createPost = new CreatePostDto(title, description, contentUrl, contentType);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

var commandPost = new AddPostCommand(createPost, userId);

Expand Down Expand Up @@ -75,7 +75,7 @@ public async Task HandleAsync_ShouldLimitResults_ToSpecifiedFirstParameter()
var contentType = ContentType.Image;

var createPost = new CreatePostDto(title, description, contentUrl, contentType);
var userId = Guid.NewGuid();
var userId = Fixture.ExistingUser.Id;

var commandPost = new AddPostCommand(createPost, userId);

Expand Down
Loading