| 1 | using ChapterX.Application.Story.Commands;
|
|---|
| 2 | using ChapterX.Application.Story.Queries;
|
|---|
| 3 | using MediatR;
|
|---|
| 4 | using Microsoft.AspNetCore.Authorization;
|
|---|
| 5 | using Microsoft.AspNetCore.Mvc;
|
|---|
| 6 | using Microsoft.Extensions.Logging;
|
|---|
| 7 |
|
|---|
| 8 | namespace ChapterX.API.Controllers
|
|---|
| 9 | {
|
|---|
| 10 | [Route("api/[controller]")]
|
|---|
| 11 | [ApiController]
|
|---|
| 12 | public class StoriesController : ControllerBase
|
|---|
| 13 | {
|
|---|
| 14 | private readonly IMediator _mediator;
|
|---|
| 15 | private readonly ILogger<StoriesController> _logger;
|
|---|
| 16 |
|
|---|
| 17 | public StoriesController(IMediator mediator, ILogger<StoriesController> logger)
|
|---|
| 18 | {
|
|---|
| 19 | _mediator = mediator;
|
|---|
| 20 | _logger = logger;
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | // GET: api/Stories
|
|---|
| 24 | [HttpGet]
|
|---|
| 25 | [AllowAnonymous]
|
|---|
| 26 | public async Task<ActionResult> GetAll([FromQuery] GetAllRequest request)
|
|---|
| 27 | {
|
|---|
| 28 | _logger.LogInformation("Fetching all stories");
|
|---|
| 29 | var response = await _mediator.Send(request);
|
|---|
| 30 | return Ok(response);
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | // GET: api/Stories/5
|
|---|
| 34 | [HttpGet("{id:int}")]
|
|---|
| 35 | [AllowAnonymous]
|
|---|
| 36 | public async Task<ActionResult> GetById([FromRoute] int id)
|
|---|
| 37 | {
|
|---|
| 38 | _logger.LogInformation("Fetching story with ID: {StoryId}", id);
|
|---|
| 39 | var response = await _mediator.Send(new GetRequest(id));
|
|---|
| 40 | return Ok(response);
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | // POST: api/Stories
|
|---|
| 44 | [HttpPost]
|
|---|
| 45 | [Authorize]
|
|---|
| 46 | public async Task<ActionResult> Add([FromBody] AddRequest request)
|
|---|
| 47 | {
|
|---|
| 48 | _logger.LogInformation("Adding a new story for UserId: {UserId}", request.UserId);
|
|---|
| 49 | var response = await _mediator.Send(request);
|
|---|
| 50 | return Ok(response);
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | // PUT: api/Stories/5
|
|---|
| 54 | [HttpPut("{id:int}")]
|
|---|
| 55 | [Authorize]
|
|---|
| 56 | public async Task<ActionResult> Update([FromRoute] int id, [FromBody] UpdateRequest request)
|
|---|
| 57 | {
|
|---|
| 58 | _logger.LogInformation("Updating story with ID: {StoryId}", id);
|
|---|
| 59 | if (id != request.Id)
|
|---|
| 60 | {
|
|---|
| 61 | return BadRequest("Route ID and body ID must match.");
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | var response = await _mediator.Send(request);
|
|---|
| 65 | return Ok(response);
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | // DELETE: api/Stories/5
|
|---|
| 69 | [HttpDelete("{id:int}")]
|
|---|
| 70 | [Authorize]
|
|---|
| 71 | public async Task<ActionResult> Delete([FromRoute] int id)
|
|---|
| 72 | {
|
|---|
| 73 | _logger.LogInformation("Deleting story with ID: {StoryId}", id);
|
|---|
| 74 | var response = await _mediator.Send(new DeleteRequest(id));
|
|---|
| 75 | return Ok(response);
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|