mirror of
https://github.com/bitwarden/server
synced 2025-12-15 15:53:59 +00:00
Extract logic into service
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
using Bit.Infrastructure.EntityFramework.Repositories;
|
||||
using Bit.SeederApi.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Bit.SeederApi.Controllers;
|
||||
@@ -17,12 +16,12 @@ public class SeedRequestModel
|
||||
public class SeedController : Controller
|
||||
{
|
||||
private readonly ILogger<SeedController> _logger;
|
||||
private readonly DatabaseContext _databaseContext;
|
||||
private readonly IRecipeService _recipeService;
|
||||
|
||||
public SeedController(ILogger<SeedController> logger, DatabaseContext databaseContext)
|
||||
public SeedController(ILogger<SeedController> logger, IRecipeService recipeService)
|
||||
{
|
||||
_logger = logger;
|
||||
_databaseContext = databaseContext;
|
||||
_recipeService = recipeService;
|
||||
}
|
||||
|
||||
[HttpPost("/seed")]
|
||||
@@ -32,87 +31,35 @@ public class SeedController : Controller
|
||||
|
||||
try
|
||||
{
|
||||
// Find the recipe class
|
||||
var recipeTypeName = $"Bit.Seeder.Recipes.{request.Template}";
|
||||
var recipeType = Assembly.Load("Seeder")
|
||||
.GetTypes()
|
||||
.FirstOrDefault(t => t.FullName == recipeTypeName);
|
||||
|
||||
if (recipeType == null)
|
||||
{
|
||||
return NotFound(new { Error = $"Recipe '{request.Template}' not found" });
|
||||
}
|
||||
|
||||
// Instantiate the recipe with DatabaseContext
|
||||
var recipeInstance = Activator.CreateInstance(recipeType, _databaseContext);
|
||||
if (recipeInstance == null)
|
||||
{
|
||||
return StatusCode(500, new { Error = "Failed to instantiate recipe" });
|
||||
}
|
||||
|
||||
// Find the Seed method
|
||||
var seedMethod = recipeType.GetMethod("Seed");
|
||||
if (seedMethod == null)
|
||||
{
|
||||
return StatusCode(500, new { Error = $"Seed method not found in recipe '{request.Template}'" });
|
||||
}
|
||||
|
||||
// Parse arguments and match to method parameters
|
||||
var parameters = seedMethod.GetParameters();
|
||||
var arguments = new object?[parameters.Length];
|
||||
|
||||
if (request.Arguments == null && parameters.Length > 0)
|
||||
{
|
||||
return BadRequest(new { Error = "Arguments are required for this recipe" });
|
||||
}
|
||||
|
||||
for (int i = 0; i < parameters.Length; i++)
|
||||
{
|
||||
var parameter = parameters[i];
|
||||
var parameterName = parameter.Name!;
|
||||
|
||||
if (request.Arguments?.TryGetProperty(parameterName, out JsonElement value) == true)
|
||||
{
|
||||
try
|
||||
{
|
||||
arguments[i] = JsonSerializer.Deserialize(value.GetRawText(), parameter.ParameterType);
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
return BadRequest(new
|
||||
{
|
||||
Error = $"Failed to deserialize parameter '{parameterName}'",
|
||||
Details = ex.Message
|
||||
});
|
||||
}
|
||||
}
|
||||
else if (!parameter.HasDefaultValue)
|
||||
{
|
||||
return BadRequest(new { Error = $"Missing required parameter: {parameterName}" });
|
||||
}
|
||||
else
|
||||
{
|
||||
arguments[i] = parameter.DefaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
// Invoke the Seed method
|
||||
var result = seedMethod.Invoke(recipeInstance, arguments);
|
||||
var result = _recipeService.ExecuteRecipe(request.Template, request.Arguments);
|
||||
|
||||
return Ok(new
|
||||
{
|
||||
Message = "Seed completed successfully",
|
||||
Template = request.Template,
|
||||
request.Template,
|
||||
Result = result
|
||||
});
|
||||
}
|
||||
catch (RecipeNotFoundException ex)
|
||||
{
|
||||
return NotFound(new { Error = ex.Message });
|
||||
}
|
||||
catch (RecipeExecutionException ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error executing recipe: {Template}", request.Template);
|
||||
return BadRequest(new
|
||||
{
|
||||
Error = ex.Message,
|
||||
Details = ex.InnerException?.Message
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error seeding with template: {Template}", request.Template);
|
||||
_logger.LogError(ex, "Unexpected error seeding with template: {Template}", request.Template);
|
||||
return StatusCode(500, new
|
||||
{
|
||||
Error = "An error occurred while seeding",
|
||||
Details = ex.InnerException?.Message ?? ex.Message
|
||||
Error = "An unexpected error occurred while seeding",
|
||||
Details = ex.Message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user