예제 #1
0
        public static void AddBike(string bikeName, bool newBikeName, string bikeModelDescription, bool newBikeModel)
        {
            Bike currentBike = GetBikeByName(bikeName);

            if (newBikeName == false)
            {
                if (currentBike == null)
                {
                    throw new System.Exception($"No bike name {bikeName}");
                }
            }

            string bikeId = currentBike.Id;

            BikeModel[] bikeModels = DatabaseOperations.GetBikeModels();

            BikeModel currentBikeModel = bikeModels.SingleOrDefault(bm => bm.Description == bikeModelDescription);

            if (newBikeModel == false)
            {
                if (currentBikeModel == null)
                {
                    //throw new System.Exception($"No bikemodel name {bikeModelDescription}");
                }
            }

            string bikeModelId = currentBikeModel != null ? currentBikeModel.ID : "";

            string newGuid = Guid.NewGuid().ToString();

            string sql = $"insert into dbo.inventory (id, bikeid, bikemodel) values('{newGuid}', '{bikeId}', '{bikeModelId}')";

            Exception error = ExecuteNonQuery(sql);

            if (error != null)
            {
                throw error;
            }
        }
예제 #2
0
        GetBikesAvailability(InventoryGroup[] inventoryGroups,
                             string name,
                             string email,
                             string phone,
                             DateTime startDate,
                             DateTime endDate,
                             DateSelection dateSelection)
        {
            BikesAvailability bikesAvailability
                = new BikesAvailability(
                      dateSelection,
                      name,
                      email,
                      phone);


            List <DisplayTime> availableDates = new List <DisplayTime>();

            DateTime runningDate = new DateTime(startDate.Ticks);

            DateTime toDate = new DateTime(endDate.Ticks);

            bikesAvailability.Inventory = inventoryGroups?.ToArray() ?? GetInventory();

            Appointment[] appointments = DatabaseOperations.GetAppointments();

            while (runningDate <= toDate)
            {
                List <DayPartSelection.DayPart> availableDayParts =
                    new List <DayPartSelection.DayPart>(DayPartSelection.GetAvailableDayParts(runningDate));

                foreach (DayPartSelection.DayPart dayPart in DayPartSelection.AllDayParts)
                {
                    Appointment[] appointmentsTodayAndDayPart =
                        appointments.Where(appointment => appointment.Date.Ticks == runningDate.Ticks &&
                                           appointment.DayPartOverlaps(dayPart))
                        .ToArray();

                    BikeBooking[] bikeBookings
                        = appointmentsTodayAndDayPart.SelectMany(a => a.BikeBookings)
                          .ToArray();

                    InventoryGroup[] newInventoryGroups = bikesAvailability.Inventory.Select(i => new InventoryGroup(i)).ToArray();

                    foreach (InventoryGroup inventoryGroup in newInventoryGroups)
                    {
                        BikeBooking[] overlappingBikeBookings =
                            bikeBookings.Where(b => b.BikeId == inventoryGroup.BikeId &&
                                               b.ModelId == inventoryGroup.ModelId).ToArray();

                        foreach (BikeBooking overlappingBikeBooking in overlappingBikeBookings)
                        {
                            inventoryGroup.Available--;

                            if (inventoryGroup.Wanted > inventoryGroup.Available)
                            {
                                availableDayParts.Remove(dayPart);
                            }
                        }
                    }
                }
                availableDates.Add(new DisplayTime(runningDate, availableDayParts.ToArray()));

                runningDate = runningDate.AddDays(1);
            }

            bikesAvailability.AvailableDates = availableDates.ToArray();

            if (availableDates.Any() == false)
            {
                bikesAvailability.Message = $"The bikes you want are not available in the timeframe you specified";
            }

            Event[] plannedEvents = GetEvents();

            bikesAvailability.BillingCost = new BillingCost();

            if (!(dateSelection == null || plannedEvents.Any(e => e.Date.Ticks == dateSelection.CSharpDayTime.Ticks)))
            {
                bikesAvailability.BillingCost
                    = GetPriceEstimateRental(email, bikesAvailability.Inventory, dateSelection);
            }

            return(bikesAvailability);
        }
