public async Task <IActionResult> Delete(int id) { _logger.LogInformation("Received image post delete request."); // Get the user making the request. User user = _currentUserService.GetCurrentUser(HttpContext); _logger.LogInformation("Requesting user email: {0}", user.Email); _logger.LogInformation("Image post id: {0}", id); // Find the image to delete. ImagePost imageToDelete = await _imagePostService.GetById(id); if (imageToDelete == null) { _logger.LogError("Image post with id {0} does not exist.", id); return(NotFound()); } // Check if user does not own image. if (imageToDelete.UserEmail != user.Email) { _logger.LogError("User '{0}' does not own image post with id {1}", user.Email, id); return(Forbid()); } // Delete the image record. if (!await _imagePostService.Delete(id)) { _logger.LogError("Image post with id {0} does not exist.", id); return(NotFound()); } // Delete the image from storage. if (!await _imageStorage.DeleteImage(imageToDelete.ImageUri)) { _logger.LogError("Failed to delete stored image at '{0}'.", imageToDelete.ImageUri); return(BadRequest()); } // Publish event. _logger.LogInformation("Publishing image post deleted notification."); await _notificationService.Publish(new ImagePostDeletedNotification(id)); _logger.LogInformation("Successfully deleted image post with id {0}.", id); return(NoContent()); }