Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 79 additions & 1 deletion Chefs/Business/Services/Cookbooks/CookbookService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,23 @@

namespace Chefs.Business.Services.Cookbooks;

public class CookbookService(ChefsApiClient client, IMessenger messenger, IUserService userService)
public class CookbookService(ChefsApiClient client, IMessenger messenger, IUserService userService, ILogger<CookbookService> logger)
: ICookbookService
{
public async ValueTask<Cookbook> Create(string name, IImmutableList<Recipe> recipes, CancellationToken ct)
{
try
{
return await CreateCore(name, recipes, ct);
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to create cookbook {Name}", name);
throw;
}
}

private async ValueTask<Cookbook> CreateCore(string name, IImmutableList<Recipe> recipes, CancellationToken ct)
{
var currentUser = await userService.GetCurrent(ct);
var cookbookData = Cookbook.CreateData(currentUser.Id, name, recipes);
Expand All @@ -17,6 +30,19 @@ public async ValueTask<Cookbook> Create(string name, IImmutableList<Recipe> reci
}

public async ValueTask<Cookbook> Update(Cookbook cookbook, IImmutableList<Recipe> recipes, CancellationToken ct)
{
try
{
return await UpdateCore(cookbook, recipes, ct);
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to update cookbook {CookbookId} with recipes", cookbook.Id);
throw;
}
}

private async ValueTask<Cookbook> UpdateCore(Cookbook cookbook, IImmutableList<Recipe> recipes, CancellationToken ct)
{
var updatedCookbookData = cookbook.ToData(recipes);
await client.Api.Cookbook.PutAsync(updatedCookbookData, cancellationToken: ct);
Expand All @@ -27,6 +53,19 @@ public async ValueTask<Cookbook> Update(Cookbook cookbook, IImmutableList<Recipe
}

public async ValueTask Update(Cookbook cookbook, CancellationToken ct)
{
try
{
await UpdateCore(cookbook, ct);
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to update cookbook {CookbookId}", cookbook.Id);
throw;
}
}

private async ValueTask UpdateCore(Cookbook cookbook, CancellationToken ct)
{
var cookbookData = cookbook.ToData();

Expand All @@ -35,6 +74,19 @@ public async ValueTask Update(Cookbook cookbook, CancellationToken ct)
}

public async ValueTask Save(Cookbook cookbook, CancellationToken ct)
{
try
{
await SaveCore(cookbook, ct);
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to save cookbook {CookbookId}", cookbook.Id);
throw;
}
}

private async ValueTask SaveCore(Cookbook cookbook, CancellationToken ct)
{
var currentUser = await userService.GetCurrent(ct);
var cookbookData = cookbook.ToData();
Expand All @@ -48,13 +100,39 @@ await client.Api.Cookbook.Save.PostAsync(
}

public async ValueTask<IImmutableList<Cookbook>> GetSaved(CancellationToken ct)
{
try
{
return await GetSavedCore(ct);
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to get saved cookbooks");
throw;
}
}

private async ValueTask<IImmutableList<Cookbook>> GetSavedCore(CancellationToken ct)
{
var currentUser = await userService.GetCurrent(ct);
var savedCookbooksData = await client.Api.Cookbook.Saved.GetAsync(config => config.QueryParameters.UserId = currentUser.Id, cancellationToken: ct);
return savedCookbooksData?.Select(c => new Cookbook(c)).ToImmutableList() ?? ImmutableList<Cookbook>.Empty;
}

public async ValueTask<IImmutableList<Cookbook>> GetByUser(Guid userId, CancellationToken ct)
{
try
{
return await GetByUserCore(userId, ct);
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to get cookbooks by user {UserId}", userId);
throw;
}
}

private async ValueTask<IImmutableList<Cookbook>> GetByUserCore(Guid userId, CancellationToken ct)
{
var allCookbooksData = await client.Api.Cookbook.GetAsync(cancellationToken: ct);
return allCookbooksData?.Where(r => r.UserId == userId).Select(x => new Cookbook(x)).ToImmutableList() ?? ImmutableList<Cookbook>.Empty;
Expand Down
15 changes: 14 additions & 1 deletion Chefs/Business/Services/Notifications/NotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@

namespace Chefs.Business.Services.Notifications;

public class NotificationService(ChefsApiClient client) : INotificationService
public class NotificationService(ChefsApiClient client, ILogger<NotificationService> logger) : INotificationService
{
public async ValueTask<IImmutableList<Notification>> GetAll(CancellationToken ct)
{
try
{
return await GetAllCore(ct);
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to get all notifications");
throw;
}
}

private async ValueTask<IImmutableList<Notification>> GetAllCore(CancellationToken ct)
{
var notificationsData = await client.Api.Notification.GetAsync(cancellationToken: ct);
return notificationsData?.Select(n => new Notification(n)).ToImmutableList() ?? ImmutableList<Notification>.Empty;
Expand Down
Loading
Loading