public ActionResult AddComment(int?id, BookInLibraryViewModel model) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } BookInLibrary bookInLibrary = db.BooksInLibrary.Find(id); var user = UserManager.FindById(User.Identity.GetUserId()); if (bookInLibrary == null || user == null || !IsFriendOrOwner(bookInLibrary, user)) { return(HttpNotFound()); } var comment = new Comment { Text = model.Comment, BookInLibraryId = bookInLibrary.Id, UserId = user.Id }; db.Comments.Add(comment); db.SaveChanges(); return(RedirectToAction("More", new { id })); }
public ActionResult Rent(int?id, BookInLibraryViewModel model) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } BookInLibrary bookInLibrary = db.BooksInLibrary.Find(id); var user = UserManager.FindById(User.Identity.GetUserId()); if (bookInLibrary == null || user == null || !IsFriendOrOwner(bookInLibrary, user)) { return(HttpNotFound()); } var rental = new Rental { BookInLibraryId = bookInLibrary.Id, StartOfRental = DateTime.Now, UserId = model.UserId }; db.Rentals.Add(rental); db.SaveChanges(); return(RedirectToAction("More", new { id })); }
// GET: BookInLibraries/MoreAboutRent/3 // id is the id of a BookInLibrary public ActionResult MoreAboutRent(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } BookInLibrary bookInLibrary = db.BooksInLibrary.Find(id); var user = UserManager.FindById(User.Identity.GetUserId()); var lastRental = db.Rentals.Where(r => r.BookInLibraryId == bookInLibrary.Id && r.EndOfRental == null).FirstOrDefault(); if (bookInLibrary == null || user == null || lastRental == null || !CanGiveBack(lastRental, user)) { return(HttpNotFound()); } // should be only one or null var lastReading = db.Readings.Where(r => r.BookInLibraryId == bookInLibrary.Id && r.EndOfReading == null).FirstOrDefault(); Boolean isCurrentlyReadByMe = false; Boolean isCurrentlyReadBySbElse = false; String otherReaderName = ""; if (lastReading != null) { if (lastReading.UserId == user.Id) { isCurrentlyReadByMe = true; } else { isCurrentlyReadBySbElse = true; otherReaderName = lastReading.User.UserName; } } var isLent = false; var borrowerName = ""; // borrower is the owner of a librry // should be only one or null if (lastRental != null) { isLent = true; borrowerName = lastRental.User.UserName; } var bookInLibraryViewModel = new BookInLibraryViewModel { BookInLibrary = bookInLibrary, IsCurrentlyReadByMe = isCurrentlyReadByMe, IsCurrentlyReadBySbElse = isCurrentlyReadBySbElse, OtherReaderName = otherReaderName, IsLent = isLent, BorrowerName = borrowerName, }; return(View(bookInLibraryViewModel)); }
// GET: BookInLibraries/More/3 // id is the id of a BookInLibrary public ActionResult More(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } BookInLibrary bookInLibrary = db.BooksInLibrary.Find(id); var user = UserManager.FindById(User.Identity.GetUserId()); if (bookInLibrary == null || user == null) { return(HttpNotFound()); } // check if logged user has permissions to access this book Boolean isFriendOrOwner = IsFriendOrOwner(bookInLibrary, user); Boolean isRent = db.Rentals.Any(r => (r.BookInLibraryId == bookInLibrary.Id && r.UserId == user.Id && r.EndOfRental == null)); if (isRent) { return(RedirectToAction("MoreAboutRent", new { bookInLibrary.Id })); } else if (!isFriendOrOwner) { // error ViewBag.Title = "Brak uprawnień"; ViewBag.Message = "Nie jesteś właścicielem ani członkiem bibilioteki, w której jest podana książka."; return(View("Info")); } // should be only one or null var lastReading = db.Readings.Where(r => r.BookInLibraryId == bookInLibrary.Id && r.EndOfReading == null).FirstOrDefault(); Boolean isCurrentlyReadByMe = false; Boolean isCurrentlyReadBySbElse = false; String otherReaderName = ""; if (lastReading != null) { if (lastReading.UserId == user.Id) { isCurrentlyReadByMe = true; } else { isCurrentlyReadBySbElse = true; otherReaderName = lastReading.User.UserName; } } var isLent = false; var borrowerName = ""; // should be only one or null var lastRental = db.Rentals.Where(r => r.BookInLibraryId == bookInLibrary.Id && r.EndOfRental == null).FirstOrDefault(); if (lastRental != null) { isLent = true; borrowerName = lastRental.User.UserName; } var isLentOutside = db.RentalsToOutside.Any(r => r.BookInLibraryId == bookInLibrary.Id && r.EndOfRental == null); var bookInLibraryViewModel = new BookInLibraryViewModel { BookInLibrary = bookInLibrary, IsCurrentlyReadByMe = isCurrentlyReadByMe, IsCurrentlyReadBySbElse = isCurrentlyReadBySbElse, OtherReaderName = otherReaderName, IsLent = isLent, BorrowerName = borrowerName, isLentOutside = isLentOutside, Users = new SelectList(db.Users.Where(u => u.Id != bookInLibrary.Library.ApplicationUser.Id), "Id", "UserName") }; return(View(bookInLibraryViewModel)); }