public AttendStatus AttendToTravelPlan(int userId, int travelPlanId) { try { using (var scope = _provider.CreateScope()) { var attendingUser = GetUser(userId); if (attendingUser == null) { return(AttendStatus.UserDoesntExist); // if attending user doesn't exist return false } var travelPlanService = scope.ServiceProvider.GetService <TravelPlanService>(); var travelPlan = travelPlanService.GetTravelPlan(travelPlanId); var attendedUsers = travelPlan.UserTravelPlans.ToList(); var ownerUser = attendedUsers.Where(x => x.IsUserOwner).First(); if (ownerUser.UserId == userId) // If the owner user tries to attend, don't attend { return(AttendStatus.IsOwner); } attendedUsers.Remove(ownerUser); // Remove the owner user from list int availableSeatCount = travelPlan.SeatCount - attendedUsers.Count; if (availableSeatCount <= 0) // Check if there is an available seat exists { return(AttendStatus.NoSeatsAvailable); } else { foreach (var user in attendedUsers) { if (user.UserId == userId) { return(AttendStatus.AlreadyAttended); // If user already attended to travel plan return false } } travelPlan.UserTravelPlans.Add(new UserTravelPlan // Add the user to the travel plan and return true { User = attendingUser, TravelPlan = travelPlan, IsUserOwner = false, }); var result = _rideShareDbContext.Update(travelPlan); _rideShareDbContext.SaveChanges(); return(AttendStatus.Successful); } } } catch (Exception e) { return(AttendStatus.Failure); } }
public bool SetTravelPlanStatusAsync(int userId, int planId, bool isActive) { var plan = _rideShareDbContext.TravelPlans .Where(tp => tp.TravelPlanId == planId) .Include(tp => tp.UserTravelPlans) .ThenInclude(utp => utp.User) .First(); var ownerUser = plan.UserTravelPlans.Where(utp => utp.IsUserOwner == true).First().User; if (ownerUser.UserId != userId) { return(false); } if (plan == null) { return(false); } plan.IsActive = isActive; _rideShareDbContext.SaveChanges(); return(true); }