Skip to content

Commit

Permalink
Allow users to delete profile photos
Browse files Browse the repository at this point in the history
  • Loading branch information
marvac committed Jan 24, 2019
1 parent 6d632ab commit 3509a2b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[ngClass]="photo.isMain ? 'btn-success active' : 'btn-secondary'"
[disabled]="photo.isMain">Main</button>

<button type="button" class="btn btn-xs btn-danger"><i class="fa fa-trash-o"></i></button>
<button type="button" class="btn btn-xs btn-danger" (click)="deletePhoto(photo.id)" [disabled]="photo.isMain"><i class="fa fa-trash-o"></i></button>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
});
}
}
4 changes: 4 additions & 0 deletions Client/src/app/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
}
40 changes: 40 additions & 0 deletions Controllers/PhotosController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,45 @@ public async Task<ActionResult> SetMainPhoto(int userId, int photoId)

return BadRequest("Something went wrong");
}

[HttpDelete("{photoId}")]
public async Task<IActionResult> 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");
}
}
}

0 comments on commit 3509a2b

Please sign in to comment.