예제 #1
0
        public async Task <IActionResult> Update(EditPetDTO editPetDTO, int petId)
        {
            if (petId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            var pet = await petsRepository.GetPet(petId);

            mapper.Map(editPetDTO, pet);

            if (await petsRepository.Save())
            {
                return(NoContent());
            }
            throw new Exception("Server nie zapisał zmian");
        }
예제 #2
0
        public async Task <IActionResult> AddPhoto(int petId, [FromForm] CloudPhotoDto cloudPhotoDto)
        {
            if (petId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var pet = await petsRepository.GetPet(petId);

            var file = cloudPhotoDto.File;
            var imageUploadResult = new ImageUploadResult();

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

                    imageUploadResult = cloud.Upload(imageUploadParams);
                }
            }

            cloudPhotoDto.Url      = imageUploadResult.Url.ToString();
            cloudPhotoDto.PublicId = imageUploadResult.PublicId;
            var photo = mapper.Map <Photo>(cloudPhotoDto);

            if (!pet.Photos.Any(p => p.MainPhoto))
            {
                photo.MainPhoto = true;
            }

            pet.Photos.Add(photo);

            if (await petsRepository.Save())
            {
                var photoDTO = mapper.Map <PhotoDTO>(photo);
                return(CreatedAtRoute("GetPhoto", new { photoId = photo.Id }, photoDTO));
            }

            return(BadRequest("Nie udało się załadować zdjęcia"));
        }