From 6ae765733a524f21d83144849b9f2aac792a253d Mon Sep 17 00:00:00 2001 From: maxime Date: Mon, 25 Mar 2024 22:16:30 +0100 Subject: [PATCH] add remove tactic route --- API/Controllers/TacticsController.cs | 14 ++++++++++++++ DbServices/DbTacticService.cs | 11 +++++++++++ Services/ITacticService.cs | 1 + 3 files changed, 26 insertions(+) diff --git a/API/Controllers/TacticsController.cs b/API/Controllers/TacticsController.cs index 9b5582a..916eacf 100644 --- a/API/Controllers/TacticsController.cs +++ b/API/Controllers/TacticsController.cs @@ -160,4 +160,18 @@ public class TacticController(ITacticService service, IContextAccessor accessor) var userId = accessor.CurrentUserId(HttpContext); return new CanEditResponse(await service.IsOwnerOf(userId, tacticId)); } + + + [HttpDelete("/tactics/{tacticId:int}")] + [Authorize] + public async Task RemoveTactic(int tacticId) + { + var userId = accessor.CurrentUserId(HttpContext); + if (!await service.IsOwnerOf(userId, tacticId)) + { + return Unauthorized(); + } + + return await service.RemoveTactic(tacticId) ? Ok() : NotFound(); + } } \ No newline at end of file diff --git a/DbServices/DbTacticService.cs b/DbServices/DbTacticService.cs index 7f02808..a4dbed1 100644 --- a/DbServices/DbTacticService.cs +++ b/DbServices/DbTacticService.cs @@ -195,4 +195,15 @@ public class DbTacticService(AppContext.AppContext context) : ITacticService context.SharedTactics.Remove(sharedTactic); return await context.SaveChangesAsync() > 0; } + + public async Task RemoveTactic(int tacticId) + { + var removed = await context.Tactics.Where(t => t.Id == tacticId).ExecuteDeleteAsync() > 0; + if (!removed) + return false; + + await context.TacticSteps.Where(s => s.TacticId == tacticId).ExecuteDeleteAsync(); + + return true; + } } \ No newline at end of file diff --git a/Services/ITacticService.cs b/Services/ITacticService.cs index 2a80b64..3f15f47 100644 --- a/Services/ITacticService.cs +++ b/Services/ITacticService.cs @@ -98,4 +98,5 @@ public interface ITacticService /// A task that represents the asynchronous operation. The task result contains a boolean indicating whether the removal was successful. Task RemoveTacticStep(int tacticId, int stepId); + Task RemoveTactic(int tacticId); } \ No newline at end of file