source: ChapterX.API/Controllers/StoriesController.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.8 KB
Line 
1using ChapterX.Application.Story.Commands;
2using ChapterX.Application.Story.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 StoriesController : ControllerBase
14 {
15 private readonly IMediator _mediator;
16 private readonly ILogger<StoriesController> _logger;
17
18 public StoriesController(IMediator mediator, ILogger<StoriesController> logger)
19 {
20 _mediator = mediator;
21 _logger = logger;
22 }
23
24 // GET: api/Stories
25 [HttpGet]
26 [AllowAnonymous]
27 public async Task<ActionResult> GetAll([FromQuery] GetAllRequest request)
28 {
29 _logger.LogInformation("Fetching all stories");
30 var response = await _mediator.Send(request);
31 return Ok(response);
32 }
33
34 // GET: api/Stories/5
35 [HttpGet("{id:int}")]
36 [AllowAnonymous]
37 public async Task<ActionResult> GetById([FromRoute] int id)
38 {
39 _logger.LogInformation("Fetching story with ID: {StoryId}", id);
40 var response = await _mediator.Send(new GetRequest(id));
41 return Ok(response);
42 }
43
44 // POST: api/Stories
45 [HttpPost]
46 [Authorize]
47 public async Task<ActionResult> Add([FromBody] AddRequest request)
48 {
49 var callerId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
50 _logger.LogInformation("Adding a new story for UserId: {UserId}", callerId);
51 var response = await _mediator.Send(request with { UserId = callerId });
52 return Ok(response);
53 }
54
55 // PUT: api/Stories/5
56 [HttpPut("{id:int}")]
57 [Authorize]
58 public async Task<ActionResult> Update([FromRoute] int id, [FromBody] UpdateRequest request)
59 {
60 _logger.LogInformation("Updating story with ID: {StoryId}", id);
61 if (id != request.Id)
62 {
63 return BadRequest("Route ID and body ID must match.");
64 }
65
66 var callerId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
67 var response = await _mediator.Send(request with { CallerId = callerId });
68 return Ok(response);
69 }
70
71 // DELETE: api/Stories/5
72 [HttpDelete("{id:int}")]
73 [Authorize]
74 public async Task<ActionResult> Delete([FromRoute] int id)
75 {
76 _logger.LogInformation("Deleting story with ID: {StoryId}", id);
77 var callerId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
78 var response = await _mediator.Send(new DeleteRequest(id, callerId));
79 return Ok(response);
80 }
81 }
82}
83
Note: See TracBrowser for help on using the repository browser.