コード例 #1
0
        public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDTo messageForCreationDTo)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var recipient = await _repo.GetUser(messageForCreationDTo.RecipientId);

            var sender = await _repo.GetUser(userId);

            messageForCreationDTo.UserId = userId;

            if (await _repo.GetUser(messageForCreationDTo.RecipientId) == null)
            {
                return(BadRequest("Couldn't Find User!"));
            }

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

            _repo.Add <Message>(message);


            if (await _repo.SaveAll())
            {
                var messageForReturn = _mapper.Map <MessageToReturnDto>(message);
                return(CreatedAtRoute("getMessage", new { userId, id = message.Id }, messageForReturn));
            }

            return(BadRequest("Failure in message sending!"));
        }
コード例 #2
0
        public async Task <IActionResult> photoUpload(int userId, [FromForm] PhotoFRomUserDto photoFRomUserDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            var userFromRepo = await _repo.GetUser(userId);

            var file   = photoFRomUserDto.File;
            var result = new ImageUploadResult();


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


                    result = _cloudinary.Upload(uploadParams);
                }
            }


            photoFRomUserDto.Url      = result.Uri.ToString();
            photoFRomUserDto.PublicId = result.PublicId;

            var photo = _mapper.Map <Photos>(photoFRomUserDto);

            if (!userFromRepo.Photos.Any(u => u.IsMain))
            {
                photo.IsMain = true;
            }

            userFromRepo.Photos.Add(photo);

            if (await _repo.SaveAll())
            {
                var returedPhoto = _mapper.Map <ReturnedPhotoDto>(photo);
                return(CreatedAtRoute("GetPhoto", new { userId = userId, id = photo.ID }, returedPhoto));
            }
            return(BadRequest("Couldn't Upload the photo"));
        }
コード例 #3
0
        public async Task <IActionResult> updateUser(int id, UserUpdateDto userUpdateDto)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repo.GetUser(id);

            _mapper.Map(userUpdateDto, userFromRepo);
            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

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