| 1 | using ChapterX.Application.Likes.Commands;
|
|---|
| 2 | using ChapterX.Application.Likes.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 |
|
|---|
| 9 | namespace ChapterX.API.Controllers
|
|---|
| 10 | {
|
|---|
| 11 | [Route("api/[controller]")]
|
|---|
| 12 | [ApiController]
|
|---|
| 13 | public class LikesController : ControllerBase
|
|---|
| 14 | {
|
|---|
| 15 | private readonly IMediator _mediator;
|
|---|
| 16 | private readonly ILikesRepository _likesRepository;
|
|---|
| 17 | private readonly ILogger<LikesController> _logger;
|
|---|
| 18 |
|
|---|
| 19 | public LikesController(IMediator mediator, ILikesRepository likesRepository, ILogger<LikesController> logger)
|
|---|
| 20 | {
|
|---|
| 21 | _mediator = mediator;
|
|---|
| 22 | _likesRepository = likesRepository;
|
|---|
| 23 | _logger = logger;
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | [HttpGet("story/{storyId:int}")]
|
|---|
| 27 | [AllowAnonymous]
|
|---|
| 28 | public async Task<ActionResult> GetByStory(int storyId)
|
|---|
| 29 | {
|
|---|
| 30 | var likes = await _likesRepository.GetByStoryIdAsync(storyId);
|
|---|
| 31 | return Ok(new { count = likes.Count(), userIds = likes.Select(l => l.UserId).ToList() });
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | [HttpDelete("user/{userId:int}/story/{storyId:int}")]
|
|---|
| 35 | [Authorize]
|
|---|
| 36 | public async Task<ActionResult> DeleteByUserAndStory(int userId, int storyId)
|
|---|
| 37 | {
|
|---|
| 38 | var deleted = await _likesRepository.DeleteByUserAndStoryAsync(userId, storyId);
|
|---|
| 39 | if (!deleted) return NotFound();
|
|---|
| 40 | return Ok();
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | [HttpGet]
|
|---|
| 44 | [AllowAnonymous]
|
|---|
| 45 | public async Task<ActionResult> GetAll()
|
|---|
| 46 | {
|
|---|
| 47 | _logger.LogInformation("Fetching all likes");
|
|---|
| 48 | var response = await _mediator.Send(new GetAllRequest());
|
|---|
| 49 | return Ok(response);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | [HttpGet("{id:int}")]
|
|---|
| 53 | [AllowAnonymous]
|
|---|
| 54 | public async Task<ActionResult> GetById(int id)
|
|---|
| 55 | {
|
|---|
| 56 | _logger.LogInformation("Fetching like with ID: {LikeId}", id);
|
|---|
| 57 | var response = await _mediator.Send(new GetRequest(id));
|
|---|
| 58 | return Ok(response);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | [HttpPost]
|
|---|
| 62 | [Authorize]
|
|---|
| 63 | public async Task<ActionResult> Add([FromBody] AddRequest request)
|
|---|
| 64 | {
|
|---|
| 65 | _logger.LogInformation("Adding a new like for StoryId: {StoryId}", request.StoryId);
|
|---|
| 66 | var exists = await _likesRepository.ExistsAsync(request.UserId, request.StoryId);
|
|---|
| 67 | if (exists) return Ok();
|
|---|
| 68 | var response = await _mediator.Send(request);
|
|---|
| 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 like with ID: {LikeId}", id);
|
|---|
| 77 | if (id != request.Id)
|
|---|
| 78 | {
|
|---|
| 79 | return BadRequest("Route ID and body ID must match.");
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | var response = await _mediator.Send(request);
|
|---|
| 83 | return Ok(response);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | [HttpDelete("{id:int}")]
|
|---|
| 87 | [Authorize]
|
|---|
| 88 | public async Task<ActionResult> Delete(int id)
|
|---|
| 89 | {
|
|---|
| 90 | _logger.LogInformation("Deleting like with ID: {LikeId}", id);
|
|---|
| 91 | var response = await _mediator.Send(new DeleteRequest(id));
|
|---|
| 92 | return Ok(response);
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|