Exemplo n.º 1
0
        public ResponseVM ApproveRequest([FromQuery] int rideId, [FromQuery] string requestId, [FromQuery] bool isApprove, [FromQuery] string providerId)
        {
            ResponseVM response = new ResponseVM
            {
                Result = true
            };

            if (isApprove)
            {
                RideRequest request = _rideService.FindRide(rideId).Requests.Find(_ => _.Id == requestId);
                float       amount  = _rideService.CalculateCostForRide(rideId, request.PickUp, request.Drop);
                if (_userService.IsBalanceAvailable(amount, request.RiderId))
                {
                    if (_rideService.ApproveRequest(rideId, requestId, isApprove))
                    {
                        _userService.PayBill(providerId, request.RiderId, amount);
                        response.Result = true;
                    }
                    else
                    {
                        response.ErrorMessage = "The Request can't be Accepted ";
                    }
                }
                else
                {
                    response.ErrorMessage = "The Balance is not Sufficient";
                }
            }
            else
            {
                _rideService.ApproveRequest(rideId, requestId, isApprove);
            }
            return(response);
        }
        static void Menu(User user)
        {
            //int choice;
            Choice choice;
            string pickUp;
            string drop;
            float  amount;

            do
            {
                Console.WriteLine("\nPlease enter your Choice\n" +
                                  "1.Offer ride\n" +
                                  "2.Book a Car\n" +
                                  "3.View Offered Rides and Approve Requests\n" +
                                  "4.View Bookings\n" +
                                  "5.Add Amount to Wallet\n" +
                                  "6.View Balance\n" +
                                  "7.Add a Car\n" +
                                  "8.Logout");
                choice = Enum.Parse <Choice>(Reader.ReadInt(1, 8).ToString());
                switch (choice)
                {
                case Choice.OfferRide:
                    Ride ride = new Ride();
                    if (userService.HasVehicle(user.Id))
                    {
                        if (user.Age < 18)
                        {
                            Console.WriteLine("Sorry,Minors can't offer a ride");
                        }
                        else
                        {
                            ride.ProviderId = user.Id;
                            Console.WriteLine("Please enter the date and time (dd/mm/yyyy hh:mm) of your journey ");
                            ride.StartDateTime = Reader.ReadDateTime();
                            Console.WriteLine("Please enter the starting place of your journey");
                            ride.Route.Source = Reader.ReadString().ToLower();
                            Console.WriteLine("Please enter the destination of your journey");
                            ride.Route.Destination = Reader.ReadString().ToLower();
                            Console.WriteLine("Please enter the distance in KM");
                            ride.Distance            = Reader.ReadFloat();
                            ride.EndDateTime         = ride.StartDateTime + rideService.CalculateTime(ride.Distance);
                            ride.Route.TotalDistance = ride.Distance;
                            if (rideService.FindOfferedRides(user.Id).Any(_ => (_.StartDateTime <= ride.StartDateTime && ride.StartDateTime < _.EndDateTime) || (_.StartDateTime < ride.EndDateTime && _.EndDateTime > ride.EndDateTime)))
                            {
                                Console.WriteLine("Sorry,You can't Offer two rides simultaneously");
                                break;
                            }
                            do
                            {
                                Console.WriteLine("Please enter Y to add a stop Over point,N otherwise");
                                bool isWilling = Reader.ReadBool();
                                if (isWilling)
                                {
                                    ViaPoint viaPoint = new ViaPoint();
                                    Console.WriteLine("Please enter the Address");
                                    viaPoint.Location = Reader.ReadString().ToLower();
                                    Console.WriteLine("Please enter the distance from " + ride.Route.Source);
                                    viaPoint.Distance = Reader.ReadFloat(0, ride.Distance);
                                    ride.Route.ViaPoints.Add(viaPoint);
                                }
                                else
                                {
                                    break;
                                }
                            } while (true);
                            Console.WriteLine("Please enter the cost per unit distance");
                            ride.UnitDistanceCost = Reader.ReadFloat();
                            ride.ProviderName     = user.Name;
                            int carIndex;
                            if (user.Vehicles.Count == 1)
                            {
                                carIndex = 0;
                            }
                            else
                            {
                                Console.WriteLine("Please select the car ");
                                foreach (var item in user.Vehicles.Select((value, index) => new { value, index }))
                                {
                                    Console.WriteLine(item.index + 1 + ". ");
                                    DisplayCarDetails(item.value);
                                }
                                carIndex = Reader.ReadInt(1, user.Vehicles.Count) - 1;
                            }
                            int numberOfSeats = user.Vehicles[carIndex].Capacity - 1;
                            Console.WriteLine("Please enter the number of seats to Offer");
                            ride.NoOfOfferedSeats = Reader.ReadInt(1, numberOfSeats);
                            ride.VehicleId        = user.Vehicles[carIndex].Number;
                            rideService.CreateRide(ride);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Please Add a car to Offer Ride");
                    }
                    break;

                case Choice.BookACar:
                    Console.WriteLine("Please enter the Starting place");
                    pickUp = Reader.ReadString().ToLower();
                    Console.WriteLine("Please enter the Destination");
                    drop = Reader.ReadString().ToLower();
                    Console.WriteLine("Please enter the date");
                    DateTime dateTime = Reader.ReadDate();
                    Console.WriteLine("Please enter the number of seats");
                    int         noOfSeats      = Reader.ReadInt();
                    List <Ride> AvailableRides = rideService.FindRides(pickUp, drop, dateTime, user.Id, noOfSeats, VehicleType.Car);
                    if (AvailableRides.Count == 0)
                    {
                        Console.WriteLine("Rides are not available");
                    }
                    else
                    {
                        foreach (var item in AvailableRides.Select((value, index) => new { value, index }))
                        {
                            Console.Write(item.index + 1 + ". ");
                            //Vehicle car = userService.FindVehicle(item.value.VehicleId, item.value.ProviderId);
                            Vehicle car = userService.FindVehicle(item.value.VehicleId);
                            DisplayAvailableRide(item.value);
                            DisplayCarDetails(car);
                            Console.WriteLine("Start Time:" + item.value.StartDateTime + rideService.CalculateTime(item.value, pickUp, drop));
                            Console.WriteLine("Cost: " + rideService.CalculateCostForRide(item.value.Id, pickUp, drop) * noOfSeats);
                        }
                        Console.WriteLine("Please enter Y to book a car,N to Go Back");
                        bool isWilling = Reader.ReadBool();
                        if (!isWilling)
                        {
                            break;
                        }
                        Console.WriteLine("Please select a ride ");
                        int   rideIndex     = Reader.ReadInt(1, AvailableRides.Count) - 1;
                        Ride  availableRide = AvailableRides[rideIndex];
                        float cost          = rideService.CalculateCostForRide(availableRide.Id, pickUp, drop) * noOfSeats;
                        if (userService.IsBalanceAvailable(cost, user.Id))
                        {
                            rideService.RequestRide(user.Id, availableRide.Id, pickUp, drop, noOfSeats);
                            Console.WriteLine("The Ride is Requested Successfully\n" +
                                              "Please wait for the approval");
                        }
                        else
                        {
                            Console.WriteLine("Sorry,Your Balance is not Sufficient");
                        }
                    }
                    break;

                case Choice.ViewOfferedRidesAndApproveRequests:
                    List <Ride> offeredRides = rideService.FindOfferedRides(user.Id);
                    if (offeredRides.Count == 0)
                    {
                        Console.WriteLine("Whoops,No rides are offered");
                    }
                    else
                    {
                        foreach (var item in offeredRides.Select((value, index) => new { value, index }))
                        {
                            Console.Write(item.index + 1 + ". ");
                            DisplayRide(item.value);
                            if (item.value.IsRideCompleted)
                            {
                                Console.WriteLine("The Ride is Completed");
                            }
                            else
                            {
                                Console.WriteLine("The Ride is not Completed");
                            }
                            Console.WriteLine("car number: " + item.value.VehicleId);
                        }
                        Console.WriteLine("Please select the ride to view Requests");
                        int  rideIndex   = Reader.ReadInt(1, offeredRides.Count) - 1;
                        Ride offeredRide = offeredRides[rideIndex];
                        ViewResponses(offeredRide);
                        if (offeredRide.IsRideCompleted)
                        {
                            break;
                        }
                        while (offeredRide.Requests.Count != 0)
                        {
                            ViewRequests(offeredRide);
                            Console.WriteLine("Please enter Y to Accept or Reject Request,N otherwise");
                            bool isWilling = Reader.ReadBool();
                            if (!isWilling)
                            {
                                break;
                            }
                            Console.WriteLine("Please select Request to Accept or Reject");
                            int    requestIndex = Reader.ReadInt(1, offeredRide.Requests.Count) - 1;
                            string riderId      = offeredRide.Requests[requestIndex].RiderId;
                            Console.WriteLine("Please enter Y to Approve Request,N to Reject");
                            bool isApprove = Reader.ReadBool();
                            if (isApprove)
                            {
                                amount = rideService.CalculateCostForRide(offeredRide.Id, offeredRide.Requests[requestIndex].PickUp, offeredRide.Requests[requestIndex].Drop);
                                User rider = userService.FindUser(riderId);
                                if (userService.IsBalanceAvailable(amount, rider.Id))
                                {
                                    if (rideService.ApproveRequest(offeredRide.Id, offeredRide.Requests[requestIndex].RequestId, isApprove))
                                    {
                                        userService.PayBill(user.Id, rider.Id, amount);
                                        Console.WriteLine("The request is Accepted Successfully");
                                    }
                                    else
                                    {
                                        Console.WriteLine("The Request can't be Accepted");
                                    }
                                }
                            }
                            else
                            if (rideService.ApproveRequest(offeredRide.Id, offeredRide.Requests[requestIndex].RequestId, isApprove))
                            {
                                Console.WriteLine("The request is Rejected ");
                            }
                        }
                    }
                    break;

                case Choice.ViewBookings:
                    ViewBookings(user.Id);
                    break;

                case Choice.AddAmountToWallet:
                    Console.WriteLine("Please enter the amount");
                    amount = Reader.ReadFloat();
                    userService.AddAmount(amount, user.Id);
                    break;

                case Choice.ViewBalance:
                    float balance = userService.GetBalance(user.Id);
                    Console.WriteLine("The Available Balance is " + balance);
                    break;

                case Choice.AddACar:
                    Console.WriteLine("Please select the type of vehicle" +
                                      "1.Car,2.Bike");
                    VehicleType type = Reader.ReadVehicleType();
                    userService.AddVehicle(user.Id, NewVehicle(type));
                    break;

                case Choice.Logout:
                    break;
                }
            } while (choice != Choice.Logout);
        }