| 1 | using ChapterX.Application.AISuggestion.Commands;
|
|---|
| 2 | using ChapterX.Application.AISuggestion.Queries;
|
|---|
| 3 | using MediatR;
|
|---|
| 4 | using Microsoft.AspNetCore.Authorization;
|
|---|
| 5 | using Microsoft.AspNetCore.Mvc;
|
|---|
| 6 | using Microsoft.Extensions.Logging;
|
|---|
| 7 | using System.Linq;
|
|---|
| 8 |
|
|---|
| 9 | namespace ChapterX.API.Controllers
|
|---|
| 10 | {
|
|---|
| 11 | [Route("api/[controller]")]
|
|---|
| 12 | [ApiController]
|
|---|
| 13 | public class AISuggestionsController : ControllerBase
|
|---|
| 14 | {
|
|---|
| 15 | private readonly IMediator _mediator;
|
|---|
| 16 | private readonly ILogger<AISuggestionsController> _logger;
|
|---|
| 17 |
|
|---|
| 18 | public AISuggestionsController(IMediator mediator, ILogger<AISuggestionsController> 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 AI suggestions");
|
|---|
| 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 AI suggestion with ID: {AISuggestionId}", id);
|
|---|
| 38 | var response = await _mediator.Send(new GetRequest(id));
|
|---|
| 39 | return Ok(response);
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | [HttpGet("chapter/{chapterId:int}")]
|
|---|
| 43 | [AllowAnonymous]
|
|---|
| 44 | public async Task<ActionResult> GetByChapter(int chapterId)
|
|---|
| 45 | {
|
|---|
| 46 | _logger.LogInformation("Fetching AI suggestions for ChapterId: {ChapterId}", chapterId);
|
|---|
| 47 | var response = await _mediator.Send(new GetByChapterRequest(chapterId));
|
|---|
| 48 | var result = response.AISuggestions.Select(s => new
|
|---|
| 49 | {
|
|---|
| 50 | id = s.Id,
|
|---|
| 51 | originalText = s.OriginalText,
|
|---|
| 52 | suggestedText = s.SuggestedText,
|
|---|
| 53 | accepted = s.Accepted,
|
|---|
| 54 | createdAt = s.CreatedAt,
|
|---|
| 55 | appliedAt = s.AppliedAt,
|
|---|
| 56 | storyId = s.StoryId,
|
|---|
| 57 | suggestionTypes = s.SuggestionTypes.Select(t => t.SuggestionTypeValue),
|
|---|
| 58 | });
|
|---|
| 59 | return Ok(result);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | [HttpPost]
|
|---|
| 63 | [Authorize]
|
|---|
| 64 | public async Task<ActionResult> Add([FromBody] AddRequest request)
|
|---|
| 65 | {
|
|---|
| 66 | _logger.LogInformation("Adding a new AI suggestion");
|
|---|
| 67 | var response = await _mediator.Send(request);
|
|---|
| 68 | return Ok(response);
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | [HttpPut("{id:int}")]
|
|---|
| 72 | [Authorize]
|
|---|
| 73 | public async Task<ActionResult> Update(int id, [FromBody] UpdateRequest request)
|
|---|
| 74 | {
|
|---|
| 75 | _logger.LogInformation("Updating AI suggestion with ID: {AISuggestionId}", id);
|
|---|
| 76 | if (id != request.Id)
|
|---|
| 77 | {
|
|---|
| 78 | return BadRequest("Route ID and body ID must match.");
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | var response = await _mediator.Send(request);
|
|---|
| 82 | return Ok(response);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | [HttpDelete("{id:int}")]
|
|---|
| 86 | [Authorize]
|
|---|
| 87 | public async Task<ActionResult> Delete(int id)
|
|---|
| 88 | {
|
|---|
| 89 | _logger.LogInformation("Deleting AI suggestion with ID: {AISuggestionId}", id);
|
|---|
| 90 | var response = await _mediator.Send(new DeleteRequest(id));
|
|---|
| 91 | return Ok(response);
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|