mirror of
https://github.com/bitwarden/server
synced 2025-12-17 08:43:27 +00:00
Prefer Repositories for Delete of entities
This commit is contained in:
@@ -66,13 +66,13 @@ public class SeedController : Controller
|
||||
}
|
||||
|
||||
[HttpDelete("/seed/{seedId}")]
|
||||
public IActionResult Delete([FromRoute] Guid seedId)
|
||||
public async Task<IActionResult> Delete([FromRoute] Guid seedId)
|
||||
{
|
||||
_logger.LogInformation("Deleting seeded data with ID: {SeedId}", seedId);
|
||||
|
||||
try
|
||||
{
|
||||
var result = _recipeService.DestroyRecipe(seedId);
|
||||
var result = await _recipeService.DestroyRecipe(seedId);
|
||||
|
||||
return Ok(new
|
||||
{
|
||||
@@ -110,13 +110,13 @@ public class SeedController : Controller
|
||||
|
||||
var aggregateException = new AggregateException();
|
||||
|
||||
await Task.Run(() =>
|
||||
await Task.Run(async () =>
|
||||
{
|
||||
foreach (var sd in seededData)
|
||||
{
|
||||
try
|
||||
{
|
||||
_recipeService.DestroyRecipe(sd.Id);
|
||||
await _recipeService.DestroyRecipe(sd.Id);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -21,6 +21,6 @@ public interface IRecipeService
|
||||
/// <param name="seedId">The ID of the seeded data to destroy</param>
|
||||
/// <returns>The result of the destroy operation</returns>
|
||||
/// <exception cref="RecipeExecutionException">Thrown when there's an error destroying the seeded data</exception>
|
||||
object? DestroyRecipe(Guid seedId);
|
||||
Task<object?> DestroyRecipe(Guid seedId);
|
||||
List<SeededData> GetAllSeededData();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
using Bit.Infrastructure.EntityFramework.Repositories;
|
||||
using Bit.Seeder;
|
||||
@@ -11,12 +12,16 @@ public class RecipeService : IRecipeService
|
||||
private readonly DatabaseContext _databaseContext;
|
||||
private readonly ILogger<RecipeService> _logger;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly IUserRepository _userRepository;
|
||||
private readonly IOrganizationRepository _organizationRepository;
|
||||
|
||||
public RecipeService(DatabaseContext databaseContext, ILogger<RecipeService> logger, IServiceProvider serviceProvider)
|
||||
public RecipeService(DatabaseContext databaseContext, ILogger<RecipeService> logger, IServiceProvider serviceProvider, IUserRepository userRepository, IOrganizationRepository organizationRepository)
|
||||
{
|
||||
_databaseContext = databaseContext;
|
||||
_logger = logger;
|
||||
_serviceProvider = serviceProvider;
|
||||
_userRepository = userRepository;
|
||||
_organizationRepository = organizationRepository;
|
||||
}
|
||||
|
||||
public List<SeededData> GetAllSeededData()
|
||||
@@ -55,7 +60,7 @@ public class RecipeService : IRecipeService
|
||||
return (Result: recipeResult.Result, SeedId: seededData.Id);
|
||||
}
|
||||
|
||||
public object? DestroyRecipe(Guid seedId)
|
||||
public async Task<object?> DestroyRecipe(Guid seedId)
|
||||
{
|
||||
var seededData = _databaseContext.SeededData.FirstOrDefault(s => s.Id == seedId);
|
||||
if (seededData == null)
|
||||
@@ -73,13 +78,30 @@ public class RecipeService : IRecipeService
|
||||
if (trackedEntities.TryGetValue("User", out var userIds))
|
||||
{
|
||||
var users = _databaseContext.Users.Where(u => userIds.Contains(u.Id));
|
||||
_databaseContext.RemoveRange(users);
|
||||
await _userRepository.DeleteManyAsync(users);
|
||||
}
|
||||
|
||||
if (trackedEntities.TryGetValue("Organization", out var orgIds))
|
||||
{
|
||||
var organizations = _databaseContext.Organizations.Where(o => orgIds.Contains(o.Id));
|
||||
_databaseContext.RemoveRange(organizations);
|
||||
var aggregateException = new AggregateException();
|
||||
foreach (var org in organizations)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _organizationRepository.DeleteAsync(org);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
aggregateException = new AggregateException(aggregateException, ex);
|
||||
}
|
||||
}
|
||||
if (aggregateException.InnerExceptions.Count > 0)
|
||||
{
|
||||
throw new RecipeExecutionException(
|
||||
$"One or more errors occurred while deleting organizations for seed ID {seedId}",
|
||||
aggregateException);
|
||||
}
|
||||
}
|
||||
|
||||
_databaseContext.Remove(seededData);
|
||||
|
||||
Reference in New Issue
Block a user