Adding delete for spawnables

This commit is contained in:
hz
2025-06-08 21:54:57 -05:00
parent 2ed8aaffc6
commit d16ecac876
3 changed files with 38 additions and 0 deletions
+12
View File
@@ -40,4 +40,16 @@ public class SpawnableController : ControllerBase
var all = await _spawnableService.GetAllAsync(); var all = await _spawnableService.GetAllAsync();
return Ok(all); return Ok(all);
} }
[HttpDelete("{id}")]
[Authorize(Roles = "SUPER")]
public async Task<IActionResult> Delete(string id)
{
var spawnable = await _spawnableService.GetByIdAsync(id);
if (spawnable == null)
return NotFound("Spawnable not found.");
await _spawnableService.DeleteAsync(id);
return Ok($"Spawnable '{spawnable.Name}' deleted.");
}
} }
@@ -54,4 +54,16 @@ public class SpawnableControllerTests
var ok = Assert.IsType<OkObjectResult>(result); var ok = Assert.IsType<OkObjectResult>(result);
Assert.Equal(list, ok.Value); Assert.Equal(list, ok.Value);
} }
[Fact]
public async Task Delete_NotFound()
{
var svc = new Mock<SpawnableService>(config);
svc.Setup(s => s.GetByIdAsync("id")).ReturnsAsync((Spawnable)null);
var controller = new SpawnableController(svc.Object);
var result = await controller.Delete("id");
Assert.IsType<NotFoundObjectResult>(result);
}
} }
+14
View File
@@ -3,6 +3,7 @@ using System.Threading.Tasks;
using GameAuthApi.Models; using GameAuthApi.Models;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using MongoDB.Driver; using MongoDB.Driver;
using MongoDB.Bson;
namespace GameAuthApi.Services; namespace GameAuthApi.Services;
@@ -26,4 +27,17 @@ public class SpawnableService
{ {
return await _spawnables.Find(_ => true).ToListAsync(); return await _spawnables.Find(_ => true).ToListAsync();
} }
public virtual async Task<Spawnable> GetByIdAsync(string id)
{
if (!ObjectId.TryParse(id, out _))
return null;
return await _spawnables.Find(s => s.Id == id).FirstOrDefaultAsync();
}
public virtual async Task DeleteAsync(string id)
{
await _spawnables.DeleteOneAsync(s => s.Id == id);
}
} }