Beispiel #1
0
        public IHttpActionResult PutBook(int id, Book book)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != book.BookId)
            {
                return(BadRequest());
            }

            db.Entry(book).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BookExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Beispiel #2
0
        public ActionResult Create([Bind(Include = "ISBN,Name,Author,Genre,Image,OnLoan")] Book book)
        {
            if (ModelState.IsValid)
            {
                db.Books.Add(book);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(book));
        }
Beispiel #3
0
        public ActionResult Create([Bind(Include = "Book_U,Title,Writer,Summary,Publisher,Published_data")] Book book)
        {
            if (ModelState.IsValid)
            {
                db.Books.Add(book);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(book));
        }
Beispiel #4
0
        public ActionResult Create([Bind(Include = "Id,Name")] Book book)
        {
            if (ModelState.IsValid)
            {
                db.Books.Add(book);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(book));
        }
Beispiel #5
0
        public ActionResult Create([Bind(Include = "MemberId,Name")] Member member)
        {
            if (ModelState.IsValid)
            {
                db.Members.Add(member);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(member));
        }
        public ActionResult CleanDb()
        {
            var books = _db.Books.ToList();

            foreach (var book in books)
            {
                _db.Books.Remove(book);
                _db.SaveChanges();
            }
            ViewBag.Message = "Baza danych została wyczyszczona!";
            return(View());
        }
Beispiel #7
0
 public ActionResult AddBook(Book Book)
 {
     try
     {
         _db.Add(Book);
         _db.SaveChanges();
         ModelState.Clear();
         ViewData["Message"] = "1";
     }
     catch
     {
         ViewData["Message"] = "0";
     }
     return(View("../index/addbook"));
 }
Beispiel #8
0
        /// <summary>
        /// Take a book
        /// </summary>
        /// <param name="title">Book's title</param>
        /// <returns></returns>
        public Books TakeBook(string title)
        {
            // Take - user takes a book providing its title. Validation if book exists. Validation if book is
            // still available(check number of copies). Book is added to the user's list and available
            // book count is decreased.
            using (var db = new LibraryDb())
            {
                // 1. Look for book inside table 'Books' by Title
                var book = db.Books.FirstOrDefault(b => b.Title.ToLower() == title.ToLower());
                // 2. If book is found and is still available:
                if (book != null && book.Copies > 0)
                {
                    // 2.1. Decrease available copies
                    book.Copies--;
                    // 2.2. Insert new record in table 'UserBooks'
                    db.UserBooks.Add(new UserBooks()
                    {
                        Author = book.Author,
                        Title  = book.Title,
                        Year   = book.Year,
                    });

                    db.SaveChanges();

                    return(book);
                }
            }

            return(null);
        }
Beispiel #9
0
        public static void UpdateBook(BOOK book, LibraryDb dbContext = null)
        {
            using (LibraryDb db = LibraryDb.GetDbContext(dbContext))
            {
                using (TransactionScope transaction = new TransactionScope())
                {
                    if (book == null)
                    {
                        throw new ArgumentNullException($"book: {nameof(book)}");
                    }

                    BOOK dbBook = db.BOOKS.SingleOrDefault(b => b.ID == book.ID);
                    if (dbBook == null)
                    {
                        throw new Exception($"Книга с {book.ID} не найдена!");
                    }

                    dbBook.ISBN        = book.ISBN;
                    dbBook.NAME        = book.NAME;
                    dbBook.AUTHOR      = book.AUTHOR;
                    dbBook.PUBLISHING  = book.PUBLISHING;
                    dbBook.COUNT       = book.COUNT;
                    dbBook.STATUS      = book.STATUS;
                    dbBook.PRICE       = book.PRICE;
                    dbBook.DESCRIPTION = book.DESCRIPTION;

                    db.SaveChanges();
                    transaction.Complete();
                }
            }
        }
Beispiel #10
0
        public static void UpdateViewer(VIEWER viewer, LibraryDb dbContext = null)
        {
            using (LibraryDb db = LibraryDb.GetDbContext(dbContext))
            {
                using (TransactionScope transaction = new TransactionScope())
                {
                    if (viewer == null)
                    {
                        throw new ArgumentNullException($"book: {nameof(viewer)}");
                    }

                    VIEWER dbViewer = db.VIEWERS.SingleOrDefault(v => v.ID == viewer.ID);
                    if (dbViewer == null)
                    {
                        throw new Exception($"Книга с {viewer.ID} не найдена!");
                    }

                    dbViewer.NAME        = viewer.NAME;
                    dbViewer.SURNAME     = viewer.SURNAME;
                    dbViewer.MIDDLE_NAME = viewer.MIDDLE_NAME;
                    dbViewer.ADDRESS     = viewer.ADDRESS;
                    dbViewer.PHONE       = viewer.PHONE;
                    dbViewer.EMAIL       = viewer.EMAIL;

                    db.SaveChanges();
                    transaction.Complete();
                }
            }
        }
