Ejemplo n.º 1
0
        public Trip CreateNewRoadTrip(RoadTripModel model)
        {
            if (!ModelState.IsValid)
            {
                return null;
            }

            var username = ClaimsPrincipal.Current.Identity.Name;

            var ownerPerson = _ctx.Persons.First(x => x.RegisteredUserName == username);
            var newRoadTrip = new Trip {Name = model.Name, OwnerId = ownerPerson.Id};
            _ctx.Trips.Add(newRoadTrip);
            
            _ctx.SaveChanges();
            newRoadTrip.Hash = String.Format("{0}{1}", username.Substring(0, 2).ToUpper(), newRoadTrip.Id);
            var tripMap = new TripUserMap {IsOwner = true, PersonId = ownerPerson.Id, TripId = newRoadTrip.Id};
            _ctx.TripUserMaps.Add(tripMap);
            return _ctx.SaveChanges() > 0 ? newRoadTrip : null;

        }
Ejemplo n.º 2
0
        public TripDetailsModel JoinRoadTrip(JoinRoadTripModel joinRoadTrip)
        {
            var username = ClaimsPrincipal.Current.Identity.Name;

            var ownerPerson = _ctx.Persons.First(x => x.RegisteredUserName == username);

            var roadTripToJoin = _ctx.Trips.FirstOrDefault(x => x.Hash == joinRoadTrip.RoadTripHash.ToUpper());
            var roadTripUsers = _ctx.TripUserMaps.Where(x => x.TripId == roadTripToJoin.Id);
            if (roadTripUsers.Any(x=>x.PersonId == ownerPerson.Id))
            {
                throw new Exception("You are already a part of this road trip.");
            }
            if (roadTripToJoin != null)
            {
                var tripUserMap = new TripUserMap {PersonId = ownerPerson.Id, TripId = roadTripToJoin.Id, IsOwner = false};
                _ctx.TripUserMaps.Add(tripUserMap);
                _ctx.SaveChanges();
                return new TripDetailsModel
                {
                    RoadTripId = roadTripToJoin.Id,
                    //Expenses = 
                };
            }
            throw new Exception("No roadtrip found for the join code");
        }
Ejemplo n.º 3
0
        public void AddUserToRoadTrip(AddUserToRoadTripModel userToAdd)
        {
            var username = ClaimsPrincipal.Current.Identity.Name;

            var ownerPerson = _ctx.Persons.First(x => x.RegisteredUserName == username);

            var roadTripByRoadTripId = _ctx.Trips.First(x => x.Id == userToAdd.RoadTripId);
            if (roadTripByRoadTripId.OwnerId == ownerPerson.Id)
            {
                var userByUserName = _ctx.Persons.FirstOrDefault(x => x.Email.ToLower() == userToAdd.UserToAddEmail.ToLower());
                if (userByUserName == null)
                {
                    throw new Exception("This email is not registered in the system.");
                }
                //verify that the users is not already added
                if (!_ctx.TripUserMaps.Any(x => x.PersonId == userByUserName.Id && x.TripId == roadTripByRoadTripId.Id))
                {
                    var tripMap = new TripUserMap
                    {
                        IsOwner = false,
                        PersonId = userByUserName.Id,
                        TripId = roadTripByRoadTripId.Id
                    };
                    _ctx.TripUserMaps.Add(tripMap);
                    _ctx.SaveChanges();
                }
                
            }
            throw new Exception("You are not the owner of this road trip.  Only owners can add users!");
            

        }