Skip to content

Commit

Permalink
Add Project endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
protectedvoid21 committed Oct 12, 2024
1 parent 0641604 commit 053399f
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 2 deletions.
111 changes: 111 additions & 0 deletions src/BHC24.Api/Controllers/ProjectController.cs
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();
}
}
6 changes: 6 additions & 0 deletions src/BHC24.Api/Extensions/QueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public static async IAsyncEnumerable<TEntity> ToAsyncEnumerable<TEntity>(this IQ
}
}

public static async Task<PaginationResponse<TEntity>> PaginateAsync<TEntity>(
this IQueryable<TEntity> query, PaginationRequest request, CancellationToken ct = default)
{
return await query.PaginateAsync(request.Page, request.PageSize, ct);
}

public static async Task<PaginationResponse<TEntity>> PaginateAsync<TEntity>(
this IQueryable<TEntity> query, int page, int pageSize, CancellationToken ct = default)
{
Expand Down
11 changes: 11 additions & 0 deletions src/BHC24.Api/Models/Projects/Project.cs
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);
4 changes: 2 additions & 2 deletions src/BHC24.Api/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchBrowser": false,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5091",
"environmentVariables": {
Expand All @@ -22,7 +22,7 @@
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchBrowser": false,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7113;http://localhost:5091",
"environmentVariables": {
Expand Down

0 comments on commit 053399f

Please sign in to comment.