public static BookShelf GetBookShelfById(string id)//按Id取得特定的书架
 {
     using (var db = new BookShelfContext())
     {
         return(AllShelves(db).FirstOrDefault(o => o.BookShelfId == id));
     }
 }
 public static List <Book> AllBooks()//取得所有的书
 {
     using (var db = new BookShelfContext())
     {
         return(db.Books.ToList());
     }
 }
 public static List <Book> GetAllLentBooks(Client client)//取得所有已借阅的书
 {
     using (var db = new BookShelfContext())
     {
         return(db.Books.Where(o => o.State == "已被借阅").Where(p => p.ClientName == client.Name).ToList());
     }
 }
Ejemplo n.º 4
0
 public static List <Client> AllClients()//取得所有的用户
 {
     using (var db = new BookShelfContext())
     {
         return(db.Clients.ToList());
     }
 }
 public static void RemoveBooks(string bookId)//删除书籍(对单本书进行操作)
 {
     using (var db = new BookShelfContext())
     {
         var oldBooks = db.Books.Where(book => book.BookId == bookId);
         db.Books.RemoveRange(oldBooks);
         db.SaveChanges();
     }
 }
 private static void RemoveBooksFromShelf(string bookShelfId)//删除书籍(对整个书架进行操作),可能无用
 {
     using (var db = new BookShelfContext())
     {
         var oldBooks = db.Books.Where(book => book.BookShelfId == bookShelfId);
         db.Books.RemoveRange(oldBooks);
         db.SaveChanges();
     }
 }
 public static void UpdateBook(Book newBook)//更新数据库内的书籍
 {
     RemoveBooks(newBook.BookId);
     using (var db = new BookShelfContext())
     {
         db.Entry(newBook).State = EntityState.Modified;
         db.Books.Add(newBook);
         db.SaveChanges();
     }
 }
 public static void UpdateShelf(BookShelf newBookShelf)//更新数据库内的书架
 {
     RemoveBooksFromShelf(newBookShelf.BookShelfId);
     using (var db = new BookShelfContext())
     {
         db.Entry(newBookShelf).State = EntityState.Modified;
         db.Books.AddRange(newBookShelf.Books);
         db.SaveChanges();
     }
 }
 public static List <BookShelf> GetAllShelfs()//取得数据库内全部的书架
 {
     using (var db = new BookShelfContext())
     {
         var query = AllShelves(db).ToList();
         foreach (var shelf in query)
         {
             shelf.SetNum();
         }
         return(query);
     }
 }
Ejemplo n.º 10
0
 public static void AddAdministrator(Client admin)//添加管理员
 {
     try
     {
         using (var db = new BookShelfContext())
         {
             db.Clients.Add(admin);
             db.SaveChanges();
         }
     }
     catch (Exception e)
     {
         throw new ApplicationException($"添加管理员错误: {e.Message}");
     }
 }
 public static List <Book> GetAllAppointedBooks(Client client)//取得所有预约的书
 {
     using (var db = new BookShelfContext())
     {
         List <Book> appointed = new List <Book>();
         foreach (var b in db.Books.Where(o => o.State == "已被借阅").ToList())
         {
             if (b.Appointers.Contains(client.Name))
             {
                 appointed.Add(b);
             }
         }
         return(appointed);
     }
 }
