| 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 | using System.Security.Claims;
|
|---|
| 8 |
|
|---|
| 9 | namespace 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 | var stories = response.Stories.Select(s => new
|
|---|
| 32 | {
|
|---|
| 33 | id = s.Id,
|
|---|
| 34 | userId = s.UserId,
|
|---|
| 35 | title = s.Title,
|
|---|
| 36 | shortDescription = s.ShortDescription,
|
|---|
| 37 | image = s.Image,
|
|---|
| 38 | content = s.Content,
|
|---|
| 39 | matureContent = s.MatureContent,
|
|---|
| 40 | createdAt = s.CreatedAt,
|
|---|
| 41 | updatedAt = s.UpdatedAt,
|
|---|
| 42 | writer = s.Writer == null ? null : new { user = s.Writer.User == null ? null : new { username = s.Writer.User.Username } },
|
|---|
| 43 | hasGenres = s.HasGenres.Select(hg => new { genre = new { name = hg.Genre?.Name ?? "" } }),
|
|---|
| 44 | likes = s.Likes.Select(l => new { userId = l.UserId }),
|
|---|
| 45 | comments = s.Comments.Select(c => new { id = c.Id }),
|
|---|
| 46 | chapters = s.Chapters.Select(c => new { id = c.Id, viewCount = c.ViewCount }),
|
|---|
| 47 | });
|
|---|
| 48 | return Ok(new { stories });
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | // GET: api/Stories/5
|
|---|
| 52 | [HttpGet("{id:int}")]
|
|---|
| 53 | [AllowAnonymous]
|
|---|
| 54 | public async Task<ActionResult> GetById([FromRoute] int id)
|
|---|
| 55 | {
|
|---|
| 56 | _logger.LogInformation("Fetching story with ID: {StoryId}", id);
|
|---|
| 57 | var response = await _mediator.Send(new GetRequest(id));
|
|---|
| 58 | if (response.Story == null) return NotFound();
|
|---|
| 59 | var s = response.Story;
|
|---|
| 60 | return Ok(new
|
|---|
| 61 | {
|
|---|
| 62 | id = s.Id,
|
|---|
| 63 | userId = s.UserId,
|
|---|
| 64 | title = s.Title,
|
|---|
| 65 | shortDescription = s.ShortDescription,
|
|---|
| 66 | image = s.Image,
|
|---|
| 67 | content = s.Content,
|
|---|
| 68 | matureContent = s.MatureContent,
|
|---|
| 69 | createdAt = s.CreatedAt,
|
|---|
| 70 | updatedAt = s.UpdatedAt,
|
|---|
| 71 | writer = s.Writer == null ? null : new { user = s.Writer.User == null ? null : new { username = s.Writer.User.Username } },
|
|---|
| 72 | hasGenres = s.HasGenres.Select(hg => new { genre = new { name = hg.Genre?.Name ?? "" } }),
|
|---|
| 73 | likes = s.Likes.Select(l => new { userId = l.UserId }),
|
|---|
| 74 | comments = s.Comments.Select(c => new { id = c.Id }),
|
|---|
| 75 | chapters = s.Chapters.Select(c => new { id = c.Id, viewCount = c.ViewCount }),
|
|---|
| 76 | });
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | // POST: api/Stories
|
|---|
| 80 | [HttpPost]
|
|---|
| 81 | [Authorize]
|
|---|
| 82 | public async Task<ActionResult> Add([FromBody] AddRequest request)
|
|---|
| 83 | {
|
|---|
| 84 | var callerId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
|
|---|
| 85 | _logger.LogInformation("Adding a new story for UserId: {UserId}", callerId);
|
|---|
| 86 | var response = await _mediator.Send(request with { UserId = callerId });
|
|---|
| 87 | return Ok(response);
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | // PUT: api/Stories/5
|
|---|
| 91 | [HttpPut("{id:int}")]
|
|---|
| 92 | [Authorize]
|
|---|
| 93 | public async Task<ActionResult> Update([FromRoute] int id, [FromBody] UpdateRequest request)
|
|---|
| 94 | {
|
|---|
| 95 | _logger.LogInformation("Updating story with ID: {StoryId}", id);
|
|---|
| 96 | if (id != request.Id)
|
|---|
| 97 | {
|
|---|
| 98 | return BadRequest("Route ID and body ID must match.");
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | var callerId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
|
|---|
| 102 | var response = await _mediator.Send(request with { CallerId = callerId });
|
|---|
| 103 | return Ok(response);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | // DELETE: api/Stories/5
|
|---|
| 107 | [HttpDelete("{id:int}")]
|
|---|
| 108 | [Authorize]
|
|---|
| 109 | public async Task<ActionResult> Delete([FromRoute] int id)
|
|---|
| 110 | {
|
|---|
| 111 | _logger.LogInformation("Deleting story with ID: {StoryId}", id);
|
|---|
| 112 | var callerId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
|
|---|
| 113 | var isAdmin = User.IsInRole("Admin");
|
|---|
| 114 | var response = await _mediator.Send(new DeleteRequest(id, callerId, isAdmin));
|
|---|
| 115 | return Ok(response);
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|