From dc2175d6e47980a3e6f4566699025e398b165367 Mon Sep 17 00:00:00 2001 From: jvyden Date: Sat, 13 Nov 2021 07:03:30 -0500 Subject: [PATCH] Add Delete Photo endpoint --- ProjectLighthouse/Controllers/PhotosController.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ProjectLighthouse/Controllers/PhotosController.cs b/ProjectLighthouse/Controllers/PhotosController.cs index 7255fc6c..d5559365 100644 --- a/ProjectLighthouse/Controllers/PhotosController.cs +++ b/ProjectLighthouse/Controllers/PhotosController.cs @@ -115,5 +115,20 @@ namespace LBPUnion.ProjectLighthouse.Controllers return this.Ok(LbpSerializer.StringElement("photos", response)); } + + [HttpPost("deletePhoto/{id:int}")] + public async Task DeletePhoto(int id) + { + User? user = await this.database.UserFromRequest(this.Request); + if (user == null) return this.StatusCode(403, ""); + + Photo? photo = await this.database.Photos.FirstOrDefaultAsync(p => p.PhotoId == id); + if (photo == null) return this.NotFound(); + if (photo.CreatorId != user.UserId) return this.StatusCode(401, ""); + + this.database.Photos.Remove(photo); + await this.database.SaveChangesAsync(); + return this.Ok(); + } } } \ No newline at end of file