public KeyValuePair <bool, string> CancelReservation(int driveId, int userId) { Require.ThatIntIsPositive(driveId); Require.ThatIntIsPositive(userId); Drive drive = this.drives.GetByIdQueryable(driveId) .Include(d => d.DrivesPassengers) .FirstOrDefault(); if (drive == null) { return(new KeyValuePair <bool, string>(false, "This drive does not exist.")); } if (!this.usersService.CheckIfUserExists(userId)) { return(new KeyValuePair <bool, string>(false, "This user does not exist.")); } if (!drive.DrivesPassengers.Any(d => d.PassengerId == userId)) { return(new KeyValuePair <bool, string>(false, "You don't have a reservation for that drive.")); } DrivesPassengers itemToRemove = drive.DrivesPassengers .FirstOrDefault(x => x.DriveId == driveId && x.PassengerId == userId); drive.DrivesPassengers.Remove(itemToRemove); this.drives.UpdateAsync(drive); return(new KeyValuePair <bool, string>(true, "Your registration has been canceled.")); }
public KeyValuePair <bool, string> ReserveSeat(int driveId, int userId) { Require.ThatIntIsPositive(driveId); Require.ThatIntIsPositive(userId); Drive drive = this.drives.GetByIdQueryable(driveId) .Include(d => d.DrivesPassengers) .FirstOrDefault(); if (drive == null) { return(new KeyValuePair <bool, string>(false, "This drive does not exist.")); } if (!this.usersService.CheckIfUserExists(userId)) { return(new KeyValuePair <bool, string>(false, "This user does not exist.")); } if (drive.DrivesPassengers.Any(x => x.PassengerId == userId)) { return(new KeyValuePair <bool, string>(false, "You already have a reservation for the drive.")); } if (drive.DrivesPassengers.Count == drive.DeclaredSeats) { return(new KeyValuePair <bool, string>(false, "All seats for the drive are taken.")); } DrivesPassengers pair = new DrivesPassengers() { DriveId = driveId, PassengerId = userId }; drive.DrivesPassengers.Add(pair); this.drives.UpdateAsync(drive); return(new KeyValuePair <bool, string>(true, "You made a reservation! Have a safe drive.")); }