Exemplo n.º 1
0
        public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDto messageForCreationDto)
        {
            var sender = await _repo.UserRepository.GetUserAsync(userId);

            if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            messageForCreationDto.SenderId = userId;
            var recipient = await _repo.UserRepository.GetUserAsync(messageForCreationDto.RecipientId);

            if (recipient == null)
            {
                return(BadRequest("Couldn't find user"));
            }

            var message = _mapper.Map <Message>(messageForCreationDto);

            _repo.MessageRepository.Add(message);

            if (await _repo.SaveAllAsync())
            {
                var messageToReturn = _mapper.Map <MessageToReturnDto>(message);

                return(CreatedAtRoute("GetMessage", new { id = message.Id }, messageToReturn));
            }

            throw new Exception("Creating the message failed on save");
        }
Exemplo n.º 2
0
        public async Task <IActionResult> CreateExamAsync(ExamForCreationDto examForCreation)
        {
            byte[] passwordHash, passwordSalt;
            var    exam = _mapper.Map <Exam>(examForCreation);

            examForCreation.Password.CreatePasswordHash(out passwordHash, out passwordSalt);
            exam.PasswordHash = passwordHash;
            exam.PasswordSalt = passwordSalt;
            exam.AuthorId     = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);
            _repo.ExamRepository.Add(exam);
            if (await _repo.SaveAllAsync())
            {
                return(CreatedAtRoute("GetExamAsync", new { examId = exam.Id }, exam));
            }
            return(BadRequest("Failed to save exam"));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreationDto photoForCreationDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userfromRepo = await _repo.UserRepository.GetUserAsync(userId);

            var file = photoForCreationDto.File;

            var uploadResult = new ImageUploadResult();

            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File           = new FileDescription(file.Name, stream),
                        Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face")
                    };
                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }

            photoForCreationDto.Url      = uploadResult.Uri.ToString();
            photoForCreationDto.PublicId = uploadResult.PublicId;
            var photo = _mapper.Map <Photo>(photoForCreationDto);

            if (!userfromRepo.Photos.Any(p => p.IsMain))
            {
                photo.IsMain = true;
            }

            userfromRepo.Photos.Add(photo);



            if (await _repo.SaveAllAsync())
            {
                var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo);
                return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn));
            }

            return(BadRequest("Could not add photo"));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> CreateAnswerAsync(int questionId, AnswerForCreationDto answerforCreation)
        {
            var questionFromRepo = await _repo.QuestionRepository.GetQuestionAsync(questionId);

            if (questionFromRepo == null)
            {
                return(BadRequest("Question not found"));
            }
            if (questionFromRepo.Exam.AuthorId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            var answer = _mapper.Map <Answer>(answerforCreation);

            answer.QuestionId = questionId;
            _repo.AnswerRepository.Add(answer);
            if (await _repo.SaveAllAsync())
            {
                var answerForReturn = _mapper.Map <AnswerForReturnDto>(answer);
                return(CreatedAtRoute("GetAnswerAsync", new { answerId = answer.Id }, answerForReturn));
            }
            return(BadRequest("Failed to save an answer"));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> CreateQuestionAsync(int examId, QuestionForCreationDto questionForCreation)
        {
            var exam = await _repo.ExamRepository.GetExamAsync(examId);

            if (exam == null)
            {
                return(BadRequest("Exam not exists"));
            }
            if (exam.AuthorId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            var question = _mapper.Map <Question>(questionForCreation);

            question.ExamId = exam.Id;
            _repo.QuestionRepository.Add(question);
            if (await _repo.SaveAllAsync())
            {
                var questionForReturn = _mapper.Map <QuestionForReturnDto>(question);
                return(CreatedAtRoute("GetQuestionAsync", new { questionId = question.Id }, questionForReturn));
            }

            return(BadRequest("Failed to save a question"));
        }
Exemplo n.º 6
0
        public async Task <IActionResult> UpdateUser(int id, UserForUpdateDto userForUpdateDto)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userfromRepo = await _repo.UserRepository.GetUserAsync(id);

            _mapper.Map(userForUpdateDto, userfromRepo);
            if (await _repo.SaveAllAsync())
            {
                return(NoContent());
            }

            throw new Exception($"Updating user {id} failed on save");
        }