コード例 #1
0
        public void CreateTrip(Trip trip, int userId)
        {
            Errors.Clear();
            using (var scope = new TransactionScope())
            {
                try
                {
                    // 1 - Create Trip
                    _tripRepository.SaveTrip(trip);

                    if (_tripRepository.HasErrors)
                    {
                        MergeErrors(_tripRepository);
                        return;
                    }


                    // 2 - Create a pot
                    //TODO : Handle both pot mode when creating from trip
                    var pot = new Pot(-1, trip.Id, trip.Organizer, PotMode.Lead, 0.0, trip.Price, "Pot " + trip.TripName,
                                      trip.StartDate, trip.EndDate, trip.ValidityDate, trip.Description, false, string.Empty, null);
                    _potRepository.SavePot(pot);

                    if (_potRepository.HasErrors)
                    {
                        Errors.Add("Unable to create a pot associated to the trip");
                        MergeErrors(_potRepository);
                        return;
                    }

                    // 3 - add trip in organizer's trip
                    var userTrip = CreateUserTrip(trip, userId, true);
                    _userTripRepository.SaveUserTrip(userTrip);

                    if (_userTripRepository.HasErrors)
                    {
                        Errors.Add("Unable to add trip in user information");
                        MergeErrors(_userTripRepository);
                        return;
                    }

                    // 4 - add organizer as a participant
                    var tripParticipant = CreateParticipant(trip, trip.Organizer);
                    _participantRepository.SaveTripParticipant(tripParticipant);

                    if (_participantRepository.HasErrors)
                    {
                        Errors.Add("Unable to add organizer as a participant");
                        MergeErrors(_participantRepository);
                        return;
                    }



                    // 5 - add organizer as a member of the pot
                    var potUser = CreatePotUser(trip, pot, userId);
                    _potUserRepository.SavePotUser(potUser);

                    if (_potUserRepository.HasErrors)
                    {
                        Errors.Add("Unable to ad organizer to the pot");
                        MergeErrors(_potUserRepository);
                        return;
                    }

                    trip.TripPot = pot;
                    trip.AddParticipant(tripParticipant);
                    pot.AddParticipant(potUser);

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