mirror of
https://github.com/bitwarden/server
synced 2025-12-17 08:43:27 +00:00
Move delete many complexity to service
This commit is contained in:
@@ -6,16 +6,12 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
namespace Bit.SeederApi.Controllers;
|
namespace Bit.SeederApi.Controllers;
|
||||||
|
|
||||||
[Route("seed")]
|
[Route("seed")]
|
||||||
public class SeedController(
|
public class SeedController(ILogger<SeedController> logger, ISceneService sceneService) : Controller
|
||||||
ILogger<SeedController> logger,
|
|
||||||
ISceneService sceneService,
|
|
||||||
IServiceProvider serviceProvider) : Controller
|
|
||||||
{
|
{
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<IActionResult> SeedAsync([FromBody] SeedRequestModel request)
|
public async Task<IActionResult> SeedAsync([FromBody] SeedRequestModel request)
|
||||||
{
|
{
|
||||||
logger.LogInformation("Received seed request {Provider}", serviceProvider.GetType().FullName);
|
logger.LogInformation("Received seed request with template: {Template}", request.Template);
|
||||||
logger.LogInformation("Seeding with template: {Template}", request.Template);
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -39,34 +35,19 @@ public class SeedController(
|
|||||||
{
|
{
|
||||||
logger.LogInformation("Deleting batch of seeded data with IDs: {PlayIds}", string.Join(", ", playIds));
|
logger.LogInformation("Deleting batch of seeded data with IDs: {PlayIds}", string.Join(", ", playIds));
|
||||||
|
|
||||||
var aggregateException = new AggregateException();
|
|
||||||
|
|
||||||
await Task.Run(async () =>
|
|
||||||
{
|
|
||||||
foreach (var playId in playIds)
|
|
||||||
{
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await sceneService.DestroyScene(playId);
|
await sceneService.DestroyScenes(playIds);
|
||||||
|
return Ok(new { Message = "Batch delete completed successfully" });
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (AggregateException ex)
|
||||||
{
|
|
||||||
aggregateException = new AggregateException(aggregateException, ex);
|
|
||||||
logger.LogError(ex, "Error deleting seeded data: {SeedId}", playId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (aggregateException.InnerExceptions.Count > 0)
|
|
||||||
{
|
{
|
||||||
return BadRequest(new
|
return BadRequest(new
|
||||||
{
|
{
|
||||||
Error = "One or more errors occurred while deleting seeded data",
|
Error = ex.Message,
|
||||||
Details = aggregateException.InnerExceptions.Select(e => e.Message).ToList()
|
Details = ex.InnerExceptions.Select(e => e.Message).ToList()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(new { Message = "Batch delete completed successfully" });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpDelete("{playId}")]
|
[HttpDelete("{playId}")]
|
||||||
@@ -93,34 +74,20 @@ public class SeedController(
|
|||||||
{
|
{
|
||||||
logger.LogInformation("Deleting all seeded data");
|
logger.LogInformation("Deleting all seeded data");
|
||||||
|
|
||||||
// Pull all Seeded Data ids
|
|
||||||
|
|
||||||
var playIds = sceneService.GetAllPlayIds();
|
var playIds = sceneService.GetAllPlayIds();
|
||||||
|
|
||||||
var aggregateException = new AggregateException();
|
|
||||||
|
|
||||||
foreach (var playId in playIds)
|
|
||||||
{
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await sceneService.DestroyScene(playId);
|
await sceneService.DestroyScenes(playIds);
|
||||||
|
return NoContent();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (AggregateException ex)
|
||||||
{
|
|
||||||
aggregateException = new AggregateException(aggregateException, ex);
|
|
||||||
logger.LogError(ex, "Error deleting seeded data: {PlayId}", playId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (aggregateException.InnerExceptions.Count > 0)
|
|
||||||
{
|
{
|
||||||
return BadRequest(new
|
return BadRequest(new
|
||||||
{
|
{
|
||||||
Error = "One or more errors occurred while deleting seeded data",
|
Error = ex.Message,
|
||||||
Details = aggregateException.InnerExceptions.Select(e => e.Message).ToList()
|
Details = ex.InnerExceptions.Select(e => e.Message).ToList()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return NoContent();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,4 +36,11 @@ public interface ISceneService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>A list of play IDs representing active seeded data that can be destroyed.</returns>
|
/// <returns>A list of play IDs representing active seeded data that can be destroyed.</returns>
|
||||||
List<string> GetAllPlayIds();
|
List<string> GetAllPlayIds();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Destroys multiple scenes by their play IDs.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="playIds">The list of play IDs to destroy</param>
|
||||||
|
/// <exception cref="AggregateException">Thrown when one or more scenes fail to destroy</exception>
|
||||||
|
Task DestroyScenes(IEnumerable<string> playIds);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,6 +79,34 @@ public class SceneService(
|
|||||||
return new { PlayId = playId };
|
return new { PlayId = playId };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task DestroyScenes(IEnumerable<string> playIds)
|
||||||
|
{
|
||||||
|
var exceptions = new List<Exception>();
|
||||||
|
|
||||||
|
var deleteTasks = playIds.Select(async playId =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await DestroyScene(playId);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
lock (exceptions)
|
||||||
|
{
|
||||||
|
exceptions.Add(ex);
|
||||||
|
}
|
||||||
|
logger.LogError(ex, "Error deleting seeded data: {PlayId}", playId);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await Task.WhenAll(deleteTasks);
|
||||||
|
|
||||||
|
if (exceptions.Count > 0)
|
||||||
|
{
|
||||||
|
throw new AggregateException("One or more errors occurred while deleting seeded data", exceptions);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async Task<SceneResult<object?>> ExecuteSceneMethod(string templateName, JsonElement? arguments, string methodName)
|
private async Task<SceneResult<object?>> ExecuteSceneMethod(string templateName, JsonElement? arguments, string methodName)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|||||||
Reference in New Issue
Block a user