예제 #1
0
        public void UpdateAllowedNumberOfPeople(Trip trip, int newNumberOfPeople)
        {
            //TODO : Notification to user's for the change
            //TODO : Repayment for users is newNumberOfPeople is increasing
            //TODO : Update has payed flag if newNumberOfPeople is decreasing
            Errors.Clear();
            var oldNumberOfPeople = trip.NumberMaxOfPeople;

            using (var scope = new TransactionScope())
            {
                try
                {
                    // 1 - Update User pot amotunt
                    foreach (var potUser in _potUserRepository.GetPotUsers(trip.TripPot.Id))
                    {
                        potUser.TargetAmount = trip.Price / newNumberOfPeople;
                        _potUserRepository.UpdatePotUser(potUser);

                        if (_potUserRepository.HasErrors)
                        {
                            Errors.Add("Unable to update pot user informations");
                            MergeErrors(_potUserRepository);
                            return;
                        }
                    }

                    // 2 - update trip
                    trip.NumberMaxOfPeople = newNumberOfPeople;
                    _tripRepository.UpdateTrip(trip);

                    if (_tripRepository.HasErrors)
                    {
                        trip.NumberMaxOfPeople = oldNumberOfPeople;
                        Errors.Add("Unable to update trip");
                        MergeErrors(_tripRepository);
                        return;
                    }

                    scope.Complete();
                }
                catch (Exception ex)
                {
                    trip.NumberMaxOfPeople = oldNumberOfPeople;
                    HandleException(ex);
                }
            }
        }
예제 #2
0
        private void InternalCreditDebitPot(Pot pot, int userId,
                                            Action <Pot> potHandler, Action <Pot> potRollbackHandler, Action <PotUser> potUserHandler)
        {
            Errors.Clear();

            using (var scope = new TransactionScope())
            {
                try
                {
                    // 1 - Update pot user
                    var potUser = _potUserRepository.GetPotUser(pot.Id, userId);
                    if (potUser == null || _potUserRepository.HasErrors)
                    {
                        Errors.Add("Unable to find user in the pot");
                        MergeErrors(_potUserRepository);
                        return;
                    }

                    potUserHandler(potUser);

                    _potUserRepository.UpdatePotUser(potUser);
                    if (_potUserRepository.HasErrors)
                    {
                        Errors.Add("Unable to update the pot");
                        MergeErrors(_potUserRepository);
                        return;
                    }

                    // 2 - Update pot
                    potHandler(pot);
                    _potRepository.UpdatePot(pot);
                    if (_potRepository.HasErrors)
                    {
                        potRollbackHandler(pot);
                        Errors.Add("Unable to debit pot");
                        MergeErrors(_potRepository);
                        return;
                    }

                    scope.Complete();
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }