Exemple #1
0
        /// <summary>
        /// Creates new booking object in the database and sends an email with confirmation of the booking, if the conditions are met.
        /// </summary>
        public async void CreateBookingAsync()
        {
            bool bookedAlready = true;

            foreach (var item in BookingCatalogSingleton.Instance.Bookings)
            {
                if (item.User_Id == LoginHandler.SelectedUser.User_Id && item.Date ==
                    RoomReference.Date && item.Time_end >= RoomReference.TimeStart && item.Time_start <= RoomReference.TimeEnd && LoginHandler.SelectedUser.Teacher == false)
                {
                    DialogHandler.Dialog("Du har allerede booket et lokale på denne dato i samme tidsinterval. Vælg venligst et nyt tidspunkt.", "Overlappende booking");
                    bookedAlready = false;
                    break;
                }
            }

            if (LoginHandler.SelectedUser.Teacher)
            {
                if (ThreeRoomsBookingLimit() == false)
                {
                    bookedAlready = false;
                    DialogHandler.Dialog("Du kan ikke have mere end tre bookinger af gangen, hvis du vil booke dette rum må du slette en anden booking", "Kun tre bookinger");
                }
            }
            if (bookedAlready)
            {
                // I don't know why, but we need this reference to get the RoomName property in the RoomsView model.
                RoomsView selectedRoomsViewRef = RoomReference.SelectedRoomsView;
                var       result = await DialogHandler.GenericYesNoDialog("Er du sikker på du vil booke dette lokale?", "Book lokale?", "Ja, tak", "Nej, tak");

                if (CompareDatesAndTime() && result)
                {
                    Booking booking = new Booking()
                    {
                        Date       = RoomReference.Date.Date,
                        Room_Id    = selectedRoomsViewRef.Room_Id,
                        Time_start = new TimeSpan(RoomReference.TimeStart.Hours, RoomReference.TimeStart.Minutes, 0),
                        Time_end   = new TimeSpan(RoomReference.TimeEnd.Hours, RoomReference.TimeEnd.Minutes, 0),
                        User_Id    = LoginHandler.SelectedUser.User_Id
                    };

                    await PersistancyService.SaveInsertAsJsonAsync(booking, "Bookings");

                    BookingCatalogSingleton.Instance.Bookings.Clear();
                    await BookingCatalogSingleton.Instance.LoadbookingsAsync();

                    FilterSearchMethodAsync();
                    // Does not need to be awaited, since it doesn't disrupt the flow of the program.
                    MailService.MailSenderAsync(LoginHandler.SelectedUser.User_Email, "Kvittering på booking", $"Du har booket {selectedRoomsViewRef.RoomName} " +
                                                $"d. {RoomReference.Date.ToString("dd/MM/yyyy")} " +
                                                $"mellem {new DateTime(RoomReference.TimeStart.Ticks).ToString("HH:mm")} og {new DateTime(RoomReference.TimeEnd.Ticks).ToString("HH:mm")}.", true);
                    RoomReference.SelectedRoomsView = null;
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// This method replaces every booking on a room in between the selected timeend and timestart
        /// </summary>
        /// <returns></returns>
        public async Task TeacherSnatchRoom()
        {
            var query = (from b in BookingCatalogSingleton.Instance.Bookings
                         where b.Room_Id == TCPREF.BookingIsSelected.Room_Id && TCPREF.BookingIsSelected.Date.Date == b.Date.Date && b.Time_end >= TCPREF.InputTimeStart && b.Time_start <= TCPREF.InputTimeEnd
                         select b).ToList();

            if (query.Count > 0)
            {
                foreach (var item in query)
                {
                    PersistancyService.DeleteFromDatabaseAsync("Bookings", item.Booking_Id);

                    await GetMailToUser(item.User_Id, "En lærer aflyste din booking", $"Din booking den " + $"{TCPREF.BookingIsSelected.Date.ToString("dd/MM/yyyy")} fra " + $"{new DateTime(TCPREF.BookingIsSelected.BookingStart.Ticks).ToString("HH:mm")} til " + $"{new DateTime(TCPREF.BookingIsSelected.BookingEnd.Ticks).ToString("HH:mm")} i rum {TCPREF.BookingIsSelected.RoomName} " + $"er blevet aflyst af {LoginHandler.SelectedUser.User_Name}, vi beklager ulejligheden. " + $"Du er selvfølgelig velkommen til at booke et nyt rum i appen.", true);
                }
                await PersistancyService.SaveInsertAsJsonAsync(new Booking
                {
                    Date          = TCPREF.BookingIsSelected.Date.Date,
                    Room_Id       = TCPREF.BookingIsSelected.Room_Id,
                    TavleBookings = null,
                    Time_start    = new TimeSpan(TCPREF.InputTimeStart.Hours, TCPREF.InputTimeStart.Minutes, 0),
                    Time_end      = new TimeSpan(TCPREF.InputTimeEnd.Hours, TCPREF.InputTimeEnd.Minutes, 0),
                    User_Id       = LoginHandler.CurrentUserId
                }, "Bookings");

                MailService.MailSenderAsync(LoginHandler.SelectedUser.User_Email, "Kvittering på booking", $"Du har booket {TCPREF.BookingIsSelected.RoomName} " + $"d. {TCPREF.BookingIsSelected.Date.ToString("dd/MM/yyyy")} " + $"mellem {new DateTime(TCPREF.BookingIsSelected.BookingStart.Ticks).ToString("HH:mm")} og {new DateTime(TCPREF.BookingIsSelected.BookingEnd.Ticks).ToString("HH:mm")}.", true);
                ResetList();
                DialogHandler.Dialog("Din booking er nu oprettet. God dag!", "Booking Oprettet!");
            }

            if (query.Count <= 0)
            {
                var result = await DialogHandler.GenericYesNoDialog(
                    "Der er ikke nogen booking på dit valgte tidspunkt, er du sikker på at du valgte det rigtige tidspunkt?",
                    "Ingen booking på valgte tidspunkt", "Opret Ny booking i stedet", "Indtast tid igen");

                if (result)
                {
                    await PersistancyService.SaveInsertAsJsonAsync(new Booking
                    {
                        Date          = TCPREF.BookingIsSelected.Date.Date,
                        Room_Id       = TCPREF.BookingIsSelected.Room_Id,
                        TavleBookings = null,
                        Time_start    = new TimeSpan(TCPREF.InputTimeStart.Hours, TCPREF.InputTimeStart.Minutes, 0),
                        Time_end      = new TimeSpan(TCPREF.InputTimeEnd.Hours, TCPREF.InputTimeEnd.Minutes, 0),
                        User_Id       = LoginHandler.CurrentUserId
                    }, "Bookings");

                    MailService.MailSenderAsync(LoginHandler.SelectedUser.User_Email, "Kvittering på booking", $"Du har booket {TCPREF.BookingIsSelected.RoomName} " + $"d. {TCPREF.BookingIsSelected.Date.ToString("dd/MM/yyyy")} " + $"mellem {new DateTime(TCPREF.BookingIsSelected.BookingStart.Ticks).ToString("HH:mm")} og {new DateTime(TCPREF.BookingIsSelected.BookingEnd.Ticks).ToString("HH:mm")}.", true);
                    ResetList();
                    DialogHandler.Dialog("Din booking er nu oprettet. God dag!", "Booking Oprettet!");
                }
            }
        }
        /// <summary>
        /// Creates a new account and it checks if everything has been filled in if it has it creates a new user
        /// and sends it to PersistancyService.SaveInsertAsJsonAsync then refreshes the UserCatalogSingleton
        /// and then navigates to PageLogin so the user can login.
        /// </summary>
        private async void CreateAccount()
        {
            if (UserName != null && Email != null && Password != null)
            {
                if (EmailAvailability(Email))
                {
                    DialogHandler.Dialog("Den email er allerede brugt", "Oprettelse fejlet");
                }
                else
                {
                    await PersistancyService.SaveInsertAsJsonAsync(new User()
                    {
                        User_Name = UserName, User_Email = Email, Password = Password, Teacher = IsTeacher
                    }, "Users");

                    UserCatalogSingleton.Instance.LoadUsersAsync();
                    ((Frame)Window.Current.Content).Navigate(typeof(PageLogin));
                }
            }
            else
            {
                DialogHandler.Dialog("Mangler at udfylde brugernavn, email eller password", "Fejlet oprettelse");
            }
        }
        /// <summary>
        /// Method that posts the tavlebooking to the database, after it has checked if its possible in the chosen timespan.
        /// </summary>
        /// <param name="tavleEndTime">The total time for the selected tavlebooking (Comes from the Booktavle Method)</param>
        /// <param name="myTavleBooking">The chosen tavlebooking. This value comes from the SelectedDuration, and SelectedTavleStartTime properties</param>
        /// <returns></returns>
        public static async Task BookTavle(TimeSpan tavleEndTime, TavleBooking myNewTavleBooking)
        {
            AllBookingsView tempSelectedBooking = Reference.SelectedBooking;
            var             doesUserHaveAnyTavleBookingsForThisRoom = (from t in Reference.Tavlebookings
                                                                       select t).Where(x => x.Booking_Id == Reference.SelectedBooking.Booking_Id).ToList();

            if (doesUserHaveAnyTavleBookingsForThisRoom.Count > 0)
            {
                DialogHandler.Dialog("Det er ikke muligt at booke flere end 1 tavle\nSlet venligst eksisterende tavler og book derefter igen.", "For mange bookede tavler");
                return;
            }
            else
            {
                if (Reference.SelectedBooking.Type == "Klasselokale")
                {
                    var numberOfTavleBookingsForThisRoomOnThatDay = (from t in Reference.Tavlebookings
                                                                     join b in AllBookingsViewCatalogSingleton.Instance.AllBookings on t.Booking_Id equals b.Booking_Id
                                                                     select new
                    {
                        BookingId = t.Booking_Id,
                        UserId = b.User_Id,
                        RoomId = b.Room_Id,
                        BookingDate = b.Date,
                        BookingStart = b.BookingStart,
                        BookingEnd = b.BookingEnd,
                        TavleId = t.Tavle_Id,
                        TavleStart = t.Time_start,
                        TavleEnd = t.Time_end
                    }).Where(x => Reference.SelectedBooking.Room_Id == x.RoomId && Reference.SelectedBooking.Date == x.BookingDate).ToList();
                    if (numberOfTavleBookingsForThisRoomOnThatDay.Count > 0 && numberOfTavleBookingsForThisRoomOnThatDay.Count <= 2)
                    {
                        var checkTavleTime = (from t in numberOfTavleBookingsForThisRoomOnThatDay
                                              select t).Where(x => (Reference.SelectedTavleStartTime + TimeSpan.FromSeconds(1)) <= x.TavleEnd && (tavleEndTime - TimeSpan.FromSeconds(1)) >= x.TavleStart).ToList();
                        if (checkTavleTime.Count == 0)
                        {
                            // INSERT
                            if (await DialogHandler.GenericYesNoDialog("Er du sikker på du vil booke denne tavletid?\nKvittering på tavlen vil blive tilsendt via mail", "Book Book Tavle?", "Ja", "Fortryd"))
                            {
                                await PersistancyService.SaveInsertAsJsonAsync(myNewTavleBooking, "TavleBookings");

                                MailService.MailSenderAsync(LoginHandler.SelectedUser.User_Email, "Kvittering på booking af tavletid", $"Du har booked tavlen i rummet {Reference.SelectedBooking.RoomName}" +
                                                            $"d. {Reference.SelectedBooking.Date.ToString("dd/MM/yyyy")} " +
                                                            $"mellem {new DateTime(myNewTavleBooking.Time_start.Ticks).ToString("HH:mm")} og {new DateTime(myNewTavleBooking.Time_end.Ticks).ToString("HH:mm")}.", true);
                            }
                            else
                            {
                                return;
                            }
                        }
                        else
                        {
                            DialogHandler.Dialog("Denne tid modstrider en anden tavle booking\nVælg venligst en tidligere eller senere tid", "Modstridende tider");
                        }
                    }
                    else
                    {
                        // INSERT
                        if (await DialogHandler.GenericYesNoDialog("Er du sikker på du vil booke denne tavletid?\nKvittering på tavlen vil blive tilsendt via mail", "Book Book Tavle?", "Ja", "Fortryd"))
                        {
                            await PersistancyService.SaveInsertAsJsonAsync(myNewTavleBooking, "TavleBookings");

                            MailService.MailSenderAsync(LoginHandler.SelectedUser.User_Email, "Kvittering på booking af tavletid", $"Du har booked tavlen i rummet {Reference.SelectedBooking.RoomName}" +
                                                        $"d. {Reference.SelectedBooking.Date.ToString("dd/MM/yyyy")} " +
                                                        $"mellem {new DateTime(myNewTavleBooking.Time_start.Ticks).ToString("HH:mm")} og {new DateTime(myNewTavleBooking.Time_end.Ticks).ToString("HH:mm")}.", true);
                        }
                        else
                        {
                            return;
                        }
                    }
                }
            }
            RefreshLists();
            Reference.SelectedBooking = tempSelectedBooking;
        }
        /// <summary>
        /// This method books the selected room again tomorrow, if that is possible.
        /// </summary>
        public static async Task BookAgainTomorrowMethodAsync()
        {
            // Retrieves the day after the selected booking date
            DateTime tomorrow = Reference.SelectedBooking.Date.AddDays(1);
            // The copied booking that needs to be inserted into the database with the updated date.
            Booking updatedBooking = new Booking()
            {
                User_Id    = Reference.SelectedUser.User_Id,
                Room_Id    = Reference.SelectedBooking.Room_Id,
                Date       = tomorrow,
                Time_start = Reference.SelectedBooking.BookingStart,
                Time_end   = Reference.SelectedBooking.BookingEnd
            };
            // This object will be set to the returned booking that gets posted to the database and later used in the AllBookingsView object
            Booking returnedObj = null;
            // This object is the view object that gets added to the singleton, to that the view will be updated.-
            AllBookingsView viewToAdd = null;
            // Checks how many instances there is of this selectedbooking's specific room.
            var howManyOfThisRoomTomorrowQuery = (from b in AllBookingsViewCatalogSingleton.Instance.AllBookings
                                                  select b).Where(x => Reference.SelectedBooking.Room_Id == x.Room_Id && x.Date == tomorrow).ToList();

            if (howManyOfThisRoomTomorrowQuery.Count == 2)
            {
                // checks if there is any instances that overlaps the selectedbookings's time
                var checkTime = (from b in howManyOfThisRoomTomorrowQuery
                                 select b).Where(x => Reference.SelectedBooking.BookingStart > x.BookingStart && Reference.SelectedBooking.BookingStart <x.BookingEnd || Reference.SelectedBooking.BookingEnd> x.BookingStart && Reference.SelectedBooking.BookingEnd < x.BookingEnd).ToList();
                // If 0 or less
                if (checkTime.Count < 1)
                {
                    var result = await DialogHandler.GenericYesNoDialog("Er du sikker på du vil booke dette rum igen imorgen samme tid?\nKvittering på bookning af det nye rum vil blive tilsendt via mail", "Book igen imorgen?", "Ja", "Fortryd");

                    if (result)
                    {
                        // Inserts the selectedbooking into the database and updates the singleton
                        returnedObj = await PersistancyService.SaveInsertAsJsonAsync(updatedBooking, "Bookings");

                        MailService.MailSenderAsync(LoginHandler.SelectedUser.User_Email, "Kvittering på booking af rum", $"Du har booked rummet {Reference.SelectedBooking.RoomName} igen for " +
                                                    $"d. {returnedObj.Date.ToString("dd/MM/yyyy")} " +
                                                    $"mellem {new DateTime(returnedObj.Time_start.Ticks).ToString("HH:mm")} og {new DateTime(returnedObj.Time_end.Ticks).ToString("HH:mm")}.", true);
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    // Error message that displays if there already exists a booking in the database that overlaps with the selectedbooking on the day after the selectedbooking date
                    DialogHandler.Dialog("Denne booking kan ikke bookes imorgen\nda den overlapper eksisterende bookninger", "Overlappende Bookninger");
                }
            }
            else
            {
                var result = await DialogHandler.GenericYesNoDialog("Er du sikker på du vil booke dette rum igen imorgen samme tid?\nKvittering på bookning af det nye rum vil blive tilsendt via mail", "Book igen imorgen?", "Ja", "Fortryd");

                if (result)
                {
                    // Inserts the selectedbooking into the database and updates the singleton
                    returnedObj = await PersistancyService.SaveInsertAsJsonAsync(updatedBooking, "Bookings");

                    MailService.MailSenderAsync(LoginHandler.SelectedUser.User_Email, "Kvittering på booking af rum", $"Du har booked rummet {Reference.SelectedBooking.RoomName} igen for " +
                                                $"d. {returnedObj.Date.ToString("dd/MM/yyyy")} " +
                                                $"mellem {new DateTime(returnedObj.Time_start.Ticks).ToString("HH:mm")} og {new DateTime(returnedObj.Time_end.Ticks).ToString("HH:mm")}.", true);
                }
                else
                {
                    return;
                }
            }
            if (returnedObj != null)
            {
                viewToAdd = new AllBookingsView()
                {
                    RoomName        = Reference.SelectedBooking.RoomName,
                    Date            = tomorrow,
                    Booking_Id      = returnedObj.Booking_Id,
                    BookingStart    = Reference.SelectedBooking.BookingStart,
                    BookingEnd      = Reference.SelectedBooking.BookingEnd,
                    Room_Id         = Reference.SelectedBooking.Room_Id,
                    Floor           = Reference.SelectedBooking.Floor,
                    No              = Reference.SelectedBooking.No,
                    Name            = Reference.SelectedBooking.Name,
                    Building_Letter = Reference.SelectedBooking.Building_Letter,
                    Type            = Reference.SelectedBooking.Type,
                    User_Id         = Reference.SelectedBooking.User_Id
                };
                // Adds the viewToAdd object, to the singleton
                Reference.AllUserBookingsFromSingleton.Add(viewToAdd);
                // Refreshes the singleton, and re-queries the bookings for the selected user
                RefreshLists();
                // sets the selected booking to the newly added booking
                Reference.SelectedBooking = Reference.AllUserBookingsFromSingleton.First();
            }
        }