Exemple #1
0
        private async Task <GroupModel> TryToHandle(CreateGroupCommand request)
        {
            User admin = await _context.Users.FindAsync(request.Admin) ?? throw new InvalidCommandException($"El usuario administrador con id {request.Admin} no existe");

            await ValidateMembersExists(request);

            var toCreate = new Group()
            {
                Name  = request.Name,
                Admin = await _context.Users.FindAsync(request.Admin),
            };
            await _context.Groups.AddAsync(toCreate);

            await _context.SaveChangesAsync();

            if (!request.Memberships.Contains(request.Admin))
            {
                request.Memberships.Add(toCreate.Admin.Id);
            }

            Domain.Membership[] memberships = await Task.WhenAll(request.Memberships.Select(i => GetMemberships(i, toCreate)));

            toCreate.Memberships = memberships;
            await _context.SaveChangesAsync();

            return(new GroupModel(toCreate));
        }
        private async Task <Unit> TryToHandle(LeaveGroupCommand request)
        {
            User user = await _context.Users.FindAsync(request.UserId) ?? throw new InvalidCommandException($"El usuario con id {request.UserId} no existe");

            Group group = await _context.Groups.Include(group1 => group1.Admin).FirstOrDefaultAsync(g => g.Id == request.GroupId) ?? throw new InvalidCommandException($"El grupo con id {request.GroupId} no existe");

            if (group.Admin.Id == request.UserId)
            {
                throw new InvalidCommandException("El administrador no puede irse del grupo");
            }
            Domain.Membership membership = await _context.Memberships.FirstOrDefaultAsync(m => m.Group.Id == group.Id && m.User.Id == user.Id) ?? throw new InvalidCommandException($"Usuario no es parte del grupo");

            group.Memberships.Remove(membership);
            _context.Memberships.Remove(membership);

            var toDelete = _context.Participants.Where(p => p.Purchase.Group.Id == group.Id && p.UserId == user.Id);

            _context.Participants.RemoveRange(toDelete);

            var cantLeave = _context.Purchases.Any(p => p.Purchaser.Id == user.Id && p.Group.Id == group.Id);

            if (cantLeave)
            {
                throw new InvalidCommandException("No te podes ir del grupo si compraste algo");
            }

            await _context.SaveChangesAsync();

            return(Unit.Value);
        }
Exemple #3
0
        public async Task <Unit> Handle(AddFriendCommand request, CancellationToken cancellationToken)
        {
            Task <User> current = _context
                                  .Users
                                  .Include(u => u.Friends)
                                  .Include(u => u.FriendsOf)
                                  .FirstOrDefaultAsync(u => u.Id == request.CurrentUserId, cancellationToken: cancellationToken);
            Task <User> toAdd = _context
                                .Users
                                .Include(u => u.Friends)
                                .Include(u => u.FriendsOf)
                                .FirstOrDefaultAsync(u => u.Id == request.FriendUserId, cancellationToken: cancellationToken);
            User currentUser = await current;
            User userToAdd   = await toAdd;

            if (currentUser == null || userToAdd == null)
            {
                throw new InvalidCommandException("Usuarios no existen");
            }

            currentUser.AddFriend(userToAdd);

            await _context.SaveChangesAsync();

            return(Unit.Value);
        }
Exemple #4
0
        private async Task <UserModel> TryToHandle(CreateUserCommand request)
        {
            if (string.IsNullOrWhiteSpace(request.Password))
            {
                throw new InvalidCommandException($"Password is required");
            }
            if (await _context.Users.AnyAsync(user => user.Mail == request.Mail))
            {
                throw new InvalidCommandException($"User with email {request.Mail} already exists.");
            }

            var toCreate = new User()
            {
                Name     = request.Name,
                LastName = request.LastName,
                Mail     = request.Mail,
                Password = _hasher.Hash(request.Password)
            };

            await _context.Users.AddAsync(toCreate);

            await _context.SaveChangesAsync();

            return(new UserModel(toCreate));
        }
Exemple #5
0
        private async Task <UserModel> TryToUpdate(UpdateUserCommand request)
        {
            int  id       = request.Id;
            User toUpdate = await _context.Users.FindAsync(id);

            toUpdate.Name     = request.Name ?? toUpdate.Name;
            toUpdate.LastName = request.LastName ?? toUpdate.LastName;
            toUpdate.Mail     = request.Mail ?? toUpdate.Mail;
            if (!string.IsNullOrEmpty(request.Password))
            {
                toUpdate.Password = request.Password;
            }

            await _context.SaveChangesAsync();

            return(new UserModel(toUpdate));
        }
Exemple #6
0
        public async Task <Unit> Handle(DeleteGroupCommand request, CancellationToken cancellationToken)
        {
            Group toDelete = await _context
                             .Groups
                             .FirstOrDefaultAsync(g => g.Id == request.Id, cancellationToken : cancellationToken);

            if (toDelete == null)
            {
                throw new InvalidCommandException($"No existe el grupo con id {request.Id}");
            }

            DeleteMemberships(toDelete);
            _context.Groups.Remove(toDelete);
            _context.Purchases.RemoveRange(toDelete.Purchases);

            await _context.SaveChangesAsync();

            return(Unit.Value);
        }
