From 038637339e38b731a23fca56dbac384e48248466 Mon Sep 17 00:00:00 2001 From: pancakes Date: Mon, 23 Dec 2024 00:38:27 +1000 Subject: [PATCH] [backend/api] Add endpoint to move DriveFile and change move DriveFolder to POST --- .../Controllers/Web/DriveController.cs | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/Iceshrimp.Backend/Controllers/Web/DriveController.cs b/Iceshrimp.Backend/Controllers/Web/DriveController.cs index 5ffb8f52..90ca9a88 100644 --- a/Iceshrimp.Backend/Controllers/Web/DriveController.cs +++ b/Iceshrimp.Backend/Controllers/Web/DriveController.cs @@ -234,6 +234,33 @@ public class DriveController( return StatusCode(StatusCodes.Status202Accepted); } + + [HttpPost("{id}/move")] + [Authenticate] + [Authorize] + [ProducesResults(HttpStatusCode.OK)] + [ProducesErrors(HttpStatusCode.NotFound)] + public async Task UpdateFileParent(string id, DriveMoveRequest request) + { + var user = HttpContext.GetUserOrFail(); + + var file = await db.DriveFiles + .FirstOrDefaultAsync(p => p.Id == id && p.UserId == user.Id) + ?? throw GracefulException.RecordNotFound(); + + if (request.FolderId != null) + { + var parent = await db.DriveFolders + .FirstOrDefaultAsync(p => p.Id == request.FolderId && p.UserId == user.Id); + if (parent == null) + throw GracefulException.NotFound("The new parent folder doesn't exist"); + } + + file.FolderId = request.FolderId; + await db.SaveChangesAsync(); + + return await GetFileById(file.Id); + } [HttpGet("folder")] [Authenticate] @@ -373,7 +400,7 @@ public class DriveController( await db.SaveChangesAsync(); } - [HttpPut("folder/{id}/move")] + [HttpPost("folder/{id}/move")] [Authenticate] [Authorize] [ProducesResults(HttpStatusCode.OK)] @@ -381,7 +408,7 @@ public class DriveController( public async Task UpdateFolderParent(string id, DriveMoveRequest request) { var user = HttpContext.GetUserOrFail(); - + if (request.FolderId == id) throw GracefulException.BadRequest("Cannot move a folder into itself");