Ejemplo n.º 12
0
        private void Search(int i, string key)//查询方法,i表示具体的排序规则,完成
        {
            List <Book> searchBooks = new List <Book>();

            using (var db = new BookShelfContext())
            {
                switch (i)
                {
                case 1:    //按书号查询
                    foreach (var book in BookShelfService.AllBooks().Where(o => o.BookId.Contains(key) == true))
                    {
                        searchBooks.Add(book);
                    }
                    break;

                case 2:    //书名
                    foreach (var book in BookShelfService.AllBooks().Where(o => o.Name.Contains(key) == true))
                    {
                        searchBooks.Add(book);
                    }
                    break;

                case 3:    //作者
                    foreach (var book in BookShelfService.AllBooks().Where(o => o.Author.Contains(key) == true))
                    {
                        searchBooks.Add(book);
                    }
                    break;

                case 4:    //分类
                    foreach (var book in BookShelfService.AllBooks().Where(o => o.Sort == key))
                    {
                        searchBooks.Add(book);
                    }
                    break;

                case 5:    //书架号
                    foreach (var book in BookShelfService.AllBooks().Where(o => o.BookShelfId == key))
                    {
                        searchBooks.Add(book);
                    }
                    break;
                }
                booksBindingSource.DataSource = new List <Book>();
                booksBindingSource.ResetBindings(false);
                booksBindingSource.DataSource = searchBooks;
            }
        }
 private static void ClearBooks()
 {
     try
     {
         using (var db = new BookShelfContext())
         {
             var oldBooks = db.Books.Where(book => book.BookShelfId == null);
             db.Books.RemoveRange(oldBooks);
             db.SaveChanges();
         }
     }
     catch (Exception e)
     {
         MessageBox.Show("删除书架错误!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
         throw new ApplicationException($"删除书架错误!");
     }
 }
 public static void RemoveBookShelf(string id)
 {
     try
     {
         using (var db = new BookShelfContext())
         {
             var shelf = db.BookShelves.Include("Books").Where(o => o.BookShelfId == id).FirstOrDefault();
             db.BookShelves.Remove(shelf);
             db.SaveChanges();
             ClearBooks();
         }
     }
     catch (Exception e)
     {
         MessageBox.Show("删除书架错误!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
         throw new ApplicationException($"删除书架错误!");
     }
 }
 public static BookShelf AddBookShelf(BookShelf shelf)//添加新的书架
 {
     try
     {
         using (var db = new BookShelfContext())
         {
             db.BookShelves.Add(shelf);
             db.SaveChanges();
             MessageBox.Show("已成功添加书架!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
         return(shelf);
     }
     catch (Exception e)
     {
         MessageBox.Show("添加书架错误!" + e.ToString(), "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
         throw new ApplicationException($"添加书架错误: {e.Message}");
     }
 }
Ejemplo n.º 16
0
 public static bool AddClient(Client client)//添加新的用户
 {
     try
     {
         using (var db = new BookShelfContext())
         {
             db.Clients.Add(client);
             db.SaveChanges();
             MessageBox.Show("已成功注册账号!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
         return(true);
     }
     catch (Exception e)
     {
         MessageBox.Show("注册账号错误!" + e.ToString(), "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
         throw new ApplicationException($"添加书架错误: {e.Message}");
     }
 }
Ejemplo n.º 17
0
        private void Query(int i)//排序方法,i表示具体的排序规则,完成
        {
            List <Book> queryBooks = new List <Book>();

            using (var db = new BookShelfContext())
            {
                switch (i)
                {
                case 1:    //默认的按推荐数排序
                    var query1 = BookShelfService.AllBooks();
                    var list1  = from t in query1 orderby t.Recommend descending select t;
                    queryBooks = list1.ToList();
                    break;

                case 2:    //书号
                    var query2 = BookShelfService.AllBooks();
                    var list2  = from t in query2 orderby t.BookId ascending select t;
                    queryBooks = list2.ToList();
                    break;

                case 3:    //作者
                    var query3 = BookShelfService.AllBooks();
                    var list3  = from t in query3 orderby t.Author ascending select t;
                    queryBooks = list3.ToList();
                    break;

                case 4:    //分类
                    var query4 = BookShelfService.AllBooks();
                    var list4  = from t in query4 orderby t.BookId ascending select t;
                    queryBooks = list4.ToList();
                    break;

                case 5:    //书架号
                    var query5 = BookShelfService.AllBooks();
                    var list5  = from t in query5 orderby t.BookId ascending select t;
                    queryBooks = list5.ToList();
                    break;
                }
                booksBindingSource.DataSource = new List <Book>();
                booksBindingSource.ResetBindings(false);
                booksBindingSource.DataSource = queryBooks;
            }
        }
 public static void Check(int year, int month, int day)
 {
     using (var db = new BookShelfContext())
     {
         List <Book> allLentBooks = db.Books.Where(o => o.State == "已被借阅").ToList();
         foreach (var b in allLentBooks)
         {
             String time      = b.LendTime;
             int    y         = time.IndexOf("年");
             int    m         = time.IndexOf("月");
             int    d         = time.IndexOf("日");
             String bookYear  = time.Substring(0, y);
             String bookMonth = time.Substring(y + 1, m - y - 1);
             String bookDay   = time.Substring(m + 1, d - m - 1);
             Client user      = ClientService.AllClients().FirstOrDefault(o => o.Name == b.ClientName);
             if (Convert.ToInt32(bookYear) == year)
             {
                 if (Convert.ToInt32(bookMonth) == month)
                 {
                     if (Convert.ToInt32(bookDay) <= day)
                     {
                         ReturnBooks(b, user);
                     }
                 }
                 else if (Convert.ToInt32(bookMonth) < month)
                 {
                     ReturnBooks(b, user);
                 }
             }
             else if (Convert.ToInt32(bookYear) < year)
             {
                 ReturnBooks(b, user);
             }
         }
     }
 }
 public static IQueryable <BookShelf> AllShelves(BookShelfContext db)
 {
     return(db.BookShelves.Include(o => o.Books)
            );
 }