Esempio n. 1
0
        public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreateDto photoForCreateDto)
        {
            var userIdFromToken = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (userIdFromToken != userId)
            {
                return(Unauthorized());
            }
            var userFromRepo = await _repo.GetUser(userId);

            var file   = photoForCreateDto.File;
            var result = UploadPhotoToCloudinary(file);

            photoForCreateDto.Url          = result.Uri.ToString();
            photoForCreateDto.CloudinaryId = result.PublicId;
            var photo = _mapper.Map <Photo>(photoForCreateDto);

            if (!userFromRepo.Photos.Any(p => p.IsMain))
            {
                photo.IsMain = true;
            }
            photo.UserId = userId;
            _repo.Add <Photo>(photo);
            if (await _repo.SaveAll())
            {
                var photoForReturn = _mapper.Map <PhotoForReturnDto>(photo);
                return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoForReturn));
            }
            return(BadRequest(" Error in Add Photo"));
        }
        public async Task <IActionResult> AddPhotoProduct(int productId, [FromForm] PhotoForCreateDto photoForCreateDto)
        {
            var product = await _repo.GetProduct(productId);

            var file         = photoForCreateDto.File;
            var uploadResult = new ImageUploadResult();

            if (file != null && file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File           = new FileDescription(file.Name, stream),
                        Transformation = new Transformation()
                                         .Width(500).Height(500).Crop("scale")
                    };
                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }
            photoForCreateDto.Url      = uploadResult.Uri.ToString();
            photoForCreateDto.PublicId = uploadResult.PublicId;
            var photo = _mapper.Map <PhotoForProduct>(photoForCreateDto);

            photo.ProductId = productId;
            _repo.Add(photo);
            if (await _repo.SaveAll())
            {
                var photoToRetrun = _mapper.Map <PhotoForReturnDto>(photo);
                return(CreatedAtRoute("GetPhoto", new { id = photo.PhotoId }, photoToRetrun));
            }

            return(BadRequest("خطا في اضافة الصورة"));
        }
        public async Task <IActionResult> AddPhotoToUser(string id, [FromForm] PhotoForCreateDto photoForCreate)
        {
            var editorRole = await isValidEditor();

            if (editorRole == null)
            {
                return(Unauthorized());
            }

            var fromDb = await _userRepo.Find(x => x.UserId == id);

            if (fromDb != null)
            {
                // assume all is well for starters
                var success = false;

                var file         = photoForCreate.File;
                var uploadResult = new ImageUploadResult();
                // did the user upload anything?
                if (file != null && file.Length > 0)
                {
                    using (var stream = file.OpenReadStream())
                    {
                        var uploadParams = new ImageUploadParams
                        {
                            File           = new FileDescription(file.Name, stream),
                            Folder         = "users",
                            Transformation = new Transformation()
                                             .Width(120)
                                             .Height(120)
                                             .Crop("fill")
                                             .Gravity("face")
                        };
                        uploadResult = _cloudinary.Upload(uploadParams);
                    }
                    // if successfull
                    if (!string.IsNullOrEmpty(uploadResult.PublicId))
                    {
                        photoForCreate.Url      = uploadResult.Uri.ToString();
                        photoForCreate.PublicId = uploadResult.PublicId;
                        // photoForCreate.InternalId = uid;

                        // could have used mapper also
                        fromDb.PublicId = uploadResult.PublicId;
                        fromDb.Url      = photoForCreate.Url;
                        success         = await _userRepo.SaveAll();
                    }
                }

                if (success)
                {
                    return(CreatedAtRoute("GetUserPhoto", new { id = id }));
                }
            }
            return(BadRequest("Failed to complete photo add to user"));
        }
Esempio n. 4
0
        public async Task <IActionResult> AddUserPhoto(int userId, [FromForm] PhotoForCreateDto photoForCreateDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRepo = await this.datingRepo.GetUser(userId);

            var file = photoForCreateDto.File;

            if (file == null || file.Length <= 0)
            {
                return(BadRequest("Could not add zero-length photo!"));
            }

            ImageUploadResult uploadResult;

            await using (var stream = file.OpenReadStream())
            {
                var uploadParams = new ImageUploadParams()
                {
                    File           = new FileDescription(file.FileName, stream),
                    Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face")
                };

                uploadResult = await this.cloudinary.UploadAsync(uploadParams);
            }

            if (!string.IsNullOrWhiteSpace(uploadResult.Error?.Message))
            {
                return(BadRequest("Upload image encountered error."));
            }

            photoForCreateDto.Url      = uploadResult.Url.ToString();
            photoForCreateDto.PublicId = uploadResult.PublicId;

            var photo = this.mapper.Map <Photo>(photoForCreateDto);

            if (!userFromRepo.Photos.Any(u => u.IsMain))
            {
                photo.IsMain = true;
            }

            userFromRepo.Photos.Add(photo);
            if (await this.datingRepo.SaveAll())
            {
                return(CreatedAtRoute("GetPhoto",
                                      new { userId, id = photo.Id },
                                      this.mapper.Map <PhotoForReturnDto>(photo)));
            }

            return(BadRequest("Could not add photo!"));
        }
