From 3509a2b1f2c197c3d0f6f65a1e03102f93361ebc Mon Sep 17 00:00:00 2001 From: Marc Vaccaro Date: Wed, 23 Jan 2019 22:32:09 -0800 Subject: [PATCH] Allow users to delete profile photos --- .../photo-editor/photo-editor.component.html | 2 +- .../photo-editor/photo-editor.component.ts | 13 ++++++ Client/src/app/services/user.service.ts | 4 ++ Controllers/PhotosController.cs | 40 +++++++++++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) diff --git a/Client/src/app/components/members/photo-editor/photo-editor.component.html b/Client/src/app/components/members/photo-editor/photo-editor.component.html index b523bd7..e8fd30b 100644 --- a/Client/src/app/components/members/photo-editor/photo-editor.component.html +++ b/Client/src/app/components/members/photo-editor/photo-editor.component.html @@ -8,7 +8,7 @@ [ngClass]="photo.isMain ? 'btn-success active' : 'btn-secondary'" [disabled]="photo.isMain">Main - + diff --git a/Client/src/app/components/members/photo-editor/photo-editor.component.ts b/Client/src/app/components/members/photo-editor/photo-editor.component.ts index bd518fc..7e9945e 100644 --- a/Client/src/app/components/members/photo-editor/photo-editor.component.ts +++ b/Client/src/app/components/members/photo-editor/photo-editor.component.ts @@ -74,4 +74,17 @@ export class PhotoEditorComponent implements OnInit { }) } + deletePhoto(photoId: number) { + this.alertify.confirm("Are you sure you want to delete this photo?", () => { + const userId = this.authService.decodedToken.nameid; + this.userService.deletePhoto(userId, photoId) + .subscribe(() => { + const index = this.photos.findIndex(p => p.id == photoId); + this.photos.splice(index, 1); + this.alertify.success("Photo deleted"); + }, error => { + this.alertify.error(error); + }) + }); + } } diff --git a/Client/src/app/services/user.service.ts b/Client/src/app/services/user.service.ts index 594dd62..48fd15a 100644 --- a/Client/src/app/services/user.service.ts +++ b/Client/src/app/services/user.service.ts @@ -27,4 +27,8 @@ export class UserService { setMainPhoto(userId: number, photoId: number) { return this.http.post(`${this.usersUrl}${userId}/photos/${photoId}/setMain`, {}); } + + deletePhoto(userId: number, photoId: number) { + return this.http.delete(`${this.usersUrl}${userId}/photos/${photoId}`); + } } diff --git a/Controllers/PhotosController.cs b/Controllers/PhotosController.cs index 4d38ad3..7fef164 100644 --- a/Controllers/PhotosController.cs +++ b/Controllers/PhotosController.cs @@ -141,5 +141,45 @@ public async Task SetMainPhoto(int userId, int photoId) return BadRequest("Something went wrong"); } + + [HttpDelete("{photoId}")] + public async Task DeletePhoto(int userId, int photoId) + { + int id = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); + + if (id != userId) + { + return Unauthorized(); + } + + var user = await _repo.GetUser(userId); + + if (!user.Photos.Any(x => x.Id == photoId)) + { + return Unauthorized(); + } + + var photo = await _repo.GetPhoto(photoId); + + if (photo.IsMain) + { + return BadRequest("Cannot delete main photo"); + } + + if (!string.IsNullOrWhiteSpace(photo.PublicId)) + { + DeletionParams deletionParams = new DeletionParams(photo.PublicId); + var deleteResult = _cloudAccount.Destroy(deletionParams); + } + + _repo.Delete(photo); + + if (await _repo.SaveChangesAsync()) + { + return Ok(); + } + + return BadRequest("Something went wrong"); + } } }