Esempio n. 1
0
        public async Task <FriendDto> Handle(CreateFriendCommand request, CancellationToken cancellationToken)
        {
            var user = await _userManager.FindByEmailAsync("*****@*****.**");

            var adress = new Address(request.Number, request.Street, request.Neighborhood, request.City);

            var friend = new Friend(request.Name, request.Email, request.Phone, adress, user);

            var photoUploadResult = _photoAccesor.AddPhoto(request.File);

            friend.AddPhoto(photoUploadResult);

            _entityValidator.Validate(new Entity[] { friend, adress });

            if (_notification.HasNotifications)
            {
                _photoAccesor.DeletePhoto(photoUploadResult?.PublicId);
                return(null);
            }

            _context.Friends.Add(friend);

            await _context.Commit();

            return(_mapper.Map <FriendDto>(friend));
        }
        public async Task <ProductViewModel> Handle(CreateProductCommand request, CancellationToken cancellationToken)
        {
            var price = Convert.ToDecimal(request.Price);

            if (price < 0)
            {
                _notification.AddNotification("Preço", "Valor digitado é invalido");
                return(null);
            }
            var product = new Product(request.Name, price);

            var photoUploadResult = _photoAccesor.AddPhoto(request.File);

            product.AddPhoto(photoUploadResult);

            _entityValidator.Validate(new Entity[] { product });

            if (_notification.HasNotifications)
            {
                _photoAccesor.DeletePhoto(photoUploadResult?.PublicId);
                return(null);
            }

            _context.Products.Add(product);

            await _context.Commit();

            return(_mapper.Map <ProductViewModel>(product));
        }
Esempio n. 3
0
            public async Task <Result <List <Photo> > > Handle(Command request, CancellationToken cancellationToken)
            {
                var photos = new List <Photo>();

                if (request.Files != null && request.Files.Count > 0)
                {
                    foreach (var uploadedFile in request.Files)
                    {
                        var photoUploadResult = await _photoAccessor.AddPhoto(uploadedFile);

                        var photo = new Photo
                        {
                            Url = photoUploadResult.Url
                        };

                        _context.Photos.Add(photo);
                        photos.Add(photo);
                    }
                    var result = await _context.SaveChangesAsync() > 0;

                    if (!result)
                    {
                        return(Result <List <Photo> > .Failure("Ошибка при добавление фото"));
                    }
                }
                return(Result <List <Photo> > .Success(photos));
            }
Esempio n. 4
0
            public async Task <PhotoUploadResult> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.FirstOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUsername());

                if (user == null)
                {
                    throw new ErrorException(HttpStatusCode.NotFound, new { User = "******" });
                }

                var photoUploadResult = _photoAccessor.AddPhoto(request.File);

                if (user.photoId != null)
                {
                    if (_photoAccessor.DeletePhoto(user.photoId) == "ok")
                    {
                        System.Console.WriteLine("Profile Image is deleted");
                    }
                    else
                    {
                        System.Console.WriteLine("Fail Destroy Profile Image");
                    }
                }
                user.photoUrl = photoUploadResult.Url;
                user.photoId  = photoUploadResult.PublicId;
                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(photoUploadResult);
                }

                throw new Exception("Problem saving changes");
            }
Esempio n. 5
0
            public async Task <Result <Photo> > Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.Include(p => p.Photos)
                           .FirstOrDefaultAsync(x => x.UserName == _userAccessor.GetUsername());

                if (user == null)
                {
                    return(null);
                }

                var photoUploadResult = await _photoAccessor.AddPhoto(request.File);

                var photo = new Photo {
                    Url = photoUploadResult.Url,
                    Id  = photoUploadResult.PublicId
                };

                //Check if the user has any photos that are already set to as a main photo. If not this is the first photo and set it as a main photo.
                if (!user.Photos.Any(x => x.IsMain))
                {
                    photo.IsMain = true;
                }

                user.Photos.Add(photo);

                var result = await _context.SaveChangesAsync() > 0;

                if (result)
                {
                    return(Result <Photo> .Success(photo));
                }

                return(Result <Photo> .Failure("Problem adding photo"));
            }
Esempio n. 6
0
            public async Task <Result <Photo> > Handle(Command request, CancellationToken cancellationToken)
            {
                // handler logic
                var photoUploadResult = _photoAccessor.AddPhoto(request.File);
                var group             = await _context.Groups.SingleOrDefaultAsync(x => x.Id == request.Id);

                if (group == null)
                {
                    return(null);
                }

                var photo = new Photo
                {
                    Url = photoUploadResult.Url,
                    Id  = photoUploadResult.PublicId
                };

                if (!group.Photos.Any(x => x.IsMain))
                {
                    photo.IsMain = true;
                }

                group.Photos.Add(photo);

                var success = await _context.SaveChangesAsync() > 0;

                if (!success)
                {
                    return(Result <Photo> .Failure("Failed adding photo."));
                }

                return(Result <Photo> .Success(photo));
            }
