Ejemplo n.º 1
0
        public HttpResponse AddUserToTrip(string tripId)
        {
            var trip = this.data.Trips.Where(t => t.Id == tripId).FirstOrDefault();
            var user = this.data.Users.Where(x => x.Id == this.User.Id).FirstOrDefault();

            var isAlreadyJoined = this.data.UserTrips.Any(x => x.TripId == tripId && x.UserId == this.User.Id);

            if (isAlreadyJoined)
            {
                return(Redirect($"/Trips/Details?tripId={tripId}"));
            }
            if (trip.Seats == 0)
            {
                return(Redirect($"/Trips/Details?tripId={tripId}"));
            }
            trip.Seats -= 1;
            var userTrip = new Data.Models.UserTrip {
                Trip = trip, User = user
            };

            this.data.UserTrips.Add(userTrip);
            this.data.SaveChanges();

            return(this.Redirect("/Trips/All"));
        }
Ejemplo n.º 2
0
        public HttpResponse AddUserToTrip(string tripId)
        {
            var tripsId = data.Trips.Select(t => t.Id).ToList();

            if (!tripId.Contains(tripId))
            {
                return(BadRequest());
            }

            var trip = data.Trips
                       .Include(t => t.UserTrips)
                       .Where(t => t.Id == tripId)
                       .FirstOrDefault();

            if (trip.Seats > 0)
            {
                if (!trip.UserTrips.Select(u => u.UserId).Contains(User.Id))
                {
                    var userTrip = new Data.Models.UserTrip
                    {
                        UserId = User.Id,
                        TripId = tripId
                    };

                    data.UsersTrips.Add(userTrip);
                    data.SaveChanges();

                    trip.Seats--;
                    data.SaveChanges();
                }
                else
                {
                    //return Error("You are already booked for this trip.");
                    return(Redirect($"/Trips/Details?tripId={tripId}"));
                }
            }
            return(Redirect("/Trips/All"));
        }