mirror of
https://github.com/bitwarden/server
synced 2025-12-16 08:13:33 +00:00
Stricter scene and query types
SeederAPI only serves Scenes, Recipes are inteded to be used locally only.
This commit is contained in:
@@ -1,37 +1,36 @@
|
||||
using Bit.SeederApi.Models.Requests;
|
||||
using Bit.SeederApi.Models.Requests;
|
||||
using Bit.SeederApi.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Bit.SeederApi.Controllers
|
||||
namespace Bit.SeederApi.Controllers;
|
||||
|
||||
[Route("query")]
|
||||
public class QueryController(ILogger<QueryController> logger, ISeedService recipeService)
|
||||
: Controller
|
||||
{
|
||||
[Route("query")]
|
||||
public class QueryController(ILogger<QueryController> logger, IRecipeService recipeService)
|
||||
: Controller
|
||||
[HttpPost]
|
||||
public IActionResult Query([FromBody] QueryRequestModel request)
|
||||
{
|
||||
[HttpPost]
|
||||
public IActionResult Query([FromBody] QueryRequestModel request)
|
||||
logger.LogInformation("Executing query: {Query}", request.Template);
|
||||
|
||||
try
|
||||
{
|
||||
logger.LogInformation("Executing query: {Query}", request.Template);
|
||||
var result = recipeService.ExecuteQuery(request.Template, request.Arguments);
|
||||
|
||||
try
|
||||
return Json(new { Result = result });
|
||||
}
|
||||
catch (RecipeNotFoundException ex)
|
||||
{
|
||||
return NotFound(new { Error = ex.Message });
|
||||
}
|
||||
catch (RecipeExecutionException ex)
|
||||
{
|
||||
logger.LogError(ex, "Error executing query: {Query}", request.Template);
|
||||
return BadRequest(new
|
||||
{
|
||||
var result = recipeService.ExecuteQuery(request.Template, request.Arguments);
|
||||
|
||||
return Json(new { Result = result });
|
||||
}
|
||||
catch (RecipeNotFoundException ex)
|
||||
{
|
||||
return NotFound(new { Error = ex.Message });
|
||||
}
|
||||
catch (RecipeExecutionException ex)
|
||||
{
|
||||
logger.LogError(ex, "Error executing query: {Query}", request.Template);
|
||||
return BadRequest(new
|
||||
{
|
||||
Error = ex.Message,
|
||||
Details = ex.InnerException?.Message
|
||||
});
|
||||
}
|
||||
Error = ex.Message,
|
||||
Details = ex.InnerException?.Message
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,139 +1,133 @@
|
||||
using Bit.SeederApi.Models.Requests;
|
||||
using Bit.SeederApi.Models.Response;
|
||||
using Bit.SeederApi.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Bit.SeederApi.Controllers
|
||||
namespace Bit.SeederApi.Controllers;
|
||||
|
||||
[Route("seed")]
|
||||
public class SeedController(ILogger<SeedController> logger, ISeedService recipeService)
|
||||
: Controller
|
||||
{
|
||||
[Route("seed")]
|
||||
public class SeedController(ILogger<SeedController> logger, IRecipeService recipeService)
|
||||
: Controller
|
||||
[HttpPost]
|
||||
public IActionResult Seed([FromBody] SeedRequestModel request)
|
||||
{
|
||||
[HttpPost]
|
||||
public IActionResult Seed([FromBody] SeedRequestModel request)
|
||||
logger.LogInformation("Seeding with template: {Template}", request.Template);
|
||||
|
||||
try
|
||||
{
|
||||
logger.LogInformation("Seeding with template: {Template}", request.Template);
|
||||
var response = recipeService.ExecuteScene(request.Template, request.Arguments);
|
||||
|
||||
try
|
||||
{
|
||||
var (result, seedId) = recipeService.ExecuteRecipe(request.Template, request.Arguments);
|
||||
|
||||
return Json(new SeedResponseModel
|
||||
{
|
||||
SeedId = seedId,
|
||||
Result = result,
|
||||
});
|
||||
}
|
||||
catch (RecipeNotFoundException ex)
|
||||
{
|
||||
return NotFound(new { Error = ex.Message });
|
||||
}
|
||||
catch (RecipeExecutionException ex)
|
||||
{
|
||||
logger.LogError(ex, "Error executing scene: {Template}", request.Template);
|
||||
return BadRequest(new
|
||||
{
|
||||
Error = ex.Message,
|
||||
Details = ex.InnerException?.Message
|
||||
});
|
||||
}
|
||||
return Json(response);
|
||||
}
|
||||
|
||||
[HttpDelete("batch")]
|
||||
public async Task<IActionResult> DeleteBatch([FromBody] List<Guid> seedIds)
|
||||
catch (RecipeNotFoundException ex)
|
||||
{
|
||||
logger.LogInformation("Deleting batch of seeded data with IDs: {SeedIds}", string.Join(", ", seedIds));
|
||||
|
||||
var aggregateException = new AggregateException();
|
||||
|
||||
await Task.Run(async () =>
|
||||
{
|
||||
foreach (var seedId in seedIds)
|
||||
{
|
||||
try
|
||||
{
|
||||
await recipeService.DestroyRecipe(seedId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
aggregateException = new AggregateException(aggregateException, ex);
|
||||
logger.LogError(ex, "Error deleting seeded data: {SeedId}", seedId);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (aggregateException.InnerExceptions.Count > 0)
|
||||
{
|
||||
return BadRequest(new
|
||||
{
|
||||
Error = "One or more errors occurred while deleting seeded data",
|
||||
Details = aggregateException.InnerExceptions.Select(e => e.Message).ToList()
|
||||
});
|
||||
}
|
||||
return Ok(new
|
||||
{
|
||||
Message = "Batch delete completed successfully"
|
||||
});
|
||||
return NotFound(new { Error = ex.Message });
|
||||
}
|
||||
|
||||
[HttpDelete("{seedId}")]
|
||||
public async Task<IActionResult> Delete([FromRoute] Guid seedId)
|
||||
catch (RecipeExecutionException ex)
|
||||
{
|
||||
logger.LogInformation("Deleting seeded data with ID: {SeedId}", seedId);
|
||||
|
||||
try
|
||||
logger.LogError(ex, "Error executing scene: {Template}", request.Template);
|
||||
return BadRequest(new
|
||||
{
|
||||
var result = await recipeService.DestroyRecipe(seedId);
|
||||
|
||||
return Json(result);
|
||||
}
|
||||
catch (RecipeExecutionException ex)
|
||||
{
|
||||
logger.LogError(ex, "Error deleting seeded data: {SeedId}", seedId);
|
||||
return BadRequest(new
|
||||
{
|
||||
Error = ex.Message,
|
||||
Details = ex.InnerException?.Message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[HttpDelete]
|
||||
public async Task<IActionResult> DeleteAll()
|
||||
{
|
||||
logger.LogInformation("Deleting all seeded data");
|
||||
|
||||
// Pull all Seeded Data ids
|
||||
var seededData = recipeService.GetAllSeededData();
|
||||
|
||||
var aggregateException = new AggregateException();
|
||||
|
||||
await Task.Run(async () =>
|
||||
{
|
||||
foreach (var sd in seededData)
|
||||
{
|
||||
try
|
||||
{
|
||||
await recipeService.DestroyRecipe(sd.Id);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
aggregateException = new AggregateException(aggregateException, ex);
|
||||
logger.LogError(ex, "Error deleting seeded data: {SeedId}", sd.Id);
|
||||
}
|
||||
}
|
||||
Error = ex.Message,
|
||||
Details = ex.InnerException?.Message
|
||||
});
|
||||
|
||||
if (aggregateException.InnerExceptions.Count > 0)
|
||||
{
|
||||
return BadRequest(new
|
||||
{
|
||||
Error = "One or more errors occurred while deleting seeded data",
|
||||
Details = aggregateException.InnerExceptions.Select(e => e.Message).ToList()
|
||||
});
|
||||
}
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
|
||||
[HttpDelete("batch")]
|
||||
public async Task<IActionResult> DeleteBatch([FromBody] List<Guid> seedIds)
|
||||
{
|
||||
logger.LogInformation("Deleting batch of seeded data with IDs: {SeedIds}", string.Join(", ", seedIds));
|
||||
|
||||
var aggregateException = new AggregateException();
|
||||
|
||||
await Task.Run(async () =>
|
||||
{
|
||||
foreach (var seedId in seedIds)
|
||||
{
|
||||
try
|
||||
{
|
||||
await recipeService.DestroyRecipe(seedId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
aggregateException = new AggregateException(aggregateException, ex);
|
||||
logger.LogError(ex, "Error deleting seeded data: {SeedId}", seedId);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (aggregateException.InnerExceptions.Count > 0)
|
||||
{
|
||||
return BadRequest(new
|
||||
{
|
||||
Error = "One or more errors occurred while deleting seeded data",
|
||||
Details = aggregateException.InnerExceptions.Select(e => e.Message).ToList()
|
||||
});
|
||||
}
|
||||
return Ok(new
|
||||
{
|
||||
Message = "Batch delete completed successfully"
|
||||
});
|
||||
}
|
||||
|
||||
[HttpDelete("{seedId}")]
|
||||
public async Task<IActionResult> Delete([FromRoute] Guid seedId)
|
||||
{
|
||||
logger.LogInformation("Deleting seeded data with ID: {SeedId}", seedId);
|
||||
|
||||
try
|
||||
{
|
||||
var result = await recipeService.DestroyRecipe(seedId);
|
||||
|
||||
return Json(result);
|
||||
}
|
||||
catch (RecipeExecutionException ex)
|
||||
{
|
||||
logger.LogError(ex, "Error deleting seeded data: {SeedId}", seedId);
|
||||
return BadRequest(new
|
||||
{
|
||||
Error = ex.Message,
|
||||
Details = ex.InnerException?.Message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[HttpDelete]
|
||||
public async Task<IActionResult> DeleteAll()
|
||||
{
|
||||
logger.LogInformation("Deleting all seeded data");
|
||||
|
||||
// Pull all Seeded Data ids
|
||||
var seededData = recipeService.GetAllSeededData();
|
||||
|
||||
var aggregateException = new AggregateException();
|
||||
|
||||
await Task.Run(async () =>
|
||||
{
|
||||
foreach (var sd in seededData)
|
||||
{
|
||||
try
|
||||
{
|
||||
await recipeService.DestroyRecipe(sd.Id);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
aggregateException = new AggregateException(aggregateException, ex);
|
||||
logger.LogError(ex, "Error deleting seeded data: {SeedId}", sd.Id);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (aggregateException.InnerExceptions.Count > 0)
|
||||
{
|
||||
return BadRequest(new
|
||||
{
|
||||
Error = "One or more errors occurred while deleting seeded data",
|
||||
Details = aggregateException.InnerExceptions.Select(e => e.Message).ToList()
|
||||
});
|
||||
}
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user