Skip to content

Commit

Permalink
Remove using exceptions when records not found, refactor tests, and u…
Browse files Browse the repository at this point in the history
…pdate dependencies
  • Loading branch information
jonathanpotts committed Jun 12, 2024
1 parent 7c8a3d2 commit 9435a92
Show file tree
Hide file tree
Showing 37 changed files with 391 additions and 337 deletions.
2 changes: 1 addition & 1 deletion src/AI/AI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.16" />
<PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.17" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
Expand Down
2 changes: 1 addition & 1 deletion src/AIDataGenerator/AIDataGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="SkiaSharp" Version="2.88.8" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.8" />
<PackageReference Include="Spectre.Console" Version="0.49.0" />
<PackageReference Include="Spectre.Console" Version="0.49.1" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/Application.Contracts/Services/ICuisineService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public Task<CuisineDto> CreateAsync(
ClaimsPrincipal user,
CancellationToken cancellationToken = default);

public Task<CuisineDto> UpdateAsync(
public Task<CuisineDto?> UpdateAsync(
int id,
CreateUpdateCuisineDto dto,
ClaimsPrincipal user,
CancellationToken cancellationToken = default);

public Task DeleteAsync(
public Task<bool> DeleteAsync(
int id,
ClaimsPrincipal user,
CancellationToken cancellationToken = default);
Expand Down
10 changes: 5 additions & 5 deletions src/Application.Contracts/Services/IRecipeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ public Task<PagedResult<RecipeWithCuisineDto>> GetListAsync(
long id,
CancellationToken cancellationToken = default);

public Task<string> GetCoverImageAsync(
public Task<string?> GetCoverImageAsync(
long id,
CancellationToken cancellationToken = default);

public Task UpdateCoverImageAsync(
public Task<bool> UpdateCoverImageAsync(
long id,
Stream imageData,
string? description,
ClaimsPrincipal user,
CancellationToken cancellationToken = default);

public Task DeleteCoverImageAsync(
public Task<bool> DeleteCoverImageAsync(
long id,
ClaimsPrincipal user,
CancellationToken cancellationToken = default);
Expand All @@ -39,13 +39,13 @@ public Task<RecipeWithCuisineDto> CreateAsync(
ClaimsPrincipal user,
CancellationToken cancellationToken = default);

public Task<RecipeWithCuisineDto> UpdateAsync(
public Task<RecipeWithCuisineDto?> UpdateAsync(
long id,
CreateUpdateRecipeDto dto,
ClaimsPrincipal user,
CancellationToken cancellationToken = default);

public Task DeleteAsync(
public Task<bool> DeleteAsync(
long id,
ClaimsPrincipal user,
CancellationToken cancellationToken = default);
Expand Down
6 changes: 3 additions & 3 deletions src/Application/Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="11.9.1" />
<PackageReference Include="IdGen" Version="3.0.5" />
<PackageReference Include="IdGen.DependencyInjection" Version="3.0.5" />
<PackageReference Include="FluentValidation" Version="11.9.2" />
<PackageReference Include="IdGen" Version="3.0.7" />
<PackageReference Include="IdGen.DependencyInjection" Version="3.0.7" />
<PackageReference Include="Markdig" Version="0.37.0" />
<PackageReference Include="SkiaSharp" Version="2.88.8" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.8" />
Expand Down
31 changes: 20 additions & 11 deletions src/Application/Services/CuisineService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Security;
using System.Security.Claims;
using System.Security.Claims;
using FluentValidation;
using JonathanPotts.RecipeCatalog.Application.Authorization;
using JonathanPotts.RecipeCatalog.Application.Contracts.Models;
Expand Down Expand Up @@ -53,7 +52,7 @@ public async Task<CuisineDto> CreateAsync(

if (!authResult.Succeeded)
{
throw new SecurityException(
throw new UnauthorizedAccessException(
$"User is unauthorized to perform {nameof(Operations.Create)} operation on resource.");
}

Expand All @@ -72,16 +71,20 @@ public async Task<CuisineDto> CreateAsync(
return cuisine.ToCuisineDto();
}

public async Task<CuisineDto> UpdateAsync(
public async Task<CuisineDto?> UpdateAsync(
int id,
CreateUpdateCuisineDto dto,
ClaimsPrincipal user,
CancellationToken cancellationToken = default)
{
new CreateUpdateCuisineDtoValidator().ValidateAndThrow(dto);

var cuisine = await context.Cuisines.FindAsync([id], cancellationToken)
?? throw new KeyNotFoundException();
var cuisine = await context.Cuisines.FindAsync([id], cancellationToken);

if (cuisine == null)
{
return null;
}

var authResult = await authorizationService.AuthorizeAsync(
user,
Expand All @@ -90,7 +93,7 @@ public async Task<CuisineDto> UpdateAsync(

if (!authResult.Succeeded)
{
throw new SecurityException(
throw new UnauthorizedAccessException(
$"User is unauthorized to perform {nameof(Operations.Update)} operation on resource.");
}

Expand All @@ -109,13 +112,17 @@ public async Task<CuisineDto> UpdateAsync(
return cuisine.ToCuisineDto();
}

public async Task DeleteAsync(
public async Task<bool> DeleteAsync(
int id,
ClaimsPrincipal user,
CancellationToken cancellationToken = default)
{
var cuisine = await context.Cuisines.FindAsync([id], cancellationToken)
?? throw new KeyNotFoundException();
var cuisine = await context.Cuisines.FindAsync([id], cancellationToken);

if (cuisine == null)
{
return false;
}

var authResult = await authorizationService.AuthorizeAsync(
user,
Expand All @@ -124,7 +131,7 @@ public async Task DeleteAsync(

if (!authResult.Succeeded)
{
throw new SecurityException(
throw new UnauthorizedAccessException(
$"User is unauthorized to perform {nameof(Operations.Delete)} operation on resource.");
}

Expand All @@ -139,5 +146,7 @@ public async Task DeleteAsync(
await context.Entry(cuisine).ReloadAsync(cancellationToken);
throw;
}

return true;
}
}
74 changes: 49 additions & 25 deletions src/Application/Services/RecipeService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Numerics.Tensors;
using System.Security;
using System.Security.Claims;
using FluentValidation;
using IdGen;
Expand Down Expand Up @@ -103,33 +102,36 @@ public async Task<PagedResult<RecipeWithCuisineDto>> GetListAsync(
return recipe?.ToRecipeWithCuisineDto();
}

public async Task<string> GetCoverImageAsync(
public async Task<string?> GetCoverImageAsync(
long id,
CancellationToken cancellationToken = default)
{
var recipe = await context.Recipes
.AsNoTracking()
.FirstOrDefaultAsync(x => x.Id == id, cancellationToken)
?? throw new KeyNotFoundException();
.FirstOrDefaultAsync(x => x.Id == id, cancellationToken);

if (string.IsNullOrEmpty(recipe.CoverImage?.Url))
if (recipe == null || string.IsNullOrEmpty(recipe.CoverImage?.Url))
{
throw new KeyNotFoundException();
return null;
}

return Path.Combine(s_imagesDirectory, recipe.CoverImage.Url);
}

public async Task UpdateCoverImageAsync(
public async Task<bool> UpdateCoverImageAsync(
long id,
Stream imageData,
string? description,
ClaimsPrincipal user,
CancellationToken cancellationToken = default)
{
var recipe = await context.Recipes
.FirstOrDefaultAsync(x => x.Id == id, cancellationToken)
?? throw new KeyNotFoundException();
.FirstOrDefaultAsync(x => x.Id == id, cancellationToken);

if (recipe == null)
{
return false;
}

var authResult = await authorizationService.AuthorizeAsync(
user,
Expand All @@ -138,7 +140,7 @@ public async Task UpdateCoverImageAsync(

if (!authResult.Succeeded)
{
throw new SecurityException(
throw new UnauthorizedAccessException(
$"User is unauthorized to perform {nameof(Operations.Update)} operation on resource.");
}

Expand Down Expand Up @@ -184,15 +186,21 @@ await File.WriteAllBytesAsync(
await context.Entry(recipe).ReloadAsync(cancellationToken);
throw;
}

return true;
}

public async Task DeleteCoverImageAsync(
public async Task<bool> DeleteCoverImageAsync(
long id,
ClaimsPrincipal user,
CancellationToken cancellationToken = default)
{
var recipe = await context.Recipes.FindAsync([id], cancellationToken)
?? throw new KeyNotFoundException();
var recipe = await context.Recipes.FindAsync([id], cancellationToken);

if (recipe == null)
{
return false;
}

var authResult = await authorizationService.AuthorizeAsync(
user,
Expand All @@ -201,12 +209,16 @@ public async Task DeleteCoverImageAsync(

if (!authResult.Succeeded)
{
throw new SecurityException(
throw new UnauthorizedAccessException(
$"User is unauthorized to perform {nameof(Operations.Update)} operation on resource.");
}

var coverImage = recipe.CoverImage?.Url
?? throw new KeyNotFoundException();
var coverImage = recipe.CoverImage?.Url;

if (coverImage == null)
{
return false;
}

recipe.CoverImage = null;

Expand All @@ -224,6 +236,8 @@ public async Task DeleteCoverImageAsync(
{
File.Delete(Path.Combine(s_imagesDirectory, coverImage));
}

return true;
}

public async Task<RecipeWithCuisineDto> CreateAsync(
Expand All @@ -242,7 +256,7 @@ public async Task<RecipeWithCuisineDto> CreateAsync(

if (!authResult.Succeeded)
{
throw new SecurityException(
throw new UnauthorizedAccessException(
$"User is unauthorized to perform {nameof(Operations.Create)} operation on resource.");
}

Expand Down Expand Up @@ -286,16 +300,20 @@ public async Task<RecipeWithCuisineDto> CreateAsync(
return recipe.ToRecipeWithCuisineDto();
}

public async Task<RecipeWithCuisineDto> UpdateAsync(
public async Task<RecipeWithCuisineDto?> UpdateAsync(
long id,
CreateUpdateRecipeDto dto,
ClaimsPrincipal user,
CancellationToken cancellationToken = default)
{
new CreateUpdateRecipeDtoValidator().ValidateAndThrow(dto);

var recipe = await context.Recipes.FindAsync([id], cancellationToken)
?? throw new KeyNotFoundException();
var recipe = await context.Recipes.FindAsync([id], cancellationToken);

if (recipe == null)
{
return null;
}

var authResult = await authorizationService.AuthorizeAsync(
user,
Expand All @@ -304,7 +322,7 @@ public async Task<RecipeWithCuisineDto> UpdateAsync(

if (!authResult.Succeeded)
{
throw new SecurityException(
throw new UnauthorizedAccessException(
$"User is unauthorized to perform {nameof(Operations.Update)} operation on resource.");
}

Expand Down Expand Up @@ -346,13 +364,17 @@ public async Task<RecipeWithCuisineDto> UpdateAsync(
return recipe.ToRecipeWithCuisineDto();
}

public async Task DeleteAsync(
public async Task<bool> DeleteAsync(
long id,
ClaimsPrincipal user,
CancellationToken cancellationToken = default)
{
var recipe = await context.Recipes.FindAsync([id], cancellationToken)
?? throw new KeyNotFoundException();
var recipe = await context.Recipes.FindAsync([id], cancellationToken);

if (recipe == null)
{
return false;
}

var authResult = await authorizationService.AuthorizeAsync(
user,
Expand All @@ -361,7 +383,7 @@ public async Task DeleteAsync(

if (!authResult.Succeeded)
{
throw new SecurityException(
throw new UnauthorizedAccessException(
$"User is unauthorized to perform {nameof(Operations.Delete)} operation on resource.");
}

Expand All @@ -388,6 +410,8 @@ public async Task DeleteAsync(
File.Delete(imagePath);
}
}

return true;
}

public async Task<PagedResult<RecipeWithCuisineDto>> SearchAsync(
Expand Down
6 changes: 3 additions & 3 deletions src/BlazorApp.Client/BlazorApp.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="8.0.4" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="8.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="8.0.6" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="8.0.6" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 9435a92

Please sign in to comment.