Add one long hierarchical method, change some return codes
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using T120B165_ImgBoard.Dtos;
|
||||
using T120B165_ImgBoard.Dtos.Comment;
|
||||
using T120B165_ImgBoard.Dtos.Tag;
|
||||
using T120B165_ImgBoard.Models;
|
||||
using T120B165_ImgBoard.Services;
|
||||
@@ -10,7 +10,10 @@ namespace T120B165_ImgBoard.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/tags")]
|
||||
public class TagController(ITagService tagService) : ControllerBase
|
||||
public class TagController(
|
||||
ITagService tagService,
|
||||
IPostService postService,
|
||||
ICommentService commentService) : ControllerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new tag.
|
||||
@@ -126,4 +129,34 @@ public class TagController(ITagService tagService) : ControllerBase
|
||||
var updatedTag = await tagService.Update(tag, dto.Type);
|
||||
return Ok(updatedTag);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Get specific tag, specific post comment.
|
||||
/// </summary>
|
||||
/// <param name="tagName">Tag name</param>
|
||||
/// <param name="postId">Post ID</param>
|
||||
/// <param name="commentId">Comment ID</param>
|
||||
/// <response code="200">Comment data</response>
|
||||
/// <response code="400">If request is malformed</response>
|
||||
/// <response code="404">If tag or post or comment is not found</response>
|
||||
[HttpGet("{tagName}/posts/{postId:int}/comments/{commentId:int}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult<CommentDto>> GetComment(string tagName, int postId, int commentId)
|
||||
{
|
||||
var tag = await tagService.GetByName(tagName);
|
||||
if (tag == null) return NotFound();
|
||||
|
||||
var entry = await postService.GetById(postId);
|
||||
if (entry == null) return NotFound();
|
||||
|
||||
if (entry.Tags.All(t => t.Name != tag.Name)) return NotFound();
|
||||
|
||||
var comment = await commentService.GetById(commentId);
|
||||
if (comment == null || entry.Id != comment.OriginalPost.Id) return NotFound();
|
||||
|
||||
return Ok(CommentDto.FromComment(comment));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user