| 1 | using ChapterX.Application.ReadingListItems.Commands;
|
|---|
| 2 | using ChapterX.Application.ReadingListItems.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 ReadingListItemsController : ControllerBase
|
|---|
| 14 | {
|
|---|
| 15 | private readonly IMediator _mediator;
|
|---|
| 16 | private readonly IReadingListItemsRepository _readingListItemsRepository;
|
|---|
| 17 | private readonly ILogger<ReadingListItemsController> _logger;
|
|---|
| 18 |
|
|---|
| 19 | public ReadingListItemsController(IMediator mediator, IReadingListItemsRepository readingListItemsRepository, ILogger<ReadingListItemsController> logger)
|
|---|
| 20 | {
|
|---|
| 21 | _mediator = mediator;
|
|---|
| 22 | _readingListItemsRepository = readingListItemsRepository;
|
|---|
| 23 | _logger = logger;
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | [HttpGet]
|
|---|
| 27 | [AllowAnonymous]
|
|---|
| 28 | public async Task<ActionResult> GetAll()
|
|---|
| 29 | {
|
|---|
| 30 | _logger.LogInformation("Fetching all reading list items");
|
|---|
| 31 | var response = await _mediator.Send(new GetAllRequest());
|
|---|
| 32 | return Ok(response);
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | [HttpGet("{id:int}")]
|
|---|
| 36 | [AllowAnonymous]
|
|---|
| 37 | public async Task<ActionResult> GetById(int id)
|
|---|
| 38 | {
|
|---|
| 39 | _logger.LogInformation("Fetching reading list item with ID: {ReadingListItemId}", id);
|
|---|
| 40 | var response = await _mediator.Send(new GetRequest(id));
|
|---|
| 41 | return Ok(response);
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | [HttpPost]
|
|---|
| 45 | [Authorize]
|
|---|
| 46 | public async Task<ActionResult> Add([FromBody] AddRequest request)
|
|---|
| 47 | {
|
|---|
| 48 | _logger.LogInformation("Adding a new reading list item to ReadingListId: {ReadingListId}", request.ReadingListId);
|
|---|
| 49 | var response = await _mediator.Send(request);
|
|---|
| 50 | return Ok(response);
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | [HttpPut("{id:int}")]
|
|---|
| 54 | [Authorize]
|
|---|
| 55 | public async Task<ActionResult> Update(int id, [FromBody] UpdateRequest request)
|
|---|
| 56 | {
|
|---|
| 57 | _logger.LogInformation("Updating reading list item with ID: {ReadingListItemId}", id);
|
|---|
| 58 | if (id != request.Id)
|
|---|
| 59 | {
|
|---|
| 60 | return BadRequest("Route ID and body ID must match.");
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | var response = await _mediator.Send(request);
|
|---|
| 64 | return Ok(response);
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | [HttpDelete("{id:int}")]
|
|---|
| 68 | [Authorize]
|
|---|
| 69 | public async Task<ActionResult> Delete(int id)
|
|---|
| 70 | {
|
|---|
| 71 | _logger.LogInformation("Deleting reading list item with ID: {ReadingListItemId}", id);
|
|---|
| 72 | var response = await _mediator.Send(new DeleteRequest(id));
|
|---|
| 73 | return Ok(response);
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | [HttpDelete("{listId:int}/story/{storyId:int}")]
|
|---|
| 77 | [Authorize]
|
|---|
| 78 | public async Task<ActionResult> DeleteByListAndStory(int listId, int storyId)
|
|---|
| 79 | {
|
|---|
| 80 | _logger.LogInformation("Removing story {StoryId} from list {ListId}", storyId, listId);
|
|---|
| 81 | var deleted = await _readingListItemsRepository.DeleteByListAndStoryAsync(listId, storyId);
|
|---|
| 82 | return deleted ? Ok() : NotFound();
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|