-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an API for creating shared items
- Loading branch information
1 parent
8099426
commit 2237178
Showing
27 changed files
with
805 additions
and
18 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,29 @@ | ||
# This workflow will build a .NET project | ||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net | ||
|
||
name: Api Tests | ||
|
||
on: | ||
push: | ||
branches-ignore: [main] | ||
|
||
jobs: | ||
api-test: | ||
runs-on: ubuntu-latest | ||
services: | ||
postgres: | ||
image: postgres:16 | ||
env: | ||
POSTGRES_PASSWORD: postgres | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Setup .NET | ||
uses: actions/setup-dotnet@v4 | ||
with: | ||
dotnet-version: 8.0.x | ||
- name: Restore dependencies | ||
run: dotnet restore | ||
working-directory: ./tests/LiftLog.Tests.Api | ||
- name: Test | ||
run: dotnet test --no-restore --verbosity normal | ||
working-directory: ./tests/LiftLog.Tests.Api |
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,79 @@ | ||
using System.Security.Cryptography.X509Certificates; | ||
using FluentValidation; | ||
using LiftLog.Api.Db; | ||
using LiftLog.Api.Models; | ||
using LiftLog.Api.Service; | ||
using LiftLog.Lib.Models; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace LiftLog.Api.Controllers; | ||
|
||
[ApiController] | ||
public class SharedItemController( | ||
UserDataContext db, | ||
PasswordService passwordService, | ||
IdEncodingService idEncodingService | ||
) : ControllerBase | ||
{ | ||
[Route("[controller]")] | ||
[HttpPost] | ||
public async Task<IActionResult> CreateShared( | ||
CreateSharedItemRequest request, | ||
[FromServices] IValidator<CreateSharedItemRequest> validator | ||
) | ||
{ | ||
var validationResult = await validator.ValidateAsync(request); | ||
if (!validationResult.IsValid) | ||
{ | ||
return BadRequest(validationResult.Errors); | ||
} | ||
var user = await db.Users.FindAsync(request.UserId); | ||
if (user == null) | ||
{ | ||
return Unauthorized(); | ||
} | ||
|
||
if (!passwordService.VerifyPassword(request.Password, user.HashedPassword, user.Salt)) | ||
{ | ||
return Unauthorized(); | ||
} | ||
|
||
var sharedItem = new SharedItem | ||
{ | ||
UserId = request.UserId, | ||
EncryptedPayload = request.EncryptedPayload, | ||
EncryptionIV = request.EncryptionIV, | ||
Expiry = request.Expiry, | ||
}; | ||
|
||
await db.SharedItems.AddAsync(sharedItem); | ||
await db.SaveChangesAsync(); | ||
return Ok(new CreateSharedItemResponse(Id: idEncodingService.EncodeId(sharedItem.Id))); | ||
} | ||
|
||
[Route("[controller]/{id}")] | ||
[HttpGet] | ||
public async Task<IActionResult> GetSharedItem(string id) | ||
{ | ||
if (!idEncodingService.TryDecodeId(id, out var idNumber)) | ||
{ | ||
return NotFound(); | ||
} | ||
var sharedItem = await db | ||
.SharedItems.Include(x => x.User) | ||
.FirstOrDefaultAsync(x => x.Id == idNumber); | ||
if (sharedItem == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
return Ok( | ||
new GetSharedItemResponse( | ||
RsaPublicKey: new Lib.Services.RsaPublicKey(sharedItem.User.RsaPublicKey), | ||
EncryptedPayload: sharedItem.EncryptedPayload, | ||
EncryptionIV: sharedItem.EncryptionIV | ||
) | ||
); | ||
} | ||
} |
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
Oops, something went wrong.