Exemplo n.º 1
0
        public ActionResult AddPhotoForCity(int cityId, [FromBody] PhotoForAddDto photoForAddDto)
        {
            var city = _appRepository.GetCityById(cityId);

            if (city == null)
            {
                return(BadRequest("Couldn't find the city!"));
            }

            var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (currentUserId != city.UserId)
            {
                return(Unauthorized());
            }

            var file = photoForAddDto.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);
                }
            }

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

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

            photo.City = city;

            if (!city.Photos.Any(p => p.IsMain))
            {
                photo.IsMain = true;
            }

            city.Photos.Add(photo);

            if (_appRepository.SaveAll())
            {
                var photoToReturn = _mapper.Map <PhotoForGetDto>(photo);
                return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn));
            }

            return(BadRequest("Couldn't add photo!"));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> AddPhoto(int userId, PhotoForAddDto photoForAddDto)
        {
            var userFromRepo = await _repo.GetUser(userId);

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

            userFromRepo.Photos.Add(photo);
            if (await _repo.SaveAll())
            {
                return(Ok());
            }
            return(BadRequest());
        }
Exemplo n.º 3
0
        public IActionResult Post([FromForm] PhotoForAddDto photoForAddDto)
        {
            if (photoForAddDto.File.Length > 0)
            {
                ImageUploadResult imageUploadResult = _photoUpload.ImageUpload(photoForAddDto.File);
                var mapResult = _mapper.Map <Photo>(photoForAddDto);
                mapResult.PhotoUrl = imageUploadResult.Uri.ToString();
                mapResult.PublicId = imageUploadResult.PublicId;
                mapResult.UserId   = User.Claims.GetUserId().Data;
                IResult result = _photoService.Add(mapResult);

                if (result.IsSuccessful)
                {
                    this.RemoveCache();
                    return(Ok(result.Message));
                }
                return(this.ServerError(result.Message));
            }
            return(BadRequest());
        }
Exemplo n.º 4
0
        public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForAddDto photoForAddDto, int?secondId) // FromForm mowi skad zdjecie bedzie pochodzic
        {
            // sprawdza id z id z tokena
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var auction = await _repository.TakeEditingAuction(userId);

            // UWAGA DZIWNE ponizsza nazwa file MUSI ZGADZAC SIE z nazwa w POSTMANIE z NAZWA ZDJECIA wtf xd
            var file          = photoForAddDto.File;     // zrobienie pliku z klasy PhotoForAddDto zeby dane moglybyc zczytane
            var uploadResault = new ImageUploadResult(); // to ma byc na sztywno i tyle, pozniej bedzie wykorzystane to zwrocenia info o zdj

            if (file.Length > 0)                         // jezeli plik zostal wczytany
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams()                                                    // okreslamy parametry
                    {
                        File           = new FileDescription(file.Name, stream),                                  // przekazywane jest imie pliku i plik (stream)
                        Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face") // szerokosc wysokosc wypelnienie i ze ma byc scenrtowane zdj na twarzy
                    };

                    uploadResault = _cloudinary.Upload(uploadParams); // uploadujemy z przekazaniem parametrow i w uploadResault beda zwrocone info z chmury
                }
            }

            photoForAddDto.Url      = uploadResault.Uri.ToString(); // no i dajemy otrzymane id i url do naszej klasy ktora pozniej przeslemy
            photoForAddDto.PublicId = uploadResault.PublicId;

            var photo = _mapper.Map <Photo>(photoForAddDto); // mapujemy na photo z photoforadddto

            photo.SecondId = (int)secondId;

            if (auction.ItemPhotos == null)
            {
                photo.IsMain = true;
                photo.ItemId = auction.Id;
                _repository.Add(photo);
                // auction.ItemPhotos.Add(photo);
            }
            else
            {
                if (!auction.ItemPhotos.Any(p => p.IsMain)) // sprawdza czy JUZ jakies zdjecie jest glowne
                {
                    photo.IsMain = true;
                }

                auction.ItemPhotos.Add(photo);
            }


            if (await _repository.SaveAll())
            {
                var photoForReturn = _mapper.Map <PhotoForReturnDto>(photo);
                // return CreatedAtAction(nameof(GetPhoto), new { id = photo.Id }, photoForReturn);  // 1 argument to sciezka skad bedzie cos pobierane, 2 - przekazujemy id, 3 - zwracamy zdjecie
                return(CreatedAtAction(nameof(GetPhoto), new { userId, id = photo.Id }, photoForReturn)); // userId musimy tez przekazac, jest w glownym url tego kontrolera
                // return CreatedAtAction(nameof(GetPhoto), new {id = photo.Id});

                // UWAGAAAAAAAAAAAAAAAAAA jezeli tego returna z createataction nie bedzie to nie bedzie dzialac
                // cos w angularze, cos co powoduje ze zdj po uploadzie OD RAZU sie pojawiaja i nie trzeba odswiezac
            }

            return(BadRequest("Nie mozna dodac zdj"));
        }