예제 #3
0
        public static string MakeReservation(InventoryGroup[] inventoryGroups,
                                             string name,
                                             string email,
                                             string phoneNumber,
                                             DateSelection dateSelection)
        {
            string message = "An unexpected error occured";

            if (string.IsNullOrEmpty(name.Trim()))
            {
                message = "Please enter your name";
                return(message);
            }
            else if (string.IsNullOrEmpty(email.Trim()))
            {
                message = "Please enter your email";
                return(message);
            }
            else if (email.Trim().Count(c => c == '.') != 1 ||
                     email.Trim().Count(c => c == '@') != 1)
            {
                message = "Please enter a valid email address";
                return(message);
            }
            else if (string.IsNullOrEmpty(phoneNumber.Trim()))
            {
                message = "Please enter your phone number";
                return(message);
            }
            else if (phoneNumber.Count(x => Char.IsDigit(x)) != 10 &&
                     phoneNumber.Count(x => Char.IsDigit(x)) != 7)
            {
                message = "Please enter a valid phone number. The form excepts 7 (Fort Collins number) or 10 digits";
                return(message);
            }
            else if (dateSelection == null)
            {
                message = "Please select at least one date";
                return(message);
            }
            else if (inventoryGroups.All(g => g.Wanted == 0))
            {
                message = "Please select some bikes";
                return(message);
            }
            string id = string.Empty;

            BillingCost billingCost = GetPriceEstimateRental(email, inventoryGroups, dateSelection);

            id = DatabaseOperations.InsertAppointment
                     (dateSelection, name.ToString(), email.ToString(), phoneNumber.ToString());



            DatabaseOperations.AddBikeBookings(id, inventoryGroups);

            message = "Booking was successful";

            List <string> messageLines = new List <string>();

            messageLines.Add($"Id: '{id}'");
            messageLines.Add($"name: {name}");
            messageLines.Add($"email: {email}");
            messageLines.Add($"phone: {phoneNumber}");
            messageLines.Add($"price (incl tax): {billingCost.Price+ billingCost.Tax}");
            messageLines.Add($"{dateSelection.Date} {dateSelection.DayPart}");

            messageLines.Add($"Bikes:");
            foreach (InventoryGroup inventoryGroup in inventoryGroups)
            {
                if (inventoryGroup.Wanted > 0)
                {
                    messageLines.Add($"{inventoryGroup.Name}: {inventoryGroup.Model}");
                }
            }

            string renderedFullMessage = messageLines.Aggregate((i, j) => i + "\n" + j);

            string companyEmail = GetCompanyEmailAddress();

            string msg1 = new EmailSender(companyEmail).SendToBikeadelics($"Reservation", renderedFullMessage);

            if (msg1 == EmailSender.MessageAtSuccessfullySentEmail)
            {
                message += $" Email successfully sent to bikeadelic";
            }
            else
            {
                message += $" Unable to sent email to bikeadelic";
            }

            if (string.IsNullOrEmpty(email.ToString()) == false)
            {
                string rentedBikesMessagePart =
                    inventoryGroups.Where(g => g.Wanted > 0)
                    .Select((kvpi, kvpj) => $"{ kvpi.Wanted}  { kvpi.Name}")
                    .Aggregate((i, j) => $"{i} and {j}");

                string daySelectionPartMessage = $"{dateSelection.Date}";

                string GetDropOffTimeByDayPart(string dayPart)
                {
                    return(new Dictionary <string, string>
                    {
                        { DayPartSelection.DayPart.Morning.ToString(), "9.00AM" },
                        { DayPartSelection.DayPart.Afternoon.ToString(), "2.00PM" },
                        { DayPartSelection.DayPart.Day.ToString(), "9.00AM" },
                        { DayPartSelection.DayPart.Evening.ToString(), "4.00PM" }
                    }[dayPart]);
                }

                string dropOffTimePartMessage = dateSelection.DayPart != null?
                                                $" at {GetDropOffTimeByDayPart(dateSelection.DayPart)}":
                                                string.Empty;

                string customerMessage = $"Dear {name} \n\n" +
                                         $"Thank you for your reservation of {rentedBikesMessagePart}. \n\n" +
                                         $"We have you down for {daySelectionPartMessage}. \n\n" +
                                         $"We are looking forward to see you {dropOffTimePartMessage}. \n\n" +
                                         $"Please have cash payment of ${billingCost.Price} + tax ${billingCost.Tax} = ${billingCost.Price + billingCost.Tax} ready when you meet us there \n\n" +
                                         $"We will contact you at {email} or {phoneNumber} to confirm.\n\n" +
                                         $"Thank you for your business and looking forward to meet you.\n\n" +
                                         $"Arjan de Bruijn and Allison Shaw";

                string msg2 = EmailSender.Send(email.ToString(), $"Your reservation", customerMessage);

                if (msg2 == EmailSender.MessageAtSuccessfullySentEmail)
                {
                    message += $" Email successfully sent to {email.ToString()}";
                }
                else
                {
                    message += $" Unable to sent email to {email.ToString()}";
                }
            }
            else
            {
                message += " no email sent to customer, no address provided";
            }
            return(message);
        }