Example #1
0
        public async Task <ActionResult> CreateTrip(int creatorId, [FromBody] TripCreateDTO newTrip)
        {
            try
            {
                TripDTO result = await _tripService.CreateTrip(creatorId, newTrip);

                if (result != null)
                {
                    return(Ok(result));
                }
                return(BadRequest(new JsonResult("Trip dates are not valid.")));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Example #2
0
        public async Task <TripDTO> CreateTrip(int userId, TripCreateDTO newTrip)
        {
            using (_unitOfWork)
            {
                if (!DateManagerService.checkFromToDates(newTrip.From, newTrip.To))
                {
                    return(null);
                }

                Trip trip = _mapper.Map <TripCreateDTO, Trip>(newTrip);
                User user = await _unitOfWork.UserRepository.FindByID(userId);

                if (user.MyTrips == null)
                {
                    user.MyTrips = new List <Trip>();
                }
                user.MyTrips.Add(trip);

                if (trip.Travelers == null)
                {
                    trip.Travelers = new List <User>();
                }
                trip.Travelers.Add(user);

                AbstractFactory factory = getConcreteFactory(newTrip.TripCategory);

                trip.AddOn    = factory.CreateAddOn();
                trip.TripType = factory.CreateTripType();

                await _unitOfWork.TripTypeRepository.Create(trip.TripType);

                await _unitOfWork.TripRepository.Create(trip);

                _unitOfWork.UserRepository.Update(user);
                await _unitOfWork.Save();

                TripDTO returnTrip = _mapper.Map <Trip, TripDTO>(trip);
                return(returnTrip);
            }
        }