public async Task <IActionResult> Edit(int id, [Bind("AnswerImageId,FileName,Url,Question")] AnswerImage answerImage)
        {
            if (id != answerImage.AnswerImageId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(answerImage);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AnswerImageExists(answerImage.AnswerImageId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(answerImage));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Upload(IFormFile file)
        {
            BlobContainerClient containerClient;

            // Create the container and return a container client object
            try
            {
                containerClient = await _blobServiceClient.CreateBlobContainerAsync(containerName);

                containerClient.SetAccessPolicy(Azure.Storage.Blobs.Models.PublicAccessType.BlobContainer);
            }
            catch (RequestFailedException)
            {
                containerClient = _blobServiceClient.GetBlobContainerClient(containerName);
            }


            try
            {
                // create the blob to hold the data
                var blockBlob = containerClient.GetBlobClient(file.FileName);
                if (await blockBlob.ExistsAsync())
                {
                    await blockBlob.DeleteAsync();
                }

                using (var memoryStream = new MemoryStream())
                {
                    // copy the file data into memory
                    await file.CopyToAsync(memoryStream);

                    // navigate back to the beginning of the memory stream
                    memoryStream.Position = 0;

                    // send the file to the cloud
                    await blockBlob.UploadAsync(memoryStream);

                    memoryStream.Close();
                }

                // add the photo to the database if it uploaded successfully
                var image = new AnswerImage();
                image.Url      = blockBlob.Uri.AbsoluteUri;
                image.FileName = file.FileName;

                _context.AnswerImage.Add(image);
                _context.SaveChanges();
            }
            catch (RequestFailedException)
            {
                View("Error");
            }

            return(RedirectToAction("Index"));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Upload(IFormFile answerImage)
        {
            BlobContainerClient containerClient;

            try
            {
                containerClient = await _blobServiceClient.CreateBlobContainerAsync(containerName);

                containerClient.SetAccessPolicy(Azure.Storage.Blobs.Models.PublicAccessType.BlobContainer);
            }
            catch (RequestFailedException)
            {
                containerClient = _blobServiceClient.GetBlobContainerClient(containerName);
            }


            try
            {
                var blockBlob = containerClient.GetBlobClient(answerImage.FileName);
                if (await blockBlob.ExistsAsync())
                {
                    await blockBlob.DeleteAsync();
                }

                using (var memoryStream = new MemoryStream())
                {
                    await answerImage.CopyToAsync(memoryStream);

                    memoryStream.Position = 0;

                    await blockBlob.UploadAsync(memoryStream);

                    memoryStream.Close();
                }

                // add the photo to the database if it uploaded successfully
                var image = new AnswerImage();
                image.Url      = blockBlob.Uri.AbsoluteUri;
                image.FileName = answerImage.FileName;

                _context.images.Add(image);
                _context.SaveChanges();
            }
            catch (RequestFailedException)
            {
                View("Error");
            }

            return(RedirectToAction("Index"));
        }
Exemplo n.º 4
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var questionRepository = _unitOfWork.Repository <Question>();
                var questionWithAnswerSpecification = new QuestionWithAnswerSpecification(request.QuestionId);
                var question =
                    await questionRepository.GetEntityWithSpecificationAsync(questionWithAnswerSpecification);

                if (question == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, "Question not found");
                }

                if (question.IsAnswerVideo)
                {
                    throw new RestException(HttpStatusCode.BadRequest, "Answer should not be image");
                }

                if (question.IsAnswerVideo && question.Answer?.AnswerVideo != null)
                {
                    throw new RestException(HttpStatusCode.BadRequest, "Answer already exists");
                }

                var uploadResult = await _imageService.UploadImageAsync(request.File, "OnlineEducation/AnswerImages");

                var answerImage = new AnswerImage
                {
                    Url       = uploadResult.Url,
                    PublicId  = uploadResult.PublicId,
                    CreatedAt = uploadResult.CreatedAt,
                };

                question.Answer ??= new Answer();
                question.Answer.AnswerImages ??= new List <AnswerImage>();
                question.Answer.AnswerImages.Add(answerImage);

                var result = await _unitOfWork.CompleteAsync() > 0;

                if (result)
                {
                    return(Unit.Value);
                }

                throw new Exception(ExceptionMessages.ProblemSavingChanges);
            }