[backend/api] Add conflict check to UpdateFileParent

This commit is contained in:
pancakes 2025-01-30 22:02:49 +10:00 committed by Laura Hausmann
parent 957e84eab7
commit 6322d5fb91
No known key found for this signature in database
GPG key ID: D044E84C5BE01605

View file

@ -241,7 +241,8 @@ public class DriveController(
[Authenticate]
[Authorize]
[ProducesResults(HttpStatusCode.OK)]
[ProducesErrors(HttpStatusCode.NotFound)]
[ProducesErrors(HttpStatusCode.NotFound, HttpStatusCode.Conflict)]
[SuppressMessage("Performance", "CA1862", Justification = "string.Equals() cannot be used in DbSet LINQ operations")]
public async Task<DriveFileResponse> UpdateFileParent(string id, DriveMoveRequest request)
{
var user = HttpContext.GetUserOrFail();
@ -258,6 +259,13 @@ public class DriveController(
throw GracefulException.NotFound("The new parent folder doesn't exist");
}
var existing = await db.DriveFiles
.AnyAsync(p => p.Name.ToLower() == file.Name.ToLower()
&& p.FolderId == request.FolderId
&& p.UserId == user.Id);
if (existing)
throw GracefulException.Conflict("A file with this name already exists in this folder");
file.FolderId = request.FolderId;
await db.SaveChangesAsync();