public ActionResult AddReadingState(string state, string lib, string bk) { string userId = User.Identity.GetUserId(); UserReading userReading = db.UserReadings.FirstOrDefault(x => x.UserId == userId && x.BookId.ToString() == bk); if (userReading == null) { int bookId = Convert.ToInt32(bk); userReading = new UserReading() { BookId = bookId, UserId = userId }; db.UserReadings.Add(userReading); } if (state == "0") { db.UserReadings.Remove(userReading); } else if (state == "1") { userReading.StartDate = DateTime.Now; userReading.EndDate = null; } else if (state == "2") { userReading.StartDate = userReading.StartDate == null ? DateTime.Now : userReading.StartDate; userReading.EndDate = DateTime.Now; } db.SaveChanges(); return(RedirectToAction("Book", new { lib = lib, bk = bk })); }
private Book GetLastBook(UserReading userReading) { if (userReading == null) { return(null); } else { Book book = db.Books.FirstOrDefault(bk => bk.Id == userReading.BookId); return(book); } }
public ActionResult Book(LibraryBookDetails model, string lib, string bk) { if (String.IsNullOrEmpty(lib) || String.IsNullOrEmpty(bk)) { return(RedirectToAction("")); } if (!HasAccessToLib(lib)) { return(RedirectToAction("")); } Book book = db.Books.FirstOrDefault(x => x.Id.ToString() == bk); LibraryBook libraryBook = db.LibraryBooks.FirstOrDefault(x => x.BookId.ToString() == bk && x.LibraryId.ToString() == lib); //comments List <LibraryComment> comments = db.LibraryComments.Where(x => x.LibraryBookId == libraryBook.Id).ToList(); List <LibraryComment> modelComments = new List <LibraryComment>(); var q = (from lc in comments join us in db.Users on lc.UserId equals us.Id select new { lc = lc.Comment, us }); foreach (var t in q) { modelComments.Add(new LibraryComment() { Comment = t.lc, User = t.us }); } //userreading var userID = User.Identity.GetUserId(); UserReading userReading = db.UserReadings.FirstOrDefault(x => x.UserId == userID && x.BookId.ToString() == bk); //libraryLending LibraryLending libraryLending = db.LibraryLendings.FirstOrDefault(ll => ll.EndDate == null && (ll.LibraryBookId == libraryBook.Id || ll.CopyLibraryBookId == libraryBook.Id)); ViewBag.Lending = GetBookState(book, lib); if (ViewBag.Lending == "out") { LibraryBook lb = db.LibraryBooks.FirstOrDefault(x => x.Id == libraryLending.CopyLibraryBookId); Library library = db.Libraries.FirstOrDefault(l => l.Id == lb.LibraryId); ApplicationUser borrowUser = db.Users.FirstOrDefault(usr => usr.Id == library.UserId); ViewBag.BorrowingUser = borrowUser; } else if (ViewBag.Lending == "out-ext") { ViewBag.BorrowingUserExternal = libraryLending.ExternalBorrower; } else if (ViewBag.Lending == "in") { LibraryBook lb = db.LibraryBooks.FirstOrDefault(x => x.Id == libraryLending.LibraryBookId); Library library = db.Libraries.FirstOrDefault(l => l.Id == lb.LibraryId); ApplicationUser lendUser = db.Users.FirstOrDefault(usr => usr.Id == library.UserId); ViewBag.LendingUser = lendUser; } else if (ViewBag.Lending == "in-ext") { ViewBag.LendingUserExternal = libraryLending.ExternalLender; } //Are you an owner? Library libraryOwner = db.Libraries.FirstOrDefault(l => l.Id == libraryBook.LibraryId); ViewBag.Owner = userID == libraryOwner.UserId ? true : false; model.Book = book; model.Comments = comments; model.UserReading = userReading; model.LibraryLending = libraryLending; return(View(model)); }