public void Participate(Trip trip, int userId, string userPseudo) { Errors.Clear(); using (var scope = new TransactionScope()) { try { // 1 - Create trip participant var participant = CreateParticipant(trip, userPseudo); _participantRepository.SaveTripParticipant(participant); if (_participantRepository.HasErrors) { Errors.Add("Unable to add user to trip participant"); MergeErrors(_participantRepository); return; } // 2 - Create user trip var userTrip = CreateUserTrip(trip, userId, false); _userTripRepository.SaveUserTrip(userTrip); if (_userTripRepository.HasErrors) { Errors.Add("Unable to add trip to user information"); MergeErrors(_userTripRepository); return; } // 3 - Create pot user var potUser = CreatePotUser(trip, trip.TripPot, userId); _potUserRepository.SavePotUser(potUser); if (_potUserRepository.HasErrors) { Errors.Add("Unable to add user to trip's pot"); MergeErrors(_potUserRepository); return; } trip.AddParticipant(participant); trip.TripPot.AddParticipant(potUser); scope.Complete(); } catch (Exception ex) { HandleException(ex); } } }
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); } } }