Exemplo n.º 1
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);
                }
            }
        }
Exemplo n.º 2
0
        public void Quit(Trip trip, int userId, string userPseudo)
        {
            //TODO : Handle Repayment
            //TODO : Handle Fees for us
            //TODO : No need to go in DB for PotUser and TripParticipant
            //TODO : Notification
            Errors.Clear();

            using (var scope = new TransactionScope())
            {
                try
                {
                    // 1 - Delete trip participant
                    var participant = _participantRepository.GetTripParticipant(trip.Id, userPseudo);
                    if (participant == null || _participantRepository.HasErrors)
                    {
                        Errors.Add("Unable to find participant");
                        MergeErrors(_participantRepository);
                        return;
                    }

                    _participantRepository.DeleteTripParticipant(participant);
                    if (_participantRepository.HasErrors)
                    {
                        Errors.Add("Unable to delete participant");
                        MergeErrors(_participantRepository);
                        return;
                    }

                    // 2 - Delete user trip information
                    var userTrip = _userTripRepository.GetUserTrip(userId, trip.TripName);
                    if (userTrip == null || _userTripRepository.HasErrors)
                    {
                        Errors.Add("Unable to find user's trip");
                        MergeErrors(_userTripRepository);
                        return;
                    }

                    _userTripRepository.DeleteUserTrip(userTrip);
                    if (_userTripRepository.HasErrors)
                    {
                        Errors.Add("Unable to delete user's trip");
                        MergeErrors(_userTripRepository);
                        return;
                    }

                    // 3 - Delete pot user information
                    var potUser = _potUserRepository.GetPotUser(trip.TripPot.Id, userId);
                    if (potUser == null || _potUserRepository.HasErrors)
                    {
                        Errors.Add("Unable to find user in the trip's pot");
                        MergeErrors(_potUserRepository);
                        return;
                    }

                    _potUserRepository.DeletePotUser(potUser);
                    if (_potUserRepository.HasErrors)
                    {
                        Errors.Add("Unable to delete user from pot");
                        MergeErrors(_potUserRepository);
                        return;
                    }

                    // 4 - Update pot target amount if needed
                    if (potUser.Amount > 0)
                    {
                        trip.TripPot.CurrentAmount -= potUser.Amount;
                        _potRepository.UpdatePot(trip.TripPot);
                        if (_potRepository.HasErrors)
                        {
                            trip.TripPot.CurrentAmount += potUser.Amount;
                            Errors.Add("Unable to update trip's pot");
                            MergeErrors(_potRepository);
                            return;
                        }
                    }

                    trip.DeleteParticipant(participant);
                    trip.TripPot.DeleteParticipant(potUser);

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