-
Notifications
You must be signed in to change notification settings - Fork 9
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 fd968d7
Showing
29 changed files
with
862 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 @@ | ||
**/Migrations/**/* |
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,49 @@ | ||
# 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: password | ||
POSTGRES_DB: liftlog | ||
options: >- | ||
--health-cmd pg_isready | ||
--health-interval 10s | ||
--health-timeout 5s | ||
--health-retries 5 | ||
ports: | ||
# Maps tcp port 5432 on service container to the host | ||
- 5432:5432 | ||
env: | ||
ConnectionStrings__UserDataContext: Host=localhost;Port=5432;Database=liftlog;Username=postgres;Password=password | ||
ConnectionStrings__RateLimitContext: Host=localhost;Port=5432;Database=liftlog;Username=postgres;Password=password | ||
OpenAiApiKey: "sk-123" | ||
WebAuthApiKey: "1234" | ||
GooglePlayServiceAccountEmail: "123" | ||
GooglePlayServiceAccountKeyBase64: "123" | ||
IdEncodingService__Alphabet: "123abc" | ||
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: Build | ||
run: dotnet build | ||
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.