source: ChapterX.API/Controllers/CollaborationsController.cs@ 0b502c2

main
Last change on this file since 0b502c2 was 7fbb91c, checked in by kikisrbinoska <srbinoskakristina07@…>, 3 months ago

Added functional collaboration between users

  • Property mode set to 100644
File size: 3.2 KB
Line 
1using ChapterX.Application.Collaboration.Commands;
2using ChapterX.Application.Collaboration.Queries;
3using ChapterX.Domain.Repositories;
4using MediatR;
5using Microsoft.AspNetCore.Authorization;
6using Microsoft.AspNetCore.Mvc;
7using Microsoft.Extensions.Logging;
8
9namespace ChapterX.API.Controllers
10{
11 [Route("api/[controller]")]
12 [ApiController]
13 public class CollaborationsController : ControllerBase
14 {
15 private readonly IMediator _mediator;
16 private readonly ICollaborationRepository _collaborationRepository;
17 private readonly ILogger<CollaborationsController> _logger;
18
19 public CollaborationsController(IMediator mediator, ICollaborationRepository collaborationRepository, ILogger<CollaborationsController> logger)
20 {
21 _mediator = mediator;
22 _collaborationRepository = collaborationRepository;
23 _logger = logger;
24 }
25
26 [HttpGet]
27 [AllowAnonymous]
28 public async Task<ActionResult> GetAll()
29 {
30 _logger.LogInformation("Fetching all collaborations");
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();
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
Note: See TracBrowser for help on using the repository browser.