public bool Add(AuthorBLL item, out string serverSideError) { if (item.IsValid) { using (ProtoLibEntities context = new ProtoLibEntities()) { if (DatabaseDependantValidation(item, context, out serverSideError)) { Author newAuth = new Author(); //newAuth.BookDetailsSet = whoa... Okay, remember.. first authors are added, then new books and author specified there CrossLayerEntityConverter.AuthorBllToDal(context, item, newAuth); context.Authors.AddObject(newAuth); context.SaveChanges(); return true; } } return false; } else { serverSideError = "The Author object is invalid"; return false; } }
public bool Add(LibraryBookBLL item, out string serverSideError) { if (item.IsValid) { using (ProtoLibEntities context = new ProtoLibEntities()) { if (DatabaseDependantValidation(item, context, out serverSideError)) { LibraryBook newLibBook = new LibraryBook(); CrossLayerEntityConverter.LibraryBookBllToDal(context, item, newLibBook); context.LibraryBooks.AddObject(newLibBook); context.SaveChanges(); return true; } } return false; } else { serverSideError = "The LibraryBook object is invalid"; return false; } }
public bool Add(BookDetailsBLL item, out string serverSideError) { //HACK: in manip bar impl, we try to insert a new book before working on it. Better approach possible. if (item.Description.Title == null) item.Description.Title = "New book"; if (item.IsValid) { using (ProtoLibEntities context = new ProtoLibEntities()) { if (DatabaseDependantValidation(item,context, out serverSideError)) { //Good to go. BookDetails newBook = new BookDetails(); newBook.BookDimensions = new BookDimensions(); CrossLayerEntityConverter.BookDetailsBllToDal(context, item, newBook); context.BookDetailsSet.AddObject(newBook); context.SaveChanges(); return true; } } return false; } else { //FIXME: return IDataErrorInfo validation serverSideError = "The BookDetails object is invalid"; return false; } }
public bool Delete(int id) { using (ProtoLibEntities context = new ProtoLibEntities()) { var dalAuth = (from a in context.Authors where a.AuthorID == id select a).SingleOrDefault(); if (dalAuth != null) { //ALT: Handle author deletion in a better way //Delete this author's credit from all books var creditedBooks = from b in context.BookDetailsSet where b.Authors.Contains(dalAuth) select b; foreach (BookDetails book in creditedBooks) book.Authors.Remove(dalAuth); context.Authors.DeleteObject(dalAuth); context.SaveChanges(); return true; } } return false; }
public bool IssueBook(int libBookID, int memberID, out string serverSideError) { serverSideError = null; using (ProtoLibEntities context = new ProtoLibEntities()) { LibraryBook libBook = (from lb in context.LibraryBooks where lb.BookID == libBookID select lb).SingleOrDefault(); if (libBook != null) { Member mem = (from m in context.Members where m.MemberID == memberID select m).SingleOrDefault(); if (mem != null) { Transaction t = new Transaction(); t.LibraryBook = libBook; t.Member = mem; t.Fine = 0.0; t.CheckedOutOn = DateTime.Now; t.ReturnedOn = null; libBook.BookStatus = (from st in context.BookStatus1 where st.StatusID == 101 select st).Single(); context.Transactions.AddObject(t); context.SaveChanges(); return true; } else { serverSideError = string.Format("No member with ID {0} exists", memberID.ToString()); return false; } } else { serverSideError = string.Format("No library book with that ID {0} exists", libBookID.ToString()); return false; } } }
public bool Delete(int id) { using (ProtoLibEntities context = new ProtoLibEntities()) { StaffAccount dalSA = (from sa in context.StaffAccounts.Include("AccountPrefs") where sa.AccountID == id select sa).SingleOrDefault(); if (dalSA != null) { context.AccountPrefsSet.DeleteObject(dalSA.AccountPrefs); context.StaffAccounts.DeleteObject(dalSA); context.SaveChanges(); return true; } } return false; }
public bool Delete(int id) { using (ProtoLibEntities context = new ProtoLibEntities()) { var libBook = (from lb in context.LibraryBooks where lb.BookInfoID == id select lb).SingleOrDefault(); if (libBook != null) { //FIXME: Don't remove transaction history on deleting book var trans = libBook.Transactions.ToList(); foreach (var t in trans) context.Transactions.DeleteObject(t); context.LibraryBooks.DeleteObject(libBook); context.SaveChanges(); return true; } } return false; }
public bool Add(StaffAccountBLL item, out string serverSideError) { serverSideError = null; if (item.IsValid) { using (ProtoLibEntities context = new ProtoLibEntities()) { if (DatabaseDependantValidation(item, context, out serverSideError)) { StaffAccount dalSA = new StaffAccount(); dalSA.AccountPrefs = new AccountPrefs(); CrossLayerEntityConverter.StaffAccountBllToDal(context, item, dalSA); context.StaffAccounts.AddObject(dalSA); context.SaveChanges(); return true; } } } return false; }
public bool Update(List<LibraryBookBLL> items, out string serverSideError) { bool failure = false; serverSideError = null; string error = ""; string temp = ""; int numUpdated = 0; using (ProtoLibEntities context = new ProtoLibEntities()) { foreach (LibraryBookBLL bllLibBook in items) { if (bllLibBook.IsValid) { if (DatabaseDependantValidation(bllLibBook, context, out temp, bllLibBook.ItemID)) { LibraryBook dalLibBook = (from lb in context.LibraryBooks where lb.BookID == bllLibBook.ItemID select lb).SingleOrDefault(); if (dalLibBook != null) { CrossLayerEntityConverter.LibraryBookBllToDal(context, bllLibBook, dalLibBook); } else { error += "\nNo item with ID " + bllLibBook.ItemID.ToString() + " exists."; failure = true; } } else { error += ("\n" + temp); failure = true; } } else { error += ("\nItem with ID: " +bllLibBook.ItemID.ToString() + " is in an invalid state."); failure = true; } } numUpdated = context.SaveChanges(); } if (failure) { serverSideError = "There were unsuccessful updates. " + numUpdated.ToString() + " items were updated.\n" + error; return false; } Debug.Assert(serverSideError == null); return true; }
public bool Update(LibraryBookBLL newItem, out string serverSideError) { if (newItem.IsValid) { using (ProtoLibEntities context = new ProtoLibEntities()) { if (DatabaseDependantValidation(newItem, context, out serverSideError, newItem.ItemID)) { LibraryBook dalLibBook = (from lb in context.LibraryBooks where lb.BookID == newItem.ItemID select lb).Single(); CrossLayerEntityConverter.LibraryBookBllToDal(context, newItem, dalLibBook); context.SaveChanges(); Debug.Assert(serverSideError == null); return true; } else return false; } } serverSideError = "Item is in an invalid state!"; return false; }
public bool Delete(int id) { using (ProtoLibEntities context = new ProtoLibEntities()) { var book = (from b in context.BookDetailsSet.Include("BookDimensions") where b.BookDetailsID == id select b).SingleOrDefault(); if (book != null) { context.BookDimensions.DeleteObject(book.BookDimensions); var libBooks = book.LibraryBooks.ToList(); foreach (var lb in libBooks) context.DeleteObject(lb); var cats = book.Categories.ToList(); foreach (var c in cats) c.BookDetailsSet.Remove(book); foreach (Subject s in book.Subjects) s.BookDetailsSet.Remove(book); Publisher p = book.Publisher; p.BookDetailsSet.Remove(book); var auths = book.Authors.ToList(); foreach (var a in auths) a.BookDetailsSet.Remove(book); context.BookDetailsSet.DeleteObject(book); context.SaveChanges(); return true; } } return false; }
public bool Update(BookDetailsBLL newItem, out string serverSideError) { if (newItem.IsValid) { using (ProtoLibEntities context = new ProtoLibEntities()) { if (DatabaseDependantValidation(newItem, context, out serverSideError, newItem.ItemID)) { BookDetails dalBook = (from b in context.BookDetailsSet where b.BookDetailsID == newItem.ItemID select b).Single(); CrossLayerEntityConverter.BookDetailsBllToDal(context, newItem, dalBook); context.SaveChanges(); //FIXME: Remove assert! Debug.Assert(serverSideError == null); return true; } else return false; } } //FIXME: Return IDataErrorInfo validation serverSideError = "Item is in an invalid state!"; return false; }
public bool Update(AuthorBLL newItem, out string serverSideError) { if (newItem.IsValid) { using (ProtoLibEntities context = new ProtoLibEntities()) { if (DatabaseDependantValidation(newItem, context, out serverSideError, newItem.ItemID)) { Author dalAuth = (from a in context.Authors where a.AuthorID == newItem.ItemID select a).Single(); CrossLayerEntityConverter.AuthorBllToDal(context, newItem, dalAuth); context.SaveChanges(); Debug.Assert(serverSideError == null); return true; } else return false; } } serverSideError = "Item is in an invalid state!"; return false; }
public bool Update(List<StaffAccountBLL> items, out string serverSideError) { bool failure = false; serverSideError = null; string error = ""; string temp = ""; int numUpdated = 0; using (ProtoLibEntities context = new ProtoLibEntities()) { foreach (StaffAccountBLL bllSA in items) { if (bllSA.IsValid) { if (DatabaseDependantValidation(bllSA, context, out temp, bllSA.ItemID)) { StaffAccount dalSA = (from sa in context.StaffAccounts where sa.AccountID == bllSA.ItemID select sa).SingleOrDefault(); if (dalSA != null) { CrossLayerEntityConverter.StaffAccountBllToDal(context, bllSA, dalSA); } else { error += "\nNo item with ID " + bllSA.ItemID.ToString() + " exists."; failure = true; } } else { error += ("\n" + temp); failure = true; } } else { error += ("\nItem with ID: " + bllSA.ItemID.ToString() + " is in an invalid state."); failure = true; } } numUpdated = context.SaveChanges(); } if (failure) { serverSideError = "There were unsuccessful updates. " + numUpdated.ToString() + " items were updated.\n" + error; return false; } return true; }
public bool Update(StaffAccountBLL newItem, out string serverSideError) { if (newItem.IsValid) { using (ProtoLibEntities context = new ProtoLibEntities()) { if (DatabaseDependantValidation(newItem, context, out serverSideError, newItem.ItemID)) { StaffAccount dalSA = (from sa in context.StaffAccounts where sa.AccountID == newItem.ItemID select sa).Single(); CrossLayerEntityConverter.StaffAccountBllToDal(context, newItem, dalSA); context.SaveChanges(); return true; } else return false; } } serverSideError = "Item is in an invalid state!"; return false; }
public bool ReturnBook(int libBookID, int memberID, int finePerDay, int borrowLimit, out string serverSideError) { serverSideError = null; using (ProtoLibEntities context = new ProtoLibEntities()) { LibraryBook libBook = (from lb in context.LibraryBooks where lb.BookID == libBookID select lb).SingleOrDefault(); if (libBook != null) { Member mem = (from m in context.Members where m.MemberID == memberID select m).SingleOrDefault(); if (mem != null) { Transaction trans = (from t in mem.Transactions where t.BookID == libBookID select t).SingleOrDefault(); if (trans != null) { trans.ReturnedOn = DateTime.Now; TimeSpan ts = (DateTime)trans.ReturnedOn - trans.CheckedOutOn; if (ts.Days > borrowLimit) { trans.Fine = ts.Days * finePerDay; } libBook.BookStatus = (from st in context.BookStatus1 where st.StatusID == 101 select st).Single(); context.SaveChanges(); return true; } else { serverSideError = string.Format("The library book with ID {0} is not issued to member ID {1}", libBookID.ToString(), memberID.ToString()); return false; } } else { serverSideError = string.Format("No member with ID {0} exists", memberID.ToString()); return false; } } else { serverSideError = string.Format("No library book with that ID {0} exists", libBookID.ToString()); return false; } } }