Beispiel #11
0
        /// <summary>
        /// User returns a book
        /// </summary>
        /// <param name="title">Book's title</param>
        /// <returns>Book returned (if found)</returns>
        public UserBooks ReturnBook(string title)
        {
            // Return - user returns a book providing its title. Validation if the book is in the user’s list.
            // Book is removed from the user’s list and the available count is increased.
            // 1. Use the DB
            // 2. Find the book in 'UserBooks' by title
            // 3. If book is found:
            // 3.1. Delete it from 'UserBooks'
            // 3.2. Increase Copies count for the same book inside the 'Books' table
            using (var db = new LibraryDb())
            {
                var userBook = db.UserBooks.FirstOrDefault(b => b.Title.ToLower() == title.ToLower());
                if (userBook != null)
                {
                    db.UserBooks.Remove(userBook);

                    // increase availabe count
                    var libraryBook = db.Books.FirstOrDefault(b => b.Title.ToLower() == title.ToLower());
                    libraryBook.Copies++;

                    db.SaveChanges();

                    return(userBook);
                }
            }

            return(null);
        }
        public IActionResult Delete(Book book)
        {
            using (var db = new LibraryDb())
            {
                db.Books.Remove(book);
                db.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
Beispiel #13
0
        public static void AddNewCard(BOOK book, LibraryDb dbContext = null)
        {
            using (LibraryDb db = LibraryDb.GetDbContext(dbContext))
            {
                using (TransactionScope transaction = new TransactionScope())
                {
                    db.BOOKS.Add(book);

                    db.SaveChanges();

                    transaction.Complete();
                }
            }
        }
Beispiel #14
0
        public static void AddNewViewer(VIEWER viewer, LibraryDb dbContext = null)
        {
            using (LibraryDb db = LibraryDb.GetDbContext(dbContext))
            {
                using (TransactionScope transaction = new TransactionScope())
                {
                    db.VIEWERS.Add(viewer);

                    db.SaveChanges();

                    transaction.Complete();
                }
            }
        }
        public IActionResult Create(string title, string author, double price)
        {
            if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(author) || price < 1)
            {
                return(RedirectToAction("Index"));
            }
            using (var db = new LibraryDb())
            {
                Book book = new Book
                {
                    Title  = title,
                    Author = author,
                    Price  = price
                };

                db.Books.Add(book);
                db.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
Beispiel #16
0
        public static void BooksRemove(decimal[] booksId, LibraryDb dbContext = null)
        {
            using (LibraryDb db = LibraryDb.GetDbContext(dbContext))
            {
                using (TransactionScope transaction = new TransactionScope())
                {
                    foreach (decimal id in booksId)
                    {
                        BOOK book = db.BOOKS.SingleOrDefault(b => b.ID == id);

                        if (book != null)
                        {
                            db.BOOKS.Remove(book);
                        }
                    }

                    db.SaveChanges();

                    transaction.Complete();
                }
            }
        }
Beispiel #17
0
        public static void ViewersRemove(decimal[] idViewers, LibraryDb dbContext = null)
        {
            using (LibraryDb db = LibraryDb.GetDbContext(dbContext))
            {
                using (TransactionScope transaction = new TransactionScope())
                {
                    foreach (decimal id in idViewers)
                    {
                        VIEWER viewer = db.VIEWERS.SingleOrDefault(b => b.ID == id);

                        if (viewer != null)
                        {
                            db.VIEWERS.Remove(viewer);
                        }
                    }

                    db.SaveChanges();

                    transaction.Complete();
                }
            }
        }
Beispiel #18
0
        public ActionResult ReturnBook(int id, Loan loan)

        {
            var loanQuery = from item in db.Loans

                            where item.LoanId == id

                            select item;



            if (ModelState.IsValid)

            {
                var book = db.Loans.FirstOrDefault(s => s.LoanId == id);

                if (book != null)

                {
                    book.Book.OnLoan = 0;

                    //book.FinePrice = 0.00M;


                    db.Entry(book).State = EntityState.Modified;
                    db.Loans.Remove(book);
                    db.SaveChanges();



                    // return RedirectToAction("Index");
                }
            }


            return(View());
            // return View("Index");
        }
Beispiel #19
0
 public void Save()
 {
     _db.SaveChanges();
 }