-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0641604
commit 053399f
Showing
4 changed files
with
130 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
using BHC24.Api.Extensions; | ||
using BHC24.Api.Models; | ||
using BHC24.Api.Models.Projects; | ||
using BHC24.Api.Persistence; | ||
using BHC24.Api.Persistence.Models; | ||
using BHC24.Api.Services; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace BHC24.Api.Controllers; | ||
|
||
[ApiController] | ||
[Authorize] | ||
[Route("api/[controller]")] | ||
public class ProjectController : ControllerBase | ||
{ | ||
private readonly BhcDbContext _dbContext; | ||
private readonly AuthUserProvider _authUser; | ||
|
||
public ProjectController(BhcDbContext dbContext, AuthUserProvider authUser) | ||
{ | ||
_dbContext = dbContext; | ||
_authUser = authUser; | ||
} | ||
|
||
[HttpGet, AllowAnonymous] | ||
public async Task<IActionResult> GetList([FromQuery] PaginationRequest request) | ||
{ | ||
var projects = await _dbContext.Projects | ||
.Select(p => new GetProjectResponse | ||
{ | ||
Title = p.Title, | ||
Description = p.Description, | ||
Owner = p.Owner.UserName, | ||
Collaborators = p.Collaborators.Select(c => c.UserName), | ||
}) | ||
.PaginateAsync(request); | ||
|
||
return Ok(projects); | ||
} | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> Add(AddProjectRequest request) | ||
{ | ||
var user = await _authUser.GetAsync(); | ||
|
||
var project = new Project | ||
{ | ||
Title = request.Title, | ||
Description = request.Description, | ||
Owner = user | ||
}; | ||
|
||
_dbContext.Projects.Add(project); | ||
await _dbContext.SaveChangesAsync(); | ||
|
||
return Ok(); | ||
} | ||
|
||
[HttpPut("{id}")] | ||
public async Task<IActionResult> Update(int id, AddProjectRequest request) | ||
{ | ||
var project = await _dbContext.Projects | ||
.Include(p => p.Owner) | ||
.Include(p => p.Collaborators) | ||
.FirstOrDefaultAsync(p => p.Id == id); | ||
|
||
if (project is null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
if (project.Owner.Id != (await _authUser.GetAsync()).Id) | ||
{ | ||
return Forbid(); | ||
} | ||
|
||
project.Title = request.Title; | ||
project.Description = request.Description; | ||
|
||
await _dbContext.SaveChangesAsync(); | ||
|
||
return Ok(); | ||
} | ||
|
||
[HttpDelete("{id}")] | ||
public async Task<IActionResult> Delete(int id) | ||
{ | ||
var project = await _dbContext.Projects | ||
.Include(p => p.Owner) | ||
.Include(p => p.Collaborators) | ||
.FirstOrDefaultAsync(p => p.Id == id); | ||
|
||
if (project == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
if (project.Owner.Id != (await _authUser.GetAsync()).Id) | ||
{ | ||
return Forbid(); | ||
} | ||
|
||
_dbContext.Projects.Remove(project); | ||
await _dbContext.SaveChangesAsync(); | ||
|
||
return Ok(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace BHC24.Api.Models.Projects; | ||
|
||
public record GetProjectResponse | ||
{ | ||
public string Title { get; init; } | ||
public string Description { get; init; } | ||
public string Owner { get; init; } | ||
public IEnumerable<string> Collaborators { get; init; } | ||
} | ||
|
||
public record AddProjectRequest(string Title, string Description); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters