예제 #1
0
        public ActionResult Borrow(string id)
        {
            currentUser = db.standardUsers.Find(currentUser.Id);
            if (id == null)
            {
                ViewBag.error = "Isbn no girilmedi";
                return(View("Error"));
            }

            Book book = db.books.Find(id);

            if (book == null)
            {
                ViewBag.error = "Kitap bulunamadı";
                return(View("Error"));
            }
            if (book.userMap != null)
            {
                ViewBag.error = "Bu kitap teslim alınmış.";
                return(View("Error"));
            }

            List <Book> myBooks = db.books.Include(a => a.userMap).Where(x => x.userMap.userId == currentUser.Id).ToList();

            if (myBooks.Count >= 3)
            {
                ViewBag.error = "Bir kullanıcı 3'ten fazla sayıda kitap alamaz.";
                return(View("Error"));
            }

            if (myBooks.Where(x => x.userMap.deliveryDate < Kutuphane.Date.Date.date).ToList().Count > 0)
            {
                ViewBag.error = "Teslim süren geçmiş kitapların var. Önce onları teslim et.";
                return(View("Error"));
            }


            UserBookMap willBeAdded = new UserBookMap();

            willBeAdded.book         = book;
            willBeAdded.deliveryDate = Kutuphane.Date.Date.date.AddDays(7);
            willBeAdded.Isbn         = book.Isbn;
            willBeAdded.user         = currentUser;
            willBeAdded.userId       = currentUser.Id;
            db.userBookMaps.Add(willBeAdded);
            db.SaveChanges();



            return(RedirectToAction("MyBooks"));
        }
예제 #2
0
        public ActionResult Deliver(string id)
        {
            currentUser = db.standardUsers.Find(currentUser.Id);
            if (id == null)
            {
                ViewBag.error = "Isbn no girilmedi";
                return(View("Error"));
            }
            Book book = db.books.Find(id);

            if (book == null)
            {
                ViewBag.error = "Kitap bulunamadı";
                return(View("Error"));
            }
            if (book.userMap == null)
            {
                ViewBag.error = "Kitap kütüphanemizdedir.";
                return(View("Error"));
            }
            if (book.userMap.userId != currentUser.Id)
            {
                ViewBag.error = "Başka kişinin üzerinde olan kitabı sen teslim edemezsin";
                return(View("Error"));
            }

            else
            {
                UserBookMap willBeDeleted = db.userBookMaps.FirstOrDefault(a => a.Isbn == id && a.userId == currentUser.Id);
                if (willBeDeleted != null)
                {
                    db.userBookMaps.Remove(willBeDeleted);
                    db.SaveChanges();
                }
            }
            return(RedirectToAction("MyBooks"));
        }