-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProjectsController.cs
73 lines (61 loc) · 2.64 KB
/
ProjectsController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using BugLab.API.Extensions;
using BugLab.Business.Commands.Projects;
using BugLab.Business.Interfaces;
using BugLab.Business.Queries.Projects;
using BugLab.Data.Extensions;
using BugLab.Shared.QueryParams;
using BugLab.Shared.Requests.Projects;
using BugLab.Shared.Responses;
using Mapster;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace BugLab.API.Controllers
{
public class ProjectsController : BaseApiController
{
private readonly IAuthService _authService;
public ProjectsController(IMediator mediator, IAuthService authService) : base(mediator)
{
_authService = authService;
}
[HttpGet("{id}", Name = nameof(GetProject))]
public async Task<ActionResult<ProjectResponse>> GetProject(int id, CancellationToken cancellationToken)
{
var project = await _mediator.Send(new GetProjectQuery(id), cancellationToken);
return project;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<ProjectResponse>>> GetProjects([FromQuery] PaginationParams queryParams, CancellationToken cancellationToken)
{
var query = new GetProjectsQuery(User.UserId());
queryParams.Adapt(query);
var projects = await _mediator.Send(query, cancellationToken);
Response.AddPaginationHeader(projects.PageNumber, projects.PageSize, projects.TotalPages, projects.TotalItems);
return projects;
}
[HttpPost]
public async Task<IActionResult> AddProject(AddProjectRequest request, CancellationToken cancellationToken)
{
var id = await _mediator.Send(new AddProjectCommand(User.UserId(), request.Title, request.Description), cancellationToken);
return CreatedAtRoute(nameof(GetProject), new { id }, id);
}
[HttpPut("{id}")]
public async Task<IActionResult> UpdateProject(int id, UpdateProjectRequest request, CancellationToken cancellationToken)
{
await _authService.HasAccessToProject(User.UserId(), id);
await _mediator.Send(new UpdateProjectCommand(id, request.Title, request.Description), cancellationToken);
return NoContent();
}
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteProject(int id, CancellationToken cancellationToken)
{
await _authService.HasAccessToProject(User.UserId(), id);
await _mediator.Send(new DeleteProjectCommand(id), cancellationToken);
return NoContent();
}
}
}