Пример #1
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 _usersService.GetUser(userId);

            _photosService.UploadPhotoToCloudinary(photoForCreationDto.File, photoForCreationDto);

            var photo = _photosService.MapUploadedPhoto(photoForCreationDto, userFromRepo);

            if (await _usersService.SaveChangesInContext())
            {
                var photoToReturn = _photosService.MapPhotoForReturn(photo);
                return(CreatedAtRoute("GetPhoto", new { userId, id = photo.Id }, photoToReturn));
            }

            return(BadRequest("Could not add the photo"));
        }