Esempio n. 1
0
        public ActionResult AddPhotoForCity(int cityId, [FromForm] PhotoForCreationDto photoForCreation)
        {
            var city = _appRepository.GetCityById(cityId);

            if (city == null)
            {
                return(BadRequest("City not found"));
            }
            var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (currentUserId != city.UserId)
            {
                return(Unauthorized());
            }
            var file         = photoForCreation.File;
            var uploadResult = new ImageUploadResult();

            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File = new FileDescription(file.Name, stream)
                    };
                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }

            photoForCreation.Url      = uploadResult.Uri.ToString();
            photoForCreation.PublicId = uploadResult.PublicId;

            var photo = _mapper.Map <Photo>(photoForCreation);

            photo.City = city;
            if (!city.Photos.Any(p => p.IsMain))
            {
                photo.IsMain = true;
            }
            city.Photos.Add(photo);
            if (_appRepository.SaveAll())
            {
                var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo);
                return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn));
            }
            return(BadRequest("Hay Aksi"));
        }
Esempio n. 2
0
 public ActionResult Add([FromBody] City city)
 {
     _appRepository.Add(city);
     _appRepository.SaveAll();
     return(Ok(city));
 }