| 1 | using ChapterX.Application.Comment.Commands;
|
|---|
| 2 | using ChapterX.Application.Comment.Queries;
|
|---|
| 3 | using ChapterX.Domain.Repositories;
|
|---|
| 4 | using MediatR;
|
|---|
| 5 | using Microsoft.AspNetCore.Authorization;
|
|---|
| 6 | using Microsoft.AspNetCore.Mvc;
|
|---|
| 7 | using Microsoft.Extensions.Logging;
|
|---|
| 8 | using System.Security.Claims;
|
|---|
| 9 |
|
|---|
| 10 | namespace ChapterX.API.Controllers
|
|---|
| 11 | {
|
|---|
| 12 | [Route("api/[controller]")]
|
|---|
| 13 | [ApiController]
|
|---|
| 14 | public class CommentsController : ControllerBase
|
|---|
| 15 | {
|
|---|
| 16 | private readonly IMediator _mediator;
|
|---|
| 17 | private readonly ICommentRepository _commentRepository;
|
|---|
| 18 | private readonly ILogger<CommentsController> _logger;
|
|---|
| 19 |
|
|---|
| 20 | public CommentsController(IMediator mediator, ICommentRepository commentRepository, ILogger<CommentsController> logger)
|
|---|
| 21 | {
|
|---|
| 22 | _mediator = mediator;
|
|---|
| 23 | _commentRepository = commentRepository;
|
|---|
| 24 | _logger = logger;
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | [HttpGet("story/{storyId:int}")]
|
|---|
| 28 | [AllowAnonymous]
|
|---|
| 29 | public async Task<ActionResult> GetByStory(int storyId)
|
|---|
| 30 | {
|
|---|
| 31 | var comments = await _commentRepository.GetByStoryIdAsync(storyId);
|
|---|
| 32 | var result = comments.Select(c => new
|
|---|
| 33 | {
|
|---|
| 34 | id = c.Id,
|
|---|
| 35 | content = c.Content,
|
|---|
| 36 | userId = c.UserId,
|
|---|
| 37 | storyId = c.StoryId,
|
|---|
| 38 | username = c.User?.Username ?? "",
|
|---|
| 39 | createdAt = c.CreatedAt,
|
|---|
| 40 | });
|
|---|
| 41 | return Ok(result);
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | [HttpGet]
|
|---|
| 45 | [AllowAnonymous]
|
|---|
| 46 | public async Task<ActionResult> GetAll()
|
|---|
| 47 | {
|
|---|
| 48 | _logger.LogInformation("Fetching all comments");
|
|---|
| 49 | var response = await _mediator.Send(new GetAllRequest());
|
|---|
| 50 | return Ok(response);
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | [HttpGet("{id:int}")]
|
|---|
| 54 | [AllowAnonymous]
|
|---|
| 55 | public async Task<ActionResult> GetById(int id)
|
|---|
| 56 | {
|
|---|
| 57 | _logger.LogInformation("Fetching comment with ID: {CommentId}", id);
|
|---|
| 58 | var response = await _mediator.Send(new GetRequest(id));
|
|---|
| 59 | return Ok(response);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | [HttpPost]
|
|---|
| 63 | [Authorize]
|
|---|
| 64 | public async Task<ActionResult> Add([FromBody] AddRequest request)
|
|---|
| 65 | {
|
|---|
| 66 | var callerId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
|
|---|
| 67 | _logger.LogInformation("Adding a new comment");
|
|---|
| 68 | var response = await _mediator.Send(request with { UserId = callerId });
|
|---|
| 69 | return Ok(response);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | [HttpPut("{id:int}")]
|
|---|
| 73 | [Authorize]
|
|---|
| 74 | public async Task<ActionResult> Update(int id, [FromBody] UpdateRequest request)
|
|---|
| 75 | {
|
|---|
| 76 | _logger.LogInformation("Updating comment with ID: {CommentId}", id);
|
|---|
| 77 | if (id != request.Id)
|
|---|
| 78 | {
|
|---|
| 79 | return BadRequest("Route ID and body ID must match.");
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | var callerId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
|
|---|
| 83 | var response = await _mediator.Send(request with { CallerId = callerId });
|
|---|
| 84 | return Ok(response);
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | [HttpDelete("{id:int}")]
|
|---|
| 88 | [Authorize]
|
|---|
| 89 | public async Task<ActionResult> Delete(int id)
|
|---|
| 90 | {
|
|---|
| 91 | _logger.LogInformation("Deleting comment with ID: {CommentId}", id);
|
|---|
| 92 | var callerId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
|
|---|
| 93 | var response = await _mediator.Send(new DeleteRequest(id, callerId));
|
|---|
| 94 | return Ok(response);
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|