Exemplo n.º 1
0
        public async Task <Photo> InsertAsync(PhotoCreate photoCreate, int applicationUserId)
        {
            var dataTable = new DataTable();

            dataTable.Columns.Add("PublicId", typeof(string));
            dataTable.Columns.Add("ImageUrl", typeof(string));
            dataTable.Columns.Add("Description", typeof(string));

            dataTable.Rows.Add(photoCreate.PublicId, photoCreate.ImageUrl, photoCreate.Description);

            int newPhotoId;

            using (var connection = new SqlConnection(_config.GetConnectionString("DefaultConnection")))
            {
                await connection.OpenAsync();

                newPhotoId = await connection.ExecuteScalarAsync <int>(
                    "Photo_Insert",
                    new
                {
                    Photo             = dataTable.AsTableValuedParameter("dbo.PhotoType"),
                    ApplicationUserId = applicationUserId
                },
                    commandType : CommandType.StoredProcedure);
            }

            Photo photo = await GetAsync(newPhotoId);

            return(photo);
        }
Exemplo n.º 2
0
        public async Task <bool> InsertBlogPhotoAsync(PhotoCreate photoCreate, int applicationUserId)
        {
            await _context.BlogPhotos.AddAsync(new BlogPhoto {
                AppUserId   = applicationUserId,
                ImageUrl    = photoCreate.ImageUrl,
                PublicId    = photoCreate.PublicId,
                Description = photoCreate.Description,
            });

            var created = await _context.SaveChangesAsync();

            return(created > 0);
        }
Exemplo n.º 3
0
        public async Task <IActionResult> CreateUserPhoto(int userId, [FromForm] PhotoCreate photo)
        {
            var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (userId != currentUserId)
            {
                return(Unauthorized());
            }

            var dbUser = await _repo.GetUser(userId);

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

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

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

            if (!dbUser.Photos.Any(u => u.IsMain))
            {
                dbPhoto.IsMain = true;
            }

            dbUser.Photos.Add(dbPhoto);


            if (await _repo.SaveAll())
            {
                var detail = _mapper.Map <UserDetailPhoto>(photo);
                return(CreatedAtRoute("GetPhoto", new { id = detail.Id }, detail));
            }

            return(BadRequest("Could not add the photo"));
        }
Exemplo n.º 4
0
        public async Task <ActionResult <Photo> > UploadPhoto(IFormFile file)
        {
            int applciationUserID = int.Parse(User.Claims.First(i => i.Type == JwtRegisteredClaimNames.NameId).Value);

            var uploadResult = await _photoService.AddPhotoAsync(file);

            if (uploadResult.Error != null)
            {
                return(BadRequest(uploadResult.Error.Message));
            }

            var photoCreate = new PhotoCreate
            {
                PublicID    = uploadResult.PublicId,
                ImageUrl    = uploadResult.SecureUrl.AbsoluteUri,
                Description = file.FileName
            };
            var photo = await _photoRepository.InsertAsync(photoCreate, applciationUserID);

            return(Ok(photo));
        }
Exemplo n.º 5
0
        public async Task <ActionResult <Photo> > UploadPhoto(IFormFile file)
        {
            var user = await _unitOfWork.UserRepository.GetUserByUsername(User.GetUsername());

            var result = await _photoService.AddPhotoAsync(file);

            if (result.Error != null)
            {
                return(BadRequest(result.Error.Message));
            }

            var photoCreate = new PhotoCreate
            {
                PublicId    = result.PublicId,
                ImageUrl    = result.SecureUrl.AbsoluteUri,
                Description = file.FileName
            };

            var photoInsert = await _unitOfWork.PhotoRepository.InsertBlogPhotoAsync(photoCreate, user.Id);

            return(Ok());
        }