| 1 | using ChapterX.Application.Chapter.Commands;
|
|---|
| 2 | using ChapterX.Application.Chapter.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 ChaptersController : ControllerBase
|
|---|
| 15 | {
|
|---|
| 16 | private readonly IMediator _mediator;
|
|---|
| 17 | private readonly IChapterRepository _chapterRepository;
|
|---|
| 18 | private readonly ILogger<ChaptersController> _logger;
|
|---|
| 19 |
|
|---|
| 20 | public ChaptersController(IMediator mediator, IChapterRepository chapterRepository, ILogger<ChaptersController> logger)
|
|---|
| 21 | {
|
|---|
| 22 | _mediator = mediator;
|
|---|
| 23 | _chapterRepository = chapterRepository;
|
|---|
| 24 | _logger = logger;
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | [HttpGet]
|
|---|
| 28 | [AllowAnonymous]
|
|---|
| 29 | public async Task<ActionResult> GetAll()
|
|---|
| 30 | {
|
|---|
| 31 | _logger.LogInformation("Fetching all chapters");
|
|---|
| 32 | var response = await _mediator.Send(new GetAllRequest());
|
|---|
| 33 | return Ok(response);
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | [HttpGet("{id:int}")]
|
|---|
| 37 | [AllowAnonymous]
|
|---|
| 38 | public async Task<ActionResult> GetById(int id)
|
|---|
| 39 | {
|
|---|
| 40 | _logger.LogInformation("Fetching chapter with ID: {ChapterId}", id);
|
|---|
| 41 | var response = await _mediator.Send(new GetRequest(id));
|
|---|
| 42 | return Ok(response);
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | [HttpPatch("{id:int}/view")]
|
|---|
| 46 | [AllowAnonymous]
|
|---|
| 47 | public async Task<ActionResult> IncrementView(int id)
|
|---|
| 48 | {
|
|---|
| 49 | await _chapterRepository.IncrementViewCountAsync(id);
|
|---|
| 50 | return NoContent();
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | [HttpPost]
|
|---|
| 54 | [Authorize]
|
|---|
| 55 | public async Task<ActionResult> Add([FromBody] AddRequest request)
|
|---|
| 56 | {
|
|---|
| 57 | _logger.LogInformation("Adding a new chapter with Number: {Number}", request.Number);
|
|---|
| 58 | var response = await _mediator.Send(request);
|
|---|
| 59 | return Ok(response);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | [HttpPut("{id:int}")]
|
|---|
| 63 | [Authorize]
|
|---|
| 64 | public async Task<ActionResult> Update(int id, [FromBody] UpdateRequest request)
|
|---|
| 65 | {
|
|---|
| 66 | _logger.LogInformation("Updating chapter with ID: {ChapterId}", id);
|
|---|
| 67 | if (id != request.Id)
|
|---|
| 68 | {
|
|---|
| 69 | return BadRequest("Route ID and body ID must match.");
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | var callerId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
|
|---|
| 73 | var response = await _mediator.Send(request with { CallerId = callerId });
|
|---|
| 74 | return Ok(response);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | [HttpDelete("{id:int}")]
|
|---|
| 78 | [Authorize]
|
|---|
| 79 | public async Task<ActionResult> Delete(int id)
|
|---|
| 80 | {
|
|---|
| 81 | _logger.LogInformation("Deleting chapter with ID: {ChapterId}", id);
|
|---|
| 82 | var callerId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
|
|---|
| 83 | var response = await _mediator.Send(new DeleteRequest(id, callerId));
|
|---|
| 84 | return Ok(response);
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|