Esempio n. 5
0
        public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreateDto photoForCreateDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var UserFromRepo = await _repo.GetUser(userId, true);

            var file         = photoForCreateDto.File;
            var uploadResult = new ImageUploadResult();

            if (file != null && file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File           = new FileDescription(file.Name, stream),
                        Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face")
                    };
                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }

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

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

            if (!UserFromRepo.Photos.Any(p => p.IsMain))
            {
                photo.IsMain = true;
            }
            UserFromRepo.Photos.Add(photo);
            if (await _repo.SaveAll())
            {
                try
                {
                    var PhotoToReturn = _mapper.Map <PhotoForReturnDto>(photo);
                    //var x = CreatedAtRoute(routeName: nameof(GetPhoto), routeValues: new { id = photo.Id }, value: PhotoToReturn);
                    //var x = CreatedAtAction(actionName: "GetPhoto", controllerName: "Photos", routeValues: new {id = photo.Id }, value: PhotoToReturn);
                    var x = Created("http://localhost:5000/api/Users/" + userId + "/photos/" + photo.Id, value: PhotoToReturn);
                    return(x);
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            return(BadRequest("خطأ في رفع الصورة"));
        }
        public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreateDto photoForCreateDto)
        {
            //--> Vérifier id de user avec id dans token
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            //--> Récupérer les donner de user
            var userFromRepo = await _repo.GetUser(userId, true);

            var file         = photoForCreateDto.File;
            var uploadResult = new ImageUploadResult();

            //--> Télécharger photo
            if (file != null && file.Length > 0)
            {
                //--> Paramétrage de photo
                using (var stream = file.OpenReadStream()){
                    var uploadParams = new ImageUploadParams()
                    {
                        File           = new FileDescription(file.Name, stream),
                        Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face")
                    };
                    //--> téléchargement de photo chez Cloudinary
                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }
            //--> Récupérer les données de photo
            photoForCreateDto.Url      = uploadResult.Uri.ToString();
            photoForCreateDto.publicId = uploadResult.PublicId;
            var photo = _mapper.Map <Photo>(photoForCreateDto);

            //--> tester s'il y a une photo principale de user
            if (!userFromRepo.Photos.Any(p => p.IsMain))
            {
                photo.IsMain = true;
            }

            //--> Ajouter photo à la BD
            userFromRepo.Photos.Add(photo);
            if (await _repo.SaveAll())
            {
                var PhotoToReturn = _mapper.Map <PhotoForReturnDto>(photo);
                return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, PhotoToReturn));
            }


            return(BadRequest("Erreur dans l'ajout de photo"));
        }
        public async Task <IActionResult> AddPhotoForUser(int userId, PhotoForCreateDto photoDto)
        {
            var user = await _repo.GetUser(userId);

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

            if (currentUserId != user.Id)
            {
                return(Unauthorized());
            }
            var file         = photoDto.File;
            var uploadResult = new ImageUploadResult();

            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File           = new FileDescription(file.Name, stream),
                        Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face")
                    };
                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }
            photoDto.Url      = uploadResult.Uri.ToString();
            photoDto.PublicId = uploadResult.PublicId;
            var photo = _mapper.Map <Photo>(photoDto);

            photo.User = user;
            if (!user.Photos.Any(m => m.IsMain))
            {
                photo.IsMain = true;
            }
            user.Photos.Add(photo);
            //var photoToReturn = _mapper.Map<PhotoForReturnDto>(photo);
            if (await _repo.SaveAll())
            {
                var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo);
                return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn));
            }

            return(BadRequest("Could not add the photo"));
        }
Esempio n. 8
0
        public async Task <IActionResult> AddPhotoForUser(int userID,
                                                          [FromForm] PhotoForCreateDto photoForCreateDto)
        {
            if (userID != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repo.GetUser(userID);

            var file = photoForCreateDto.File;

            var uploadResult = new ImageUploadResult();

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

                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }
            photoForCreateDto.Url      = uploadResult.Url.ToString();
            photoForCreateDto.PublicID = uploadResult.PublicId;

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

            if (!userFromRepo.Photos.Any(u => u.IsMain))
            {
                photo.IsMain = true;
            }

            userFromRepo.Photos.Add(photo);
            if (await _repo.SaveAll())
            {
                var photoForReturn = _mapper.Map <PhotoForReturnDto>(photo);
                return(CreatedAtRoute("GetPhoto", new { userID = userID, iD = photo.ID }, photoForReturn));
            }

            return(BadRequest("Cloud not add the photot"));
        }
