|
| 1 | +using BugLab.Business.Commands.Projects; |
| 2 | +using BugLab.Business.Interfaces; |
| 3 | +using BugLab.Business.Queries.Projects; |
| 4 | +using BugLab.Data.Extensions; |
| 5 | +using BugLab.Shared.Responses; |
| 6 | +using MediatR; |
| 7 | +using Microsoft.AspNetCore.Mvc; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Linq; |
| 10 | +using System.Threading; |
| 11 | +using System.Threading.Tasks; |
| 12 | + |
| 13 | +namespace BugLab.API.Controllers |
| 14 | +{ |
| 15 | + [Route("api/projects/{projectId}/[controller]")] |
| 16 | + public class ProjectUsersController : BaseApiController |
| 17 | + { |
| 18 | + private readonly IAuthService _authService; |
| 19 | + |
| 20 | + public ProjectUsersController(IMediator mediator, IAuthService authService) : base(mediator) |
| 21 | + { |
| 22 | + _authService = authService; |
| 23 | + } |
| 24 | + |
| 25 | + [HttpGet] |
| 26 | + public async Task<ActionResult<IEnumerable<UserResponse>>> GetProjectUsers(int projectId, CancellationToken cancellationToken) |
| 27 | + { |
| 28 | + var users = await _mediator.Send(new GetProjectUsersQuery(projectId), cancellationToken); |
| 29 | + |
| 30 | + return Ok(users); |
| 31 | + } |
| 32 | + |
| 33 | + [HttpPost] |
| 34 | + public async Task<IActionResult> AddUsersToProject(int projectId, [FromQuery] IEnumerable<string> userIds, CancellationToken cancellationToken) |
| 35 | + { |
| 36 | + if (!userIds.Any()) return NoContent(); |
| 37 | + |
| 38 | + await _authService.HasAccessToProject(User.UserId(), projectId); |
| 39 | + await _mediator.Send(new AddProjectUsersCommand(projectId, userIds), cancellationToken); |
| 40 | + |
| 41 | + return NoContent(); |
| 42 | + } |
| 43 | + |
| 44 | + [HttpDelete("{id}")] |
| 45 | + public async Task<IActionResult> DeleteProjectUser(int projectId, string id, CancellationToken cancellationToken) |
| 46 | + { |
| 47 | + await _authService.HasAccessToProject(User.UserId(), projectId); |
| 48 | + await _mediator.Send(new DeleteProjectUserCommand(projectId, id), cancellationToken); |
| 49 | + |
| 50 | + return NoContent(); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments