public static bool borrowBook(int _id, int _customerId, int _bookId) { try { Customers customer = CustomerCRUD.getCustomer(_customerId); Books book = BookCRUD.getBook(_bookId); if ((book == null) || (book.state == 0) || (customer.money <= 0)) { return(false); } BookCRUD.updateState(book.id, 0); // 1-book in stock, 0-book not in stock BookCRUD.updateReturnDate(book.id, DateTime.Today.AddDays(14)); LibraryLinqDataContext db = new LibraryLinqDataContext(); BorrowedBooks borrowed = new BorrowedBooks(); borrowed.id = _id; borrowed.bookId = book.id; borrowed.customerId = customer.id; db.BorrowedBooks.InsertOnSubmit(borrowed); db.SubmitChanges(); return(true); } catch (Exception) { return(false); } }
public void fetchFromDBTest() { LibraryLinqDataContext db = new LibraryLinqDataContext(); Customers customer = db.Customers.FirstOrDefault(c => c.id.Equals(1)); Assert.AreEqual(customer.id, 1); Assert.AreEqual(customer.name, "Mary Smith"); Assert.AreEqual(customer.money, 100); }
public static int getCustomerId(int _id) { try { LibraryLinqDataContext db = new LibraryLinqDataContext(); BorrowedBooks borrowed = db.BorrowedBooks.Where(p => p.id == _id).First(); return(borrowed.customerId); } catch (Exception) { return(-1); } }
static public int getMoney(int _id) { try { LibraryLinqDataContext db = new LibraryLinqDataContext(); Customers customer = db.Customers.Where(p => p.id == _id).First(); return(customer.money); } catch (System.InvalidOperationException) { return(-1); } }
static public string getName(int _id) { try { LibraryLinqDataContext db = new LibraryLinqDataContext(); Customers customer = db.Customers.Where(p => p.id == _id).First(); return(customer.name); } catch (System.InvalidOperationException) { return(null); } }
public static BorrowedBooks getBorrowedBooks(int _bookId) { try { LibraryLinqDataContext db = new LibraryLinqDataContext(); BorrowedBooks borrowed = db.BorrowedBooks.Where(p => p.bookId == _bookId).First(); return(borrowed); } catch (Exception) { return(null); } }
static public int getMaxId() { LibraryLinqDataContext db = new LibraryLinqDataContext(); if (countBooks() == 0) { return(0); } else { return(db.Books.OrderByDescending(p => p.id).First().id); } }
static public Customers getCustomer(int _id) { try { LibraryLinqDataContext db = new LibraryLinqDataContext(); Customers customer = db.Customers.Where(p => p.id == _id).First(); return(customer); } catch (Exception) { return(null); } }
static public int getState(int _id) { LibraryLinqDataContext db = new LibraryLinqDataContext(); try { Books book = db.Books.Where(p => p.id == _id).First(); return((int)book.state); } catch (System.InvalidOperationException) { return(-1); } }
static public DateTime getReturnDate(int _id) { LibraryLinqDataContext db = new LibraryLinqDataContext(); try { Books book = db.Books.Where(p => p.id == _id).First(); return((DateTime)book.returnDate); } catch (System.InvalidOperationException) { return(DateTime.Today); } }
static public int getPenaltyCost(int _id) { LibraryLinqDataContext db = new LibraryLinqDataContext(); try { Books book = db.Books.Where(p => p.id == _id).First(); return(book.penaltyCost); } catch (System.InvalidOperationException) { return(0); } }
static public string getType(int _id) { LibraryLinqDataContext db = new LibraryLinqDataContext(); try { Books book = db.Books.Where(p => p.id == _id).First(); return(book.type); } catch (System.InvalidOperationException) { return(null); } }
static public bool removeBook(int _id) { try { LibraryLinqDataContext db = new LibraryLinqDataContext(); Books book = db.Books.Where(p => p.id == _id).First(); db.Books.DeleteOnSubmit(book); db.SubmitChanges(); return(true); } catch (Exception) { return(false); } }
static public bool updateMoney(int _id, int _difference) { try { LibraryLinqDataContext db = new LibraryLinqDataContext(); Customers customer = db.Customers.Where(p => p.id == _id).First(); customer.money += _difference; db.SubmitChanges(); return(true); } catch (Exception) { return(false); } }
static public bool updateName(int _id, string _name) { try { LibraryLinqDataContext db = new LibraryLinqDataContext(); Customers customer = db.Customers.Where(p => p.id == _id).First(); customer.name = _name; db.SubmitChanges(); return(true); } catch (Exception) { return(false); } }
static public bool updateType(int _id, string _type) { try { LibraryLinqDataContext db = new LibraryLinqDataContext(); Books book = db.Books.Where(p => p.id == _id).First(); book.type = _type; db.SubmitChanges(); return(true); } catch (Exception) { return(false); } }
static public bool updatePenaltyCost(int _id, int _penaltyCost) { try { LibraryLinqDataContext db = new LibraryLinqDataContext(); Books book = db.Books.Where(p => p.id == _id).First(); book.penaltyCost = 10; db.SubmitChanges(); return(true); } catch (Exception) { return(false); } }
static public bool addCustomer(int _id, string _name, int _money) { try { Customers customer = new Customers(); customer.id = _id; customer.name = _name; customer.money = _money; LibraryLinqDataContext db = new LibraryLinqDataContext(); db.Customers.InsertOnSubmit(customer); db.SubmitChanges(); return(true); } catch (Exception) { return(false); } }
static public bool removeCustomer(int _id) { try { LibraryLinqDataContext db = new LibraryLinqDataContext(); Customers customer = db.Customers.Where(p => p.id == _id).First(); if (customer.money < 0) { return(false); } db.Customers.DeleteOnSubmit(customer); db.SubmitChanges(); return(true); } catch (Exception) { return(false); } }
public static bool returnBook(int _bookId) { try { LibraryLinqDataContext db = new LibraryLinqDataContext(); BorrowedBooks borrowed = db.BorrowedBooks.Where(p => p.bookId == _bookId).First(); if (DateTime.Compare(DateTime.Today, (DateTime)BookCRUD.getBook(_bookId).returnDate) > 0) { TimeSpan days = DateTime.Today - (DateTime)BookCRUD.getBook(_bookId).returnDate; int penalty = -(BookCRUD.getBook(_bookId).penaltyCost *days.Days); CustomerCRUD.updateMoney(borrowed.customerId, penalty); } BookCRUD.updateState(_bookId, 1); // 1 - book back in stock db.BorrowedBooks.DeleteOnSubmit(borrowed); db.SubmitChanges(); return(true); } catch (Exception) { return(false); } }
static public bool addBook(int _id, string _title, string _author, string _type, int _penaltyCost, DateTime _returnDate, int _state) { try { Books book = new Books(); book.id = _id; book.title = _title; book.author = _author; book.type = _type; book.penaltyCost = _penaltyCost; book.returnDate = _returnDate; book.state = _state; LibraryLinqDataContext db = new LibraryLinqDataContext(); db.Books.InsertOnSubmit(book); db.SubmitChanges(); return(true); } catch (Exception) { return(false); } }
public void AddBookToDatabase() { //OpenSqlConnection(); LibraryLinqDataContext db = new LibraryLinqDataContext(); Books book = new Books(); book.id = 100; book.penaltyCost = 1; book.returnDate = null; book.state = 1; book.author = "God"; book.title = "Bible"; book.type = "Classic"; db.Books.InsertOnSubmit(book); db.SubmitChanges(); Books book1 = db.Books.FirstOrDefault(b => b.id.Equals(100)); Assert.AreEqual(book1.id, 100); Assert.AreEqual(book1.penaltyCost, 1); Assert.IsNull(book1.returnDate); Assert.AreEqual(book1.author, "God"); Assert.AreEqual(book1.title, "Bible"); db.Books.DeleteOnSubmit(book); db.SubmitChanges(); }
static public int countCustomers() { LibraryLinqDataContext db = new LibraryLinqDataContext(); return(db.Customers.Count()); }
static public int countBooks() { LibraryLinqDataContext db = new LibraryLinqDataContext(); return(db.Books.Count()); }