示例#1
0
        public async Task <IActionResult> AddPhotoForUser(string userId, [FromForm]  PhotoForCreationDto photoForCreationDto)
        {
            if (userId != User.FindFirst(ClaimTypes.NameIdentifier).Value)
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repo.GetPersonById(userId);

            var file = photoForCreationDto.File;

            var photoForCreation = await _photoRepo.UploadPhotoToCloud(photoForCreationDto, _clodinaryConfig);

            var photo = Helpers.HelperMapper.MapPhotoForCreationDtoToPhoto(photoForCreation);

            // check if there is no main Photo for the person
            if (userFromRepo.Photos != null)
            {
                if (!userFromRepo.Photos.Any(p => p.IsMain))
                {
                    photo.IsMain = true;
                }
                var currentPhototCount = userFromRepo.Photos.Count();
                photo.Id = userFromRepo.Photos.Last().Id + 1;
            }
            else
            {
                photo.Id     = 0;
                photo.IsMain = true;
            }

            if (userFromRepo.Photos == null)
            {
                userFromRepo.Photos = new List <Photo>();
            }

            userFromRepo.Photos.Add(photo);

            var response = await _repo.UpdatePersonsData(userId, userFromRepo);

            if (response == HttpStatusCode.OK)
            {
                var phototToReturn = Helpers.HelperMapper.MapPhotoToPhotoForReturnDto(photo);
                return(CreatedAtRoute("GetPhoto", new { userId = userId, id = photo.Id }, phototToReturn));
            }


            return(BadRequest("Adding photo failed"));
        }
        public async Task <IActionResult> Put(string id, [FromBody] UserForUpdateDtos user)
        {
            // check if the user is authorized
            if (id != User.FindFirst(ClaimTypes.NameIdentifier).Value)
            {
                return(Unauthorized());
            }
            var currentPerson = await _cosmosManager.GetPersonById(id);

            var person   = Helpers.HelperMapper.MapUserToUserForUpdateDto(currentPerson, user);
            var response = await _cosmosManager.UpdatePersonsData(id, person);

            if (response == HttpStatusCode.OK)
            {
                return(NoContent());
            }
            return(BadRequest("Unable to update the person"));
        }