Пример #1
0
        public async Task <IActionResult> GetAll()
        {
            int currentUserId = User.Claims.GetUserId();

            AzureBlobService blobService = new AzureBlobService();
            await blobService.InitializeBlob();

            var quizzes = quizRepository.GetQuizByUserId(currentUserId);

            foreach (var quiz in quizzes)
            {
                quiz.PictureUrl = blobService.urlPath.AbsoluteUri.ToString() + "users/" + quiz.PictureUrl;
            }

            return(Ok(quizzes.Select(quiz => this.entityToVmMapper.Map(quiz))));
        }
Пример #2
0
        public async Task <IActionResult> Delete(int id)
        {
            int currentUserId = User.Claims.GetUserId();
            var deletedQuiz   = quizRepository.DeleteQuiz(id, currentUserId);

            if (deletedQuiz != null)
            {
                AzureBlobService blobService = new AzureBlobService();
                await blobService.InitializeBlob();

                blobService.DeletePhoto(deletedQuiz.PictureUrl);
                return(Ok());
            }
            else
            {
                return(BadRequest("Deletion Impossible. Quiz does not exist."));
            }
        }
Пример #3
0
        public async Task <IActionResult> Put(NewQuizViewModel quizBodyData, int id)
        {
            int           currentUserId     = User.Claims.GetUserId();
            IActionResult imageHttpResponse = FileValidityChecker(quizBodyData);

            if (IActionResult.Equals(imageHttpResponse, Ok()))
            {
                return(imageHttpResponse);
            }

            string           fileUrl;
            AzureBlobService blobService;

            try {
                blobService = new AzureBlobService();
                await blobService.InitializeBlob();

                var previousQuizState = quizRepository.GetQuizById(id);

                blobService.DeletePhoto(previousQuizState.PictureUrl);
                fileUrl = await blobService.UploadPhoto(quizBodyData.Files[0]);
            }
            catch (Exception) {
                return(BadRequest("Image could not be uploaded."));
            }

            Quiz updatedQuiz = quizRepository.UpdateQuiz(new Quiz {
                Title      = quizBodyData.Title,
                PictureUrl = fileUrl,
                UserId     = currentUserId,
                Id         = id
            });

            if (updatedQuiz == null)
            {
                return(BadRequest("Quiz does not exist."));
            }

            QuizViewModel quizVm = entityToVmMapper.Map(updatedQuiz);

            quizVm.PictureUrl = blobService.urlPath.AbsoluteUri.ToString() + "users/" + fileUrl;

            return(Ok(new { quizVm.Id, quizVm.Title, quizVm.PictureUrl }));
        }
Пример #4
0
        public async Task <IActionResult> GetQuiz(int id)
        {
            int currentUserId = User.Claims.GetUserId();

            AzureBlobService blobService = new AzureBlobService();
            await blobService.InitializeBlob();

            Quiz quiz = quizRepository.GetQuizById(id);

            if (quiz == null || quiz.UserId != currentUserId)
            {
                return(NotFound("Quiz doesn't exist"));
            }

            quiz.PictureUrl = blobService.urlPath.AbsoluteUri.ToString() + "users/" + quiz.PictureUrl;
            QuizViewModel quizVm = this.entityToVmMapper.Map(quiz);

            return(Ok(quizVm));
        }