Esempio n. 9
0
        public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreateDto photoForCreateDto)
        {
            //--> Vérifier l'authentification
            // if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            //     return Unauthorized();

            var userFromRepo = await _repo.GetUser(userId);

            var file         = photoForCreateDto.File;
            var uploadResult = new ImageUploadResult();

            if (file != null && file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File           = new FileDescription(file.Name, stream),
                        Transformation = new Transformation()
                                         .Width(500).Height(500).Crop("fill").Gravity("face")
                    };
                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }
            photoForCreateDto.Url      = uploadResult.Uri.ToString();
            photoForCreateDto.publicId = uploadResult.PublicId;
            var photo = _mapper.Map <Photo>(photoForCreateDto);

            if (!userFromRepo.Photos.Any(p => p.IsMain))
            {
                photo.IsMain = true;
            }
            userFromRepo.Photos.Add(photo);
            if (await _repo.SaveAll())
            {
                var PhotoToReturn = _mapper.Map <PhotoForReturnDto>(photo);
                // return CreatedAtRoute("GetPhoto", new {id=photo.Id},PhotoToReturn);
                return(Ok(PhotoToReturn));
            }

            return(BadRequest("Erreur lors de l'ajout de l'image"));
        }
        public async Task <IActionResult> AddPhotosupplier(int supplierId, [FromForm] PhotoForCreateDto photoForCreateDto)
        {
            if (supplierId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            var supplier = await _repo.GetSupplier(supplierId);

            var file         = photoForCreateDto.File;
            var uploadResult = new ImageUploadResult();

            if (file != null && file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File           = new FileDescription(file.Name, stream),
                        Transformation = new Transformation()
                                         .Width(500).Height(500).Crop("scale")
                    };
                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }
            photoForCreateDto.Url      = uploadResult.Uri.ToString();
            photoForCreateDto.PublicId = uploadResult.PublicId;
            var photo = _mapper.Map <PhotoForSupplier>(photoForCreateDto);

            photo.SupplierId = supplierId;
            if (!supplier.PhotoForSuppliers.Any(p => p.IsMain))
            {
                photo.IsMain = true;
            }
            _repo.Add(photo);
            if (await _repo.SaveAll())
            {
                var photoToRetrun = _mapper.Map <PhotoForReturnDto>(photo);
                return(CreatedAtRoute("GetPhotoSupplier", new { id = photo.PhotoId }, photoToRetrun));
            }

            return(BadRequest("خطا في اضافة الصورة"));
        }
Esempio n. 11
0
        public async Task <IActionResult> AddPhotoToSuit(int id, PhotoForCreateDto photoForCreate)
        {
            var editorRole = await isValidEditor();

            if (editorRole == null)
            {
                return(Unauthorized());
            }

            var fromDb = await _suitRepo.Find(x => x.SuitId == id);

            if (fromDb != null)
            {
                // assume all is well for starters
                var success = false;

                var file         = photoForCreate.File;
                var uploadResult = new ImageUploadResult();
                // did the user upload anything?
                SuitPhoto photo = null;
                if (file != null && file.Length > 0)
                {
                    using (var stream = file.OpenReadStream())
                    {
                        var uploadParams = new ImageUploadParams
                        {
                            File           = new FileDescription(file.Name, stream),
                            Folder         = "suits",
                            Transformation = new Transformation()
                                             .Width(180)
                                             .Height(180)
                                             .Crop("fill")
                                             // .Gravity("face")
                        };
                        uploadResult = _cloudinary.Upload(uploadParams);
                    }
                    // if successfull
                    if (!string.IsNullOrEmpty(uploadResult.PublicId))
                    {
                        photoForCreate.Url        = uploadResult.Uri.ToString();
                        photoForCreate.PublicId   = uploadResult.PublicId;
                        photoForCreate.InternalId = id;
                        // // Use mapper to transfer map for updating
                        photo = _mapper.Map <SuitPhoto>(photoForCreate);

                        // set first upload as default
                        if (!await _suitPhotoRepo.Any(x => x.SuitId == id))
                        {
                            photo.IsDefault = true;
                        }

                        // on success then add to user
                        _suitPhotoRepo.Add <SuitPhoto>(photo);
                        success = await _suitPhotoRepo.SaveAll();
                    }
                }
                if (success)
                {
                    var returnPhoto = _mapper.Map <PhotoForReturnDto>(photo);
                    return(CreatedAtRoute("GetSuitPhoto", new { id = photo.Id }, returnPhoto));
                }
            }
            return(BadRequest("Failed to complete add photo to suit"));
        }