Esempio n. 7
0
            public async Task <Result <Photo> > Handle(Command request, CancellationToken cancellationToken)
            {
                // handler logic

                var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUsername());

                if (user == null)
                {
                    return(null);
                }

                var photoUploadResult = _photoAccessor.AddPhoto(request.File);
                var photo             = new Photo
                {
                    Url  = photoUploadResult.Url,
                    Id   = photoUploadResult.PublicId,
                    Name = photoUploadResult.Name
                };

                if (!user.Photos.Any(x => x.IsMain))
                {
                    photo.IsMain = true;
                }

                user.Photos.Add(photo);

                var success = await _context.SaveChangesAsync() > 0;

                if (!success)
                {
                    return(Result <Photo> .Failure("Failed to add photo."));
                }

                return(Result <Photo> .Success(photo));
            }
        public async Task <Photo> Handle(AddTeamLogoCommand request, CancellationToken cancellationToken)
        {
            var photoUploadResult = _photoAccessor.AddPhoto(request.File);

            var team = await _context.Team
                       .Include(x => x.Logo)
                       .FirstOrDefaultAsync(x => x.Id == request.TeamId, cancellationToken);

            var photo = new Photo
            {
                Url    = photoUploadResult.Url,
                Id     = photoUploadResult.PublicId,
                TeamId = request.TeamId
            };

            team.Logo = photo;

            var success = await _context.SaveChangesAsync(cancellationToken) > 0;

            if (success)
            {
                return(photo);
            }

            throw new Exception("Problem saving changes");
        }
Esempio n. 9
0
            public async Task <Photo> Handle(Command request, CancellationToken cancellationToken)
            {
                // Get activity from database
                var photoUploadResult = _photoAccessor.AddPhoto(request.File);
                var currentUser       = await _context.Users
                                        .SingleOrDefaultAsync(user => user.UserName == _userAccessor.GetCurrentUsername());

                var newPhoto = new Photo
                {
                    Id  = photoUploadResult.PublicId,
                    URL = photoUploadResult.URL
                };

                // Check if main photo, if not set it
                if (!currentUser.Photos.Any(photo => photo.IsMain))
                {
                    newPhoto.IsMain = true;
                }

                currentUser.Photos.Add(newPhoto);

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(newPhoto);
                }

                throw new Exception("Problem uploading new photo");
            }
Esempio n. 10
0
        public async Task <Result <Unit> > Handle(Command request, CancellationToken cancellationToken)
        {
            if (request.Profile.Photofile == null)
            {
                return(Result <Unit> .Failure("File missing"));
            }
            var photoResult = await _photoAccessor.AddPhoto(request.Profile.Photofile);

            if (photoResult == null)
            {
                return(Result <Unit> .Failure("Failed to upload file"));
            }

            var photo = new Photo
            {
                Id     = photoResult.PublicId,
                Url    = photoResult.Url,
                IsMain = true
            };

            request.Profile.Photo = photo;

            _context.Profiles.Add(request.Profile);
            var result = await _context.SaveChangesAsync() > 0;

            if (result)
            {
                return(Result <Unit> .Success(Unit.Value));
            }
            return(Result <Unit> .Failure("Failed to add profile"));
        }
Esempio n. 11
0
            public async Task <Result <Photo> > Handle(Command request, CancellationToken cancellationToken)
            {
                var usr = await _dbContext.Users
                          .Include(p => p.Photos)
                          .FirstOrDefaultAsync(x => x.UserName == _userAccessor.GetUsername());

                if (usr == null)
                {
                    return(null);
                }
                var photoRes = await _photoAccessor.AddPhoto(request.File);

                var ph = new Photo
                {
                    Url = photoRes.Url,
                    Id  = photoRes.PublicId
                };

                if (!usr.Photos.Any(x => x.IsMain))
                {
                    ph.IsMain = true;
                }

                usr.Photos.Add(ph);

                var res = await _dbContext.SaveChangesAsync() > 0;

                if (res)
                {
                    return(Result <Photo> .Success(ph));
                }
                return(Result <Photo> .Failure("Issue uploading photo."));
            }
