示例#1
0
        public async Task <IActionResult> Update(Guid imageId, [FromBody] UpdateRequestModel request)
        {
            if (request.ImageName == null)
            {
                return(BadRequest(UploadImageResultViewModel.Failure(localizer["NameNotSet"].Value, false, localizer)));
            }
            if (request.Description == null)
            {
                return(BadRequest(UploadImageResultViewModel.Failure(localizer["DescriptionNotSet"].Value, false, localizer)));
            }
            if (request.Source == null)
            {
                return(BadRequest(UploadImageResultViewModel.Failure(localizer["SourceNotSet"].Value, false, localizer)));
            }
            if (request.VersionDescription == null)
            {
                return(BadRequest(UploadImageResultViewModel.Failure(localizer["VersionDescriptionNotSet"].Value, false, localizer)));
            }

            try
            {
                var user = await userManager.GetUserAsync(HttpContext.User);

                var applicationRequest = new UpdateImageMetadata.Request(imageId, user, request.ImageName, request.Source, request.Description, request.VersionDescription);
                await new UpdateImageMetadata(dbContext, localizer).RunAsync(applicationRequest);
                var toastText = $"{localizer["SuccessfullyUpdatedImage"]} '{request.ImageName}'";
                return(Ok(new { ToastText = toastText, ToastTitle = localizer["Success"] }));
            }
            catch (Exception e)
            {
                return(ControllerError.BadRequest(e, this));
            }
        }
示例#2
0
        public async Task <IActionResult> UploadImage([FromForm] UploadImageRequest request)
        {
            if (request.Name == null)
            {
                return(BadRequest(UploadImageResultViewModel.Failure(localizer["NameNotSet"].Value, false, localizer)));
            }
            if (request.Description == null)
            {
                return(BadRequest(UploadImageResultViewModel.Failure(localizer["DescriptionNotSet"].Value, false, localizer)));
            }
            if (request.Source == null)
            {
                return(BadRequest(UploadImageResultViewModel.Failure(localizer["SourceNotSet"].Value, false, localizer)));
            }
            if (request.File == null)
            {
                return(BadRequest(UploadImageResultViewModel.Failure(localizer["FileNotSet"].Value, false, localizer)));
            }

            try
            {
                var user = await userManager.GetUserAsync(HttpContext.User);

                if (user == null)
                {
                    return(BadRequest(UploadImageResultViewModel.Failure(localizer["NeedLogin"].Value, false, localizer)));
                }

                using (var stream = request.File.OpenReadStream())
                    using (var reader = new BinaryReader(stream))
                    {
                        var fileContent        = reader.ReadBytes((int)request.File.Length);
                        var applicationRequest = new StoreImage.Request(user, request.Name, request.Description, request.Source, request.File.ContentType, fileContent);
                        var id = await new StoreImage(dbContext, localizer).RunAsync(applicationRequest);
                        if (id == Guid.Empty)
                        {
                            throw new ApplicationException("Stored image with empty GUID as id");
                        }
                        return(Ok(UploadImageResultViewModel.Success(localizer["ImageSavedWithName"].Value + $" '{applicationRequest.Name}'", localizer)));
                    }
            }
            catch (Exception e)
            {
                return(ControllerError.BadRequest(e, this));
            }
        }
示例#3
0
        public async Task <IActionResult> Delete(Guid imageId, [FromBody] DeleteRequest request)
        {
            try
            {
                if (request.DeletionDescription == null)
                {
                    return(BadRequest(UploadImageResultViewModel.Failure("DeletionDescriptionNotSet", false, localizer)));
                }

                var user = await userManager.GetUserAsync(HttpContext.User);

                var applicationRequest = new DeleteImage.Request(user, imageId, request.DeletionDescription);
                var imageName          = await new DeleteImage(dbContext, localizer).RunAsync(applicationRequest);
                var toastText          = $"{localizer["SuccessfullyDeletedImage"]} '{imageName}'";
                return(Ok(new { ToastText = toastText, ToastTitle = localizer["Success"] }));
            }
            catch (Exception e)
            {
                return(ControllerError.BadRequest(e, this));
            }
        }