This question concerns CQRS and DDD.
I want to create a post comment. This is my endpoint: /posts/{postId}/comments <-- Http POST
But firstly I need to check if a post with a specific ID exists. In each article, each book about CQRS and DDD each class name of query starts with Getxxxxx and it returns data. I didn't find anywhere an example of query which checks if an item exists and returns true/false, why?? I wonder if I can create a query called "PostExistsQuery". Because on the whole internet there is no similar example. :O Maybe I am doing something wrong? :0
[HttpPost("/posts/{postId}/comments")]
public async Task<IActionResult> CreatePostComment(Guid postId, CreateCommentDTO commentDTO)
{
if (await _mediator.Send(new PostExistsQuery(postId)) == false) // check if a post exists
{
return NotFound();
}
var commentCommand = new CreateCommentCommand(Guid.NewGuid(), postId, commentDTO.Author, commentDTO.Content);
await _mediator.Send(commentCommand);
return CreatedAtAction(nameof(GetCommentById), new { id = commentCommand.CommentId });
}
Try to read more about Layers in DDD, especially about Domain and Application Layers. I think you missed some core concepts, because your example shows you trying to implement business logic in your API.
In your concrete example Domain layer is responsible for checking that your aggregate exists. You should use Repository pattern (or similar) in your command handler to fetch your aggregates and save them after processing. When aggregate not found, Repository throws an Exception.
P.S. CQRS pattern is all about responsibility segregation of read and write sides of your application, so you should not use read-model in command side.