/// <summary>
 /// ログアウト時に呼び出す
 /// </summary>
 public void Logout()
 {
     currentUser = null;
     this.labelUserName.Content = "Guest";
     this.LoginButton.Content = "Login";
     this.lendingHistoryButton.IsEnabled = false;
 }
 /// <summary>
 /// LibraryManagementSystem.LoginDelegateのメソッド
 /// </summary>
 /// <param name="user">ログインしたユーザ</param>
 public void Login(User user)
 {
     Console.WriteLine(user.Name);
     currentUser = user;
     this.labelUserName.Content = currentUser.Name;
     this.LoginButton.Content = "Logout";
     this.lendingHistoryButton.IsEnabled = true;
 }
 /// <summary>
 /// コンストラクタ
 /// </summary>
 /// <param name="_user"></param>
 public LendingHistoryWindow(User _user)
 {
     InitializeComponent();
     user = _user;
     var history = LendingHistoryRecord.GetUnreturnedBookFromUser(_user);
     lendingHistoryToBeDisplayed =
         new ObservableCollection<LendingHistoryRecord>(history);
     this.dataGrid.ItemsSource = lendingHistoryToBeDisplayed;
 }
 /// <summary>
 /// DBから履歴取得時にインスタンス初期化するため
 /// </summary>
 /// <param name="_id"></param>
 /// <param name="_user"></param>
 /// <param name="_book"></param>
 /// <param name="_returnDate"></param>
 /// <param name="_completionDate"></param>
 /// <param name="_created_at"></param>
 /// <param name="_edited_at"></param>
 private LendingHistoryRecord(
     string _id,
     User _user,
     Book _book,
     string _returnDate,
     string _completionDate,
     string _created_at,
     string _edited_at
     )
     : base(_id, _created_at, _edited_at)
 {
     this.user = _user;
     this.book = _book;
     this.returnDate = _returnDate;
     this.completionDate = _completionDate;
 }
        /// <summary>
        /// 本の詳細
        /// </summary>
        /// <param name="_book">詳細を表示する図書</param>
        /// <param name="_user">ログインしていればユーザのデータ、ログインしていなければnull</param>
        public BookDetailsWindow(Book _book, User _user)
        {
            InitializeComponent();
            book = _book;
            user = _user;

            if (_book == null)
            {
                return;
            }

            if (_user == null)
            {
                InitOfGuestOnly();
            }

            var history = LendingHistoryRecord.GetDueDateOfBook(_book);
            if (history.Count == 1)
            {
                CanNotBorrow(history[0].DueDate);
            }
            else
            {
                this.dueDateContent = "You can borrow.";
            }

            this.DataContext = new {
                Title = book.Title,
                Author = book.Author,
                Isbn = book.Isbn,
                Publisher = book.Publisher,
                LendInfo = dueDateContent,
                Caption = book.Caption,
            };
            if (book.ImageUrl != null)
            {
                this.imageBox.Source = new BitmapImage(new Uri(book.ImageUrl));
            }
        }
        /// <summary>
        /// ユーザの未返却本一覧をDBから取得
        /// </summary>
        /// <param name="_user">ユーザ</param>
        /// <returns>取得した貸出履歴のリスト</returns>
        public static List<LendingHistoryRecord> GetUnreturnedBookFromUser(User _user)
        {
            List<LendingHistoryRecord> result = new List<LendingHistoryRecord>();
            using (SQLiteConnection cn = new SQLiteConnection(dbConStr))
            {
                cn.Open();
                SQLiteCommand cmd = cn.CreateCommand();
                cmd.CommandText = "SELECT * FROM " + TABLE_NAME
                    + " INNER JOIN book ON lending_history.book_id = book.id"
                    + " WHERE completion_date IS NULL"
                    + " AND user_id = @USER_ID"
                    + ";";

                cmd.Parameters.Add(new SQLiteParameter("@USER_ID", _user.Id));
                using (SQLiteDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Book _book = new Book(
                        reader[7].ToString(),   // id
                        reader[8].ToString(),   // isbn
                        reader[9].ToString(),   // title
                        reader[10].ToString(),  // author
                        reader[11].ToString(),  // publisher
                        reader[12].ToString(),  // series
                        reader[13].ToString(),  // caption
                        reader[14].ToString(),  // image_url
                        reader[15].ToString(),  // created_at
                        reader[16].ToString()   // edited_at
                        );
                        LendingHistoryRecord history = new LendingHistoryRecord(
                            reader[0].ToString(),   // id
                            _user,
                            _book,
                            reader[3].ToString(),   // return_date
                            reader[4].ToString(),   // completion_date
                            reader[5].ToString(),   // created_at
                            reader[6].ToString()    // edited_at
                            );
                        history.Show();
                        result.Add(history);
                    }
                }
                cn.Close();
            }
            return result;
        }
 /// <summary>
 /// 貸し出し情報を追加する
 /// </summary>
 /// <param name="_book">貸し出す本</param>
 /// <param name="_user">貸し出す相手のアカウント</param>
 /// <returns>成功か否か</returns>
 public static Result Create(Book _book, User _user)
 {
     return Create(_book, _user.Id);
 }
        /// <summary>
        /// 登録ボタンが押された時の処理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonRegister_Click(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("ButtonRegister_Clicked");
            bool invaliedFlag = false;
            string name, email, password;

            if ((name = this.textBoxName.Text) == string.Empty)
            {
                invaliedFlag = true;
            }
            if ((email = this.textBoxEmail.Text) == string.Empty)
            {
                invaliedFlag = true;
            }
            if ((password = this.passwordBox1.Password) == string.Empty)
            {
                invaliedFlag = true;
            }
            if (this.passwordBox2.Password == string.Empty)
            {
                invaliedFlag = true;
            }
            if (password != this.passwordBox2.Password)
            {
                invaliedFlag = true;
            }

            if (invaliedFlag)
            {
                Console.WriteLine("InvaliedFlag");
                return;
            }
            Console.WriteLine("Success");
            User user = new User();
            user.Name = name;
            user.Email = email;
            user.Password = password;
            user = User.Save(user);
            if (user != null)
            {
                registrationAccountDelegate(user);
            }

            this.Close();
        }
 public void AddUser(User user)
 {
     usersToBeDisplayed.Add(user);
 }
 /// <summary>
 /// ダブルクリックされたユーザのパスワード入力を求めるウィンドウを表示する
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void dataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
 {
     User user = this.dataGrid.SelectedItem as User;
     selectedUser = user;
     if (user == null)
     {
         return;
     }
     LoginWindow loginWindow = new LoginWindow();
     loginWindow.requestUser = user;
     loginWindow.successfulLoginDelegate = SuccessfulLogin;
     loginWindow.ShowDialog();
 }
        /// <summary>
        /// 新規ユーザをDBに保存する
        /// </summary>
        /// <param name="user">保存するユーザデータ</param>
        /// <returns></returns>
        public static User Save(User user)
        {
            if (user.name == null || user.hashedPassword == null || user.email == null)
            {
                return null;
            }

            User newUser = null;
            using (SQLiteConnection cn = new SQLiteConnection(dbConStr))
            {
                cn.Open();
                SQLiteCommand cmd = cn.CreateCommand();
                cmd.CommandText =
                    "INSERT INTO "
                    + TABLE_NAME + " (name, email, password, created_at, edited_at) "
                    + "VALUES (@NAME, @EMAIL, @PASSWORD, DATETIME('now'), DATETIME('now'))";

                cmd.Parameters.Add(new SQLiteParameter("@NAME", user.name));
                cmd.Parameters.Add(new SQLiteParameter("@EMAIL", user.email));
                cmd.Parameters.Add(new SQLiteParameter("@PASSWORD", user.hashedPassword));
                cmd.ExecuteNonQuery();
                cmd.CommandText =
                    "SELECT * FROM user WHERE ROWID = last_insert_rowid();";
                cmd.Parameters.Add(new SQLiteParameter("@Name", user.name));
                using (SQLiteDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        newUser = new User(
                            reader["id"].ToString(),
                            reader["name"].ToString(),
                            reader["email"].ToString(),
                            reader["password"].ToString(),
                            reader["created_at"].ToString(),
                            reader["edited_at"].ToString()
                            );
                        newUser.Show();
                    }
                }
                cn.Close();
            }
            return newUser;
        }
 /// <summary>
 /// DBに存在するユーザデータを全て取得してユーザリストにして返す
 /// </summary>
 /// <returns>ユーザのリスト</returns>
 public static List<User> GetAllUsers()
 {
     List<User> users = new List<User>();
     using (SQLiteConnection cn = new SQLiteConnection(dbConStr))
     {
         cn.Open();
         SQLiteCommand cmd = cn.CreateCommand();
         cmd.CommandText = "SELECT * FROM " + TABLE_NAME;
         using (SQLiteDataReader reader = cmd.ExecuteReader())
         {
             while (reader.Read())
             {
                 User user = new User(
                     reader["id"].ToString(),
                     reader["name"].ToString(),
                     reader["email"].ToString(),
                     reader["password"].ToString(),
                     reader["created_at"].ToString(),
                     reader["edited_at"].ToString()
                     );
                 users.Add(user);
             }
         }
         cn.Close();
     }
     return users;
 }