Ejemplo n.º 1
0
        // No Observable collection is used because users are not to modify data
        private List <Book> GetBookList()
        {
            // The book list to be returned
            List <Book> bookList;

            // Load book collection from database
            using (var db = new BibliophileContext())
            {
                db.Books.Load();
                bookList = db.Books.Local.ToList();
            }

            return(bookList);
        }
Ejemplo n.º 2
0
        // Find and return the admin from the database that matches username and password
        private Admin GetAdmin(string username, string password)
        {
            Admin admin = null;

            using (var db = new BibliophileContext())
            {
                // Find the admin employee in the database that matches username and password
                admin = (from user in db.Users
                         where user is Admin
                         select user as Admin).AsEnumerable().FirstOrDefault(a => a.UserName == username && PasswordHasher.VerifyPassword(password, a.PassWord));
            }

            return(admin);
        }
Ejemplo n.º 3
0
        private void Request_Button_Click(object sender, RoutedEventArgs e)
        {
            // Check there's a book selection
            if (datagrid.SelectedIndex > -1)
            {
                // get the user id and email
                string strUserId = (DataContext as UserMainWindowViewModel).UserId;
                string email     = (DataContext as UserMainWindowViewModel).Email;

                // Validate for request information
                if (string.IsNullOrWhiteSpace(strUserId) || string.IsNullOrWhiteSpace(email) || !int.TryParse(strUserId, out int userid))
                {
                    MessageBox.Show("Enter a valid User Card Id and associated Email", "Error", MessageBoxButton.OK);
                    return;
                }

                // get the selected book
                int bookid = (datagrid.SelectedItem as Book).BookId.Value;

                // Search the database for the user and book
                using (var db = new BibliophileContext())
                {
                    // Find user with requested user id and email
                    User user = db.Users.FirstOrDefault(u => u.UserId == userid && u.Email == email);

                    // Check the user exist with such userid and email
                    if (user == null)
                    {
                        MessageBox.Show("User not found!", "Error", MessageBoxButton.OK);
                        return;
                    }

                    // Check the number of book copies this user has is not more than what's allowed
                    if (user.Books.Count >= User.MAXCOPIES)
                    {
                        MessageBox.Show($"Users cannot checkout more than {User.MAXCOPIES} books", "Error", MessageBoxButton.OK);
                        return;
                    }

                    // Find the selected book in the databse
                    Book book = db.Books.Find(bookid);

                    // Check if there are enough copies of the book available
                    if (book.AvailableCopies > 0)
                    {
                        // Check out the book
                        user.Books.Add(book);
                        book.Users.Add(user);
                        book.AvailableCopies--;
                        db.SaveChanges();

                        MessageBox.Show("Your copy has been reserved", "Info", MessageBoxButton.OK);
                    }
                    else
                    {
                        // This is a pending feature that is not yet implemented
                        // There will be a list of pending request and users are notified by email when a copy of the book
                        // they want becomes available

                        MessageBox.Show("There are not enough copies of the book. Your request is pending", "Info", MessageBoxButton.OK);
                    }
                }
            }
            else
            {
                MessageBox.Show("Please make a book selection", "Error", MessageBoxButton.OK);
                return;
            }
        }
Ejemplo n.º 4
0
        public AdminMainWindow(int adminId)
        {
            InitializeComponent();

            // Create the databse context of this window
            db = new BibliophileContext();

            // When the window closes save any pending changes and dispose of the context
            Closed += (sender, e) =>
            {
                try
                {
                    db.SaveChanges();
                }
                catch (Exception) { MessageBox.Show("Something went wrong while closing the window", "Error", MessageBoxButton.OK); }

                db.Dispose();
            };

            // Initialize the viewmodel of this window
            viewmodel = new AdminWindowViewModel();

            // Load the data sets
            db.Users.Load ();
            db.Books.Load ();

            // Select the two main lists to be use for the view model of the window
            viewmodel.Users = db.Users.Local;
            viewmodel.Books = db.Books.Local;

            // Select the two custom queries from the user list. One for the Admins, the other one for the checkouts
            viewmodel.Admins = new ObservableCollection<Admin>(from u in viewmodel.Users
                                                               where u is Admin
                                                               select u as Admin);

            viewmodel.Checkouts = new ObservableCollection<Tuple<User, Book>>(from u in viewmodel.Users
                                                                              from b in u.Books
                                                                              select Tuple.Create(u, b));

            // When the user collection changes we check if there was an admin employee and we add it to the query
            viewmodel.Users.CollectionChanged += (sender, e) =>
            {
                if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    foreach (var user in e.NewItems)
                        if (user is Admin admin)
                            viewmodel.Admins.Add(admin);
                }
            };

            DataContext = viewmodel;

            // Find the admin owner of this window by Id
            windowAdmin = viewmodel.Admins.FirstOrDefault(a => a.UserId == adminId);

            // Check such admin exist
            if (windowAdmin != null)
                Title = $"Logged in as {windowAdmin.UserName}";
            else
            {
                throw new Exception("Exception. Only admin employees can open this window");
            }
        }