Esempio n. 12
0
        public async Task <Result <Unit> > Handle(Command request, CancellationToken cancellationToken)
        {
            if (request.Certificate.PhotoFile == null)
            {
                return(Result <Unit> .Failure("Fill missing"));
            }

            var res = await _photoAccessor.AddPhoto(request.Certificate.PhotoFile);

            if (res == null)
            {
                return(Result <Unit> .Failure("Failed to upload photo"));
            }


            var photo = new Photo
            {
                Id     = res.PublicId,
                Url    = res.Url,
                IsMain = true
            };

            request.Certificate.Logo = photo;

            _dataContext.Certificates.Add(request.Certificate);
            var result = await _dataContext.SaveChangesAsync() > 0;

            if (!result)
            {
                Result <Unit> .Failure("Failed to add certificate");
            }

            return(Result <Unit> .Success(Unit.Value));
        }
Esempio n. 13
0
            public async Task <Photo> Handle(Command request, CancellationToken cancellationToken)
            {
                var photoUploadResult = _photoAccessor.AddPhoto(request.File);

                var user = await _context.Users.SingleOrDefaultAsync(x =>
                                                                     x.UserName == _userAccessor.GetCurrentUsername());

                var photo = new Photo
                {
                    Url = photoUploadResult.Url,
                    Id  = photoUploadResult.PublicId
                };

                if (!user.Photos.Any(x => x.isMain))
                {
                    photo.isMain = true;
                }

                // user has been already accessed - that .Add will save changes into
                // user's ICollection and Photos table.
                user.Photos.Add(photo);

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(photo);
                }

                throw new Exception("Problem saving changes");
            }
Esempio n. 14
0
            public async Task <Photo> Handle(Command request, CancellationToken cancellationToken)
            {
                var(publicId, url) = _photoAccessor.AddPhoto(request.File);
                var user = await _context.Users.SingleOrDefaultAsync(u =>
                                                                     u.UserName == _userAccessor.GetCurrentUsername(), cancellationToken : cancellationToken);

                var photo = new Photo
                {
                    Url = url,
                    Id  = publicId
                };

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

                user.Photos.Add(photo);

                var success = await _context.SaveChangesAsync(cancellationToken) > 0;

                if (success)
                {
                    return(photo);
                }
                throw new Exception("Problem saving photo.");
            }
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var photoUploadResult = _photoAccessor.AddPhoto(request.File);

                var product = await _context.Products.FindAsync(request.Id);

                if (product == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { product = "Not Found" });
                }

                var photo = new ProductPhoto
                {
                    Url = photoUploadResult.Url,
                    Id  = photoUploadResult.PublicId
                };

                if (!product.ProductPhotos.Any(x => x.IsMain))
                {
                    photo.IsMain = true;
                }

                product.ProductPhotos.Add(photo);

                var success = await _context.SaveChangesAsync();

                return(success > 0 ? Unit.Value : throw new Exception("Problem saving changes"));
            }
Esempio n. 16
0
            public async Task <Photo> Handle(Command request, CancellationToken cancellationToken)
            {
                var photoUploadResult = photoAccessor.AddPhoto(request.File);
                var user = await this.context.Users
                           .Include(u => u.Photos)
                           .SingleOrDefaultAsync(x => x.UserName == userAccessor.GetCurrentUsername());

                var photo = new Photo
                {
                    Url = photoUploadResult.Url,
                    Id  = photoUploadResult.PublicId
                };

                if (!user.Photos.Any(x => x.IsMain))
                {
                    photo.IsMain = true;
                }
                user.Photos.Add(photo);

                var success = await context.SaveChangesAsync() > 0; // SaveChangesAsync returns # of changes persisted to DB.

                if (success)
                {
                    return(photo);
                }

                throw new Exception("Problem saving activity");
            }
Esempio n. 17
0
            public async Task <UserPhotoItem> Handle(Command request, CancellationToken cancellationToken)
            {
                var currentUser       = _userAccessor.GetUserId();
                var photoUploadResult = _photoAccessor.AddPhoto(request.File);
                var photoEntity       = new Domain.Entities.UserPhoto
                {
                    AppUserId   = currentUser,
                    CreatedDate = DateTime.Now,
                    Description = request.Description,
                    ImageType   = request.ImageType,
                    Path        = photoUploadResult.Url,
                    Id          = photoUploadResult.PublicId
                };

                _context.UserPhotos.Add(photoEntity);

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(_mapper.Map <UserPhoto, UserPhotoItem>(photoEntity));
                }

                throw new Exception("Problem saving changes");
            }