Exemple #7
0
        public async Task <PurchaseModel> Handle(ModifyPurchaseCommand request, CancellationToken cancellationToken)
        {
            Purchase purchase = await _context
                                .Purchases
                                .Include(purchase1 => purchase1.Group)
                                .Include(purchase1 => purchase1.Purchaser)
                                .Include(purchase1 => purchase1.Participants)
                                .FirstOrDefaultAsync(purchase1 => purchase1.Id == request.PurchaseId, cancellationToken: cancellationToken)
                                ?? throw new InvalidCommandException("Compra no existe");

            UpdateNameIfNeeded(request, purchase);
            UpdateCostIfNeeded(request, purchase);
            UpdateCurrencyIfNeeded(request, purchase);
            await UpdateParticipantsIfNeeded(request, purchase);

            await _context.SaveChangesAsync();

            return(new PurchaseModel(purchase));
        }
Exemple #8
0
        private async Task <GroupModel> TryToUpdate(ModifyGroupCommand request)
        {
            int   id       = request.Id;
            Group toUpdate = await _context.Groups
                             .Include(group => group.Memberships)
                             .Include(group => group.Admin)
                             .Include(group => group.Purchases)
                             .FirstOrDefaultAsync(g => g.Id == id);

            toUpdate.Name = request.Name ?? toUpdate.Name;

            var oldUsers = toUpdate.Memberships.Select(membership => membership.UserId).ToList();

            CleanMemberships(toUpdate);
            Domain.Membership[] memberships = await Task.WhenAll(request.Memberships.Select(i => GetMemberships(i, toUpdate)));

            var newUsers = memberships.Select(membership => membership.UserId).ToList();
            var toRemove = oldUsers.Except(newUsers).ToList();

            foreach (Purchase purchase in toUpdate.Purchases.ToList())
            {
                foreach (int i in toRemove)
                {
                    purchase.RemoveParticipant(new User()
                    {
                        Id = i
                    });
                }

                if (toRemove.Contains(purchase.Purchaser.Id))
                {
                    toUpdate.Purchases.Remove(purchase);
                }
            }



            toUpdate.Memberships = memberships;

            await _context.SaveChangesAsync();

            return(new GroupModel(toUpdate));
        }
Exemple #9
0
        public async Task <Unit> Handle(DeleteUserCommand request, CancellationToken cancellationToken)
        {
            User toDelete = await _context.Users.FindAsync(request.Id);

            IEnumerable <Friendship> friendshipsToDelete = _context.Friendships.Where(friendship => friendship.Friend1Id == toDelete.Id || friendship.Friend2Id == toDelete.Id);

            if (_context.Groups.Include(group => group.Admin).Any(g => g.Admin.Id == request.Id))
            {
                throw new InvalidCommandException("No te podes borrar porque sos admin de una grupo");
            }
            if (toDelete == null)
            {
                return(Unit.Value);
            }

            _context.Friendships.RemoveRange(friendshipsToDelete);
            _context.Users.Remove(toDelete);
            _imageRepository.DeleteImage(request.Id);
            await _context.SaveChangesAsync();

            return(Unit.Value);
        }
        public async Task <Unit> Handle(DeleteFriendCommand request, CancellationToken cancellationToken)
        {
            /*
             * User current = await _context.Users.Include(u => u.Friends).Include(u => u.FriendsOf).FirstOrDefaultAsync(u => u.Id == request.CurrentUserId, cancellationToken: cancellationToken);
             * User friend = await _context.Users.Include(u => u.Friends).Include(u => u.FriendsOf).FirstOrDefaultAsync(u => u.Id == request.FriendUserId, cancellationToken: cancellationToken);
             *
             * if (current == null || friend == null)
             *  return Unit.Value;
             *
             * current.RemoveFriend(friend);
             */
            var friendshipsToRemove = await _context.Friendships.Where(friendship => (friendship.Friend1Id == request.CurrentUserId && friendship.Friend2Id == request.FriendUserId) ||
                                                                       (friendship.Friend1Id == request.FriendUserId && friendship.Friend2Id == request.CurrentUserId))
                                      .ToListAsync(cancellationToken: cancellationToken);

            _context.Friendships.RemoveRange(friendshipsToRemove);

            await _context.SaveChangesAsync();


            return(Unit.Value);
        }
        public async Task <PurchaseModel> Handle(CreatePurchaseCommand request, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(request.Name))
            {
                throw new InvalidCommandException($"Nombre de la compra {request.Name} es invalido");
            }
            Group group = await GetGroupIfValid(request);

            User purchaser = await GetPurchaserIfValid(request);

            IEnumerable <User> participants = await GetParticipantsIfValid(request.Participants, group);

            Currency currency = GetCurrencyIfValid(request);

            var purchase = new Purchase(purchaser, group, request.Cost, currency, participants, request.Name, request.Longitude, request.Latitude);

            group.Purchases.Add(purchase);
            await _context.SaveChangesAsync();


            return(new PurchaseModel(purchase));
        }