mirror of
https://github.com/bitwarden/server
synced 2025-12-17 08:43:27 +00:00
Change to primary constructor
This commit is contained in:
@@ -13,25 +13,17 @@ public class SeedRequestModel
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Route("")]
|
[Route("")]
|
||||||
public class SeedController : Controller
|
public class SeedController(ILogger<SeedController> logger, IRecipeService recipeService)
|
||||||
|
: Controller
|
||||||
{
|
{
|
||||||
private readonly ILogger<SeedController> _logger;
|
|
||||||
private readonly IRecipeService _recipeService;
|
|
||||||
|
|
||||||
public SeedController(ILogger<SeedController> logger, IRecipeService recipeService)
|
|
||||||
{
|
|
||||||
_logger = logger;
|
|
||||||
_recipeService = recipeService;
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpPost("/seed")]
|
[HttpPost("/seed")]
|
||||||
public IActionResult Seed([FromBody] SeedRequestModel request)
|
public IActionResult Seed([FromBody] SeedRequestModel request)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Seeding with template: {Template}", request.Template);
|
logger.LogInformation("Seeding with template: {Template}", request.Template);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var (result, seedId) = _recipeService.ExecuteRecipe(request.Template, request.Arguments);
|
var (result, seedId) = recipeService.ExecuteRecipe(request.Template, request.Arguments);
|
||||||
|
|
||||||
return Ok(new
|
return Ok(new
|
||||||
{
|
{
|
||||||
@@ -47,7 +39,7 @@ public class SeedController : Controller
|
|||||||
}
|
}
|
||||||
catch (RecipeExecutionException ex)
|
catch (RecipeExecutionException ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error executing recipe: {Template}", request.Template);
|
logger.LogError(ex, "Error executing recipe: {Template}", request.Template);
|
||||||
return BadRequest(new
|
return BadRequest(new
|
||||||
{
|
{
|
||||||
Error = ex.Message,
|
Error = ex.Message,
|
||||||
@@ -56,7 +48,7 @@ public class SeedController : Controller
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Unexpected error seeding with template: {Template}", request.Template);
|
logger.LogError(ex, "Unexpected error seeding with template: {Template}", request.Template);
|
||||||
return StatusCode(500, new
|
return StatusCode(500, new
|
||||||
{
|
{
|
||||||
Error = "An unexpected error occurred while seeding",
|
Error = "An unexpected error occurred while seeding",
|
||||||
@@ -68,7 +60,7 @@ public class SeedController : Controller
|
|||||||
[HttpDelete("/seed/batch")]
|
[HttpDelete("/seed/batch")]
|
||||||
public async Task<IActionResult> DeleteBatch([FromBody] List<Guid> seedIds)
|
public async Task<IActionResult> DeleteBatch([FromBody] List<Guid> seedIds)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Deleting batch of seeded data with IDs: {SeedIds}", string.Join(", ", seedIds));
|
logger.LogInformation("Deleting batch of seeded data with IDs: {SeedIds}", string.Join(", ", seedIds));
|
||||||
|
|
||||||
var aggregateException = new AggregateException();
|
var aggregateException = new AggregateException();
|
||||||
|
|
||||||
@@ -78,12 +70,12 @@ public class SeedController : Controller
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _recipeService.DestroyRecipe(seedId);
|
await recipeService.DestroyRecipe(seedId);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
aggregateException = new AggregateException(aggregateException, ex);
|
aggregateException = new AggregateException(aggregateException, ex);
|
||||||
_logger.LogError(ex, "Error deleting seeded data: {SeedId}", seedId);
|
logger.LogError(ex, "Error deleting seeded data: {SeedId}", seedId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -105,11 +97,11 @@ public class SeedController : Controller
|
|||||||
[HttpDelete("/seed/{seedId}")]
|
[HttpDelete("/seed/{seedId}")]
|
||||||
public async Task<IActionResult> Delete([FromRoute] Guid seedId)
|
public async Task<IActionResult> Delete([FromRoute] Guid seedId)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Deleting seeded data with ID: {SeedId}", seedId);
|
logger.LogInformation("Deleting seeded data with ID: {SeedId}", seedId);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var result = await _recipeService.DestroyRecipe(seedId);
|
var result = await recipeService.DestroyRecipe(seedId);
|
||||||
|
|
||||||
return Ok(new
|
return Ok(new
|
||||||
{
|
{
|
||||||
@@ -119,7 +111,7 @@ public class SeedController : Controller
|
|||||||
}
|
}
|
||||||
catch (RecipeExecutionException ex)
|
catch (RecipeExecutionException ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error deleting seeded data: {SeedId}", seedId);
|
logger.LogError(ex, "Error deleting seeded data: {SeedId}", seedId);
|
||||||
return BadRequest(new
|
return BadRequest(new
|
||||||
{
|
{
|
||||||
Error = ex.Message,
|
Error = ex.Message,
|
||||||
@@ -128,7 +120,7 @@ public class SeedController : Controller
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Unexpected error deleting seeded data: {SeedId}", seedId);
|
logger.LogError(ex, "Unexpected error deleting seeded data: {SeedId}", seedId);
|
||||||
return StatusCode(500, new
|
return StatusCode(500, new
|
||||||
{
|
{
|
||||||
Error = "An unexpected error occurred while deleting",
|
Error = "An unexpected error occurred while deleting",
|
||||||
@@ -141,10 +133,10 @@ public class SeedController : Controller
|
|||||||
[HttpDelete("/seed")]
|
[HttpDelete("/seed")]
|
||||||
public async Task<IActionResult> DeleteAll()
|
public async Task<IActionResult> DeleteAll()
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Deleting all seeded data");
|
logger.LogInformation("Deleting all seeded data");
|
||||||
|
|
||||||
// Pull all Seeded Data ids
|
// Pull all Seeded Data ids
|
||||||
var seededData = _recipeService.GetAllSeededData();
|
var seededData = recipeService.GetAllSeededData();
|
||||||
|
|
||||||
var aggregateException = new AggregateException();
|
var aggregateException = new AggregateException();
|
||||||
|
|
||||||
@@ -154,12 +146,12 @@ public class SeedController : Controller
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _recipeService.DestroyRecipe(sd.Id);
|
await recipeService.DestroyRecipe(sd.Id);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
aggregateException = new AggregateException(aggregateException, ex);
|
aggregateException = new AggregateException(aggregateException, ex);
|
||||||
_logger.LogError(ex, "Error deleting seeded data: {SeedId}", sd.Id);
|
logger.LogError(ex, "Error deleting seeded data: {SeedId}", sd.Id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -7,26 +7,17 @@ using Bit.Seeder;
|
|||||||
|
|
||||||
namespace Bit.SeederApi.Services;
|
namespace Bit.SeederApi.Services;
|
||||||
|
|
||||||
public class RecipeService : IRecipeService
|
public class RecipeService(
|
||||||
|
DatabaseContext databaseContext,
|
||||||
|
ILogger<RecipeService> logger,
|
||||||
|
IServiceProvider serviceProvider,
|
||||||
|
IUserRepository userRepository,
|
||||||
|
IOrganizationRepository organizationRepository)
|
||||||
|
: 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, IUserRepository userRepository, IOrganizationRepository organizationRepository)
|
|
||||||
{
|
|
||||||
_databaseContext = databaseContext;
|
|
||||||
_logger = logger;
|
|
||||||
_serviceProvider = serviceProvider;
|
|
||||||
_userRepository = userRepository;
|
|
||||||
_organizationRepository = organizationRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<SeededData> GetAllSeededData()
|
public List<SeededData> GetAllSeededData()
|
||||||
{
|
{
|
||||||
return _databaseContext.SeededData.ToList();
|
return databaseContext.SeededData.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public (object? Result, Guid? SeedId) ExecuteRecipe(string templateName, JsonElement? arguments)
|
public (object? Result, Guid? SeedId) ExecuteRecipe(string templateName, JsonElement? arguments)
|
||||||
@@ -51,10 +42,10 @@ public class RecipeService : IRecipeService
|
|||||||
CreationDate = DateTime.UtcNow
|
CreationDate = DateTime.UtcNow
|
||||||
};
|
};
|
||||||
|
|
||||||
_databaseContext.Add(seededData);
|
databaseContext.Add(seededData);
|
||||||
_databaseContext.SaveChanges();
|
databaseContext.SaveChanges();
|
||||||
|
|
||||||
_logger.LogInformation("Saved seeded data with ID {SeedId} for recipe {RecipeName}",
|
logger.LogInformation("Saved seeded data with ID {SeedId} for recipe {RecipeName}",
|
||||||
seededData.Id, templateName);
|
seededData.Id, templateName);
|
||||||
|
|
||||||
return (Result: recipeResult.Result, SeedId: seededData.Id);
|
return (Result: recipeResult.Result, SeedId: seededData.Id);
|
||||||
@@ -62,10 +53,10 @@ public class RecipeService : IRecipeService
|
|||||||
|
|
||||||
public async Task<object?> DestroyRecipe(Guid seedId)
|
public async Task<object?> DestroyRecipe(Guid seedId)
|
||||||
{
|
{
|
||||||
var seededData = _databaseContext.SeededData.FirstOrDefault(s => s.Id == seedId);
|
var seededData = databaseContext.SeededData.FirstOrDefault(s => s.Id == seedId);
|
||||||
if (seededData == null)
|
if (seededData == null)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("No seeded data found with ID {SeedId}, skipping", seedId);
|
logger.LogInformation("No seeded data found with ID {SeedId}, skipping", seedId);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,19 +69,19 @@ public class RecipeService : IRecipeService
|
|||||||
// Delete in reverse order to respect foreign key constraints
|
// Delete in reverse order to respect foreign key constraints
|
||||||
if (trackedEntities.TryGetValue("User", out var userIds))
|
if (trackedEntities.TryGetValue("User", out var userIds))
|
||||||
{
|
{
|
||||||
var users = _databaseContext.Users.Where(u => userIds.Contains(u.Id));
|
var users = databaseContext.Users.Where(u => userIds.Contains(u.Id));
|
||||||
await _userRepository.DeleteManyAsync(users);
|
await userRepository.DeleteManyAsync(users);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (trackedEntities.TryGetValue("Organization", out var orgIds))
|
if (trackedEntities.TryGetValue("Organization", out var orgIds))
|
||||||
{
|
{
|
||||||
var organizations = _databaseContext.Organizations.Where(o => orgIds.Contains(o.Id));
|
var organizations = databaseContext.Organizations.Where(o => orgIds.Contains(o.Id));
|
||||||
var aggregateException = new AggregateException();
|
var aggregateException = new AggregateException();
|
||||||
foreach (var org in organizations)
|
foreach (var org in organizations)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _organizationRepository.DeleteAsync(org);
|
await organizationRepository.DeleteAsync(org);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -105,10 +96,10 @@ public class RecipeService : IRecipeService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_databaseContext.Remove(seededData);
|
databaseContext.Remove(seededData);
|
||||||
_databaseContext.SaveChanges();
|
databaseContext.SaveChanges();
|
||||||
|
|
||||||
_logger.LogInformation("Successfully destroyed seeded data with ID {SeedId} for recipe {RecipeName}",
|
logger.LogInformation("Successfully destroyed seeded data with ID {SeedId} for recipe {RecipeName}",
|
||||||
seedId, seededData.RecipeName);
|
seedId, seededData.RecipeName);
|
||||||
|
|
||||||
return new { SeedId = seedId, RecipeName = seededData.RecipeName };
|
return new { SeedId = seedId, RecipeName = seededData.RecipeName };
|
||||||
@@ -125,12 +116,12 @@ public class RecipeService : IRecipeService
|
|||||||
var methodArguments = ParseMethodArguments(method, arguments);
|
var methodArguments = ParseMethodArguments(method, arguments);
|
||||||
var result = method.Invoke(recipeInstance, methodArguments);
|
var result = method.Invoke(recipeInstance, methodArguments);
|
||||||
|
|
||||||
_logger.LogInformation("Successfully executed {MethodName} on recipe: {TemplateName}", methodName, templateName);
|
logger.LogInformation("Successfully executed {MethodName} on recipe: {TemplateName}", methodName, templateName);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
catch (Exception ex) when (ex is not RecipeNotFoundException and not RecipeExecutionException)
|
catch (Exception ex) when (ex is not RecipeNotFoundException and not RecipeExecutionException)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Unexpected error executing {MethodName} on recipe: {TemplateName}", methodName, templateName);
|
logger.LogError(ex, "Unexpected error executing {MethodName} on recipe: {TemplateName}", methodName, templateName);
|
||||||
throw new RecipeExecutionException(
|
throw new RecipeExecutionException(
|
||||||
$"An unexpected error occurred while executing {methodName} on recipe '{templateName}'",
|
$"An unexpected error occurred while executing {methodName} on recipe '{templateName}'",
|
||||||
ex.InnerException ?? ex);
|
ex.InnerException ?? ex);
|
||||||
@@ -168,7 +159,7 @@ public class RecipeService : IRecipeService
|
|||||||
for (var i = 0; i < parameters.Length; i++)
|
for (var i = 0; i < parameters.Length; i++)
|
||||||
{
|
{
|
||||||
var parameter = parameters[i];
|
var parameter = parameters[i];
|
||||||
var service = _serviceProvider.GetService(parameter.ParameterType);
|
var service = serviceProvider.GetService(parameter.ParameterType);
|
||||||
|
|
||||||
if (service == null)
|
if (service == null)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user