| [877c13c] | 1 | using ChapterX.Application.Collaboration.Commands;
|
|---|
| 2 | using ChapterX.Application.Collaboration.Queries;
|
|---|
| [7fbb91c] | 3 | using ChapterX.Domain.Repositories;
|
|---|
| [877c13c] | 4 | using MediatR;
|
|---|
| 5 | using Microsoft.AspNetCore.Authorization;
|
|---|
| 6 | using Microsoft.AspNetCore.Mvc;
|
|---|
| 7 | using Microsoft.Extensions.Logging;
|
|---|
| 8 |
|
|---|
| 9 | namespace ChapterX.API.Controllers
|
|---|
| 10 | {
|
|---|
| 11 | [Route("api/[controller]")]
|
|---|
| 12 | [ApiController]
|
|---|
| 13 | public class CollaborationsController : ControllerBase
|
|---|
| 14 | {
|
|---|
| 15 | private readonly IMediator _mediator;
|
|---|
| [7fbb91c] | 16 | private readonly ICollaborationRepository _collaborationRepository;
|
|---|
| [877c13c] | 17 | private readonly ILogger<CollaborationsController> _logger;
|
|---|
| 18 |
|
|---|
| [7fbb91c] | 19 | public CollaborationsController(IMediator mediator, ICollaborationRepository collaborationRepository, ILogger<CollaborationsController> logger)
|
|---|
| [877c13c] | 20 | {
|
|---|
| 21 | _mediator = mediator;
|
|---|
| [7fbb91c] | 22 | _collaborationRepository = collaborationRepository;
|
|---|
| [877c13c] | 23 | _logger = logger;
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | [HttpGet]
|
|---|
| 27 | [AllowAnonymous]
|
|---|
| 28 | public async Task<ActionResult> GetAll()
|
|---|
| 29 | {
|
|---|
| 30 | _logger.LogInformation("Fetching all collaborations");
|
|---|
| [7fbb91c] | 31 | var collabs = await _collaborationRepository.GetAllAsync();
|
|---|
| 32 | var result = collabs.Select(c => new
|
|---|
| 33 | {
|
|---|
| 34 | id = c.Id,
|
|---|
| 35 | userId = c.UserId,
|
|---|
| 36 | storyId = c.StoryId,
|
|---|
| 37 | username = c.User != null ? c.User.Username : "",
|
|---|
| 38 | name = c.User != null ? c.User.Name : "",
|
|---|
| 39 | createdAt = c.CreatedAt,
|
|---|
| 40 | });
|
|---|
| 41 | return Ok(result);
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | [HttpDelete("user/{userId:int}/story/{storyId:int}")]
|
|---|
| 45 | [Authorize]
|
|---|
| 46 | public async Task<ActionResult> DeleteByUserAndStory(int userId, int storyId)
|
|---|
| 47 | {
|
|---|
| 48 | var deleted = await _collaborationRepository.DeleteByUserAndStoryAsync(userId, storyId);
|
|---|
| 49 | if (!deleted) return NotFound();
|
|---|
| 50 | return Ok();
|
|---|
| [877c13c] | 51 | }
|
|---|
| 52 |
|
|---|
| 53 | [HttpGet("{id:int}")]
|
|---|
| 54 | [AllowAnonymous]
|
|---|
| 55 | public async Task<ActionResult> GetById(int id)
|
|---|
| 56 | {
|
|---|
| 57 | _logger.LogInformation("Fetching collaboration with ID: {CollaborationId}", id);
|
|---|
| 58 | var response = await _mediator.Send(new GetRequest(id));
|
|---|
| 59 | return Ok(response);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | [HttpPost]
|
|---|
| 63 | [Authorize]
|
|---|
| 64 | public async Task<ActionResult> Add([FromBody] AddRequest request)
|
|---|
| 65 | {
|
|---|
| 66 | _logger.LogInformation("Adding a new collaboration");
|
|---|
| 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 collaboration with ID: {CollaborationId}", 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 collaboration with ID: {CollaborationId}", id);
|
|---|
| 90 | var response = await _mediator.Send(new DeleteRequest(id));
|
|---|
| 91 | return Ok(response);
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|