Esempio n. 1
0
        public async Task <IActionResult> DeleteImageById([FromRoute][ValidateGuid] string imageId)
        {
            var image = await _imagesService.GetByIdAsync(imageId);

            if (image == null)
            {
                var notFoundError = new ApiError("Image with such Id was not found.", HttpStatusCode.NotFound);
                return(NotFound(notFoundError));
            }
            var authResult = await _authorizationService.AuthorizeAsync(User, image, "SameOrAdminUser");

            if (!authResult.Succeeded)
            {
                var authError = new ApiError("You are not permitted to delete this image.", HttpStatusCode.Unauthorized);
                return(StatusCode(StatusCodes.Status403Forbidden, authError));
            }

            var result = await _imagesService.DeleteByIdAsync(imageId);

            if (result)
            {
                return(Ok());
            }

            var error = new ApiError("Error deleting image.", HttpStatusCode.InternalServerError);

            return(StatusCode(500, error));
        }