source: ChapterX.API/Controllers/ChaptersController.cs@ 0b502c2

main
Last change on this file since 0b502c2 was 0b502c2, checked in by kikisrbinoska <srbinoskakristina07@…>, 12 days ago

Fixed user profile and reading lists

  • Property mode set to 100644
File size: 2.5 KB
Line 
1using ChapterX.Application.Chapter.Commands;
2using ChapterX.Application.Chapter.Queries;
3using MediatR;
4using Microsoft.AspNetCore.Authorization;
5using Microsoft.AspNetCore.Mvc;
6using Microsoft.Extensions.Logging;
7using System.Security.Claims;
8
9namespace ChapterX.API.Controllers
10{
11 [Route("api/[controller]")]
12 [ApiController]
13 public class ChaptersController : ControllerBase
14 {
15 private readonly IMediator _mediator;
16 private readonly ILogger<ChaptersController> _logger;
17
18 public ChaptersController(IMediator mediator, ILogger<ChaptersController> logger)
19 {
20 _mediator = mediator;
21 _logger = logger;
22 }
23
24 [HttpGet]
25 [AllowAnonymous]
26 public async Task<ActionResult> GetAll()
27 {
28 _logger.LogInformation("Fetching all chapters");
29 var response = await _mediator.Send(new GetAllRequest());
30 return Ok(response);
31 }
32
33 [HttpGet("{id:int}")]
34 [AllowAnonymous]
35 public async Task<ActionResult> GetById(int id)
36 {
37 _logger.LogInformation("Fetching chapter with ID: {ChapterId}", id);
38 var response = await _mediator.Send(new GetRequest(id));
39 return Ok(response);
40 }
41
42 [HttpPost]
43 [Authorize]
44 public async Task<ActionResult> Add([FromBody] AddRequest request)
45 {
46 _logger.LogInformation("Adding a new chapter with Number: {Number}", request.Number);
47 var response = await _mediator.Send(request);
48 return Ok(response);
49 }
50
51 [HttpPut("{id:int}")]
52 [Authorize]
53 public async Task<ActionResult> Update(int id, [FromBody] UpdateRequest request)
54 {
55 _logger.LogInformation("Updating chapter with ID: {ChapterId}", id);
56 if (id != request.Id)
57 {
58 return BadRequest("Route ID and body ID must match.");
59 }
60
61 var callerId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
62 var response = await _mediator.Send(request with { CallerId = callerId });
63 return Ok(response);
64 }
65
66 [HttpDelete("{id:int}")]
67 [Authorize]
68 public async Task<ActionResult> Delete(int id)
69 {
70 _logger.LogInformation("Deleting chapter with ID: {ChapterId}", id);
71 var callerId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
72 var response = await _mediator.Send(new DeleteRequest(id, callerId));
73 return Ok(response);
74 }
75 }
76}
77
Note: See TracBrowser for help on using the repository browser.