Example #1
0
        public ActionResult Reserve([Bind(Include = "ID,FirstName,LastName,PhoneNumber,Email, book")] Reserve reserve, int id)
        {
            //reserve.book = id;
            if (ModelState.IsValid)
            {
                var bookReserved = db.Books.Where(i => i.ID == id).First();
                var quearyTemp   = from m in db.Books
                                   where m.ID == id
                                   select m;

                foreach (Books m in quearyTemp)
                {
                    m.Available = false;
                }

                reserve.book             = bookReserved;
                reserve.ReserveBeginDate = DateTime.Now;
                reserve.ReserveEndDate   = DateTime.Now.AddDays(3);

                int x = reserve.ID;

                db.Reserves.Add(reserve);
                db.SaveChanges();
                return(RedirectToAction("Confirmed"));
            }

            return(View(reserve));
        }
        public BooksController()
        {
            var quearyTemp1 = from r in db.Reserves
                              where r.ReserveBeginDate.Day - DateTime.Now.Day < -3
                              join b in db.Books
                              on r.ID equals b.ID
                              select b;

            foreach (Books y in quearyTemp1)
            {
                if (y.Taken == false)
                {
                    y.Available = true;
                }
            }
            db.SaveChanges();

            var queryTemp2 = from r in db.Reserves
                             where r.ReserveBeginDate.Day - DateTime.Now.Day < -3
                             select r;

            foreach (Reserve u in queryTemp2)
            {
                db.Reserves.Remove(u);
            }



            db.SaveChanges();
        }
Example #3
0
        /// <summary>
        /// WE are addding a graph, so reviews can also be added as well..
        /// </summary>
        /// <param name="newBook"></param>
        public Book AddBook(Book newBook)
        {
            var saved = this._ctx.Add(newBook.ToDb());

            _ctx.SaveChanges();
            return(saved.Entity.ToClient());
        }
Example #4
0
        public OperationResult <BookResponse> Execute(int id)
        {
            try
            {
                DbBook       book       = _context.Books.FirstOrDefault(x => x.Id == id);
                DbAuthorBook authorBook = _context.Authors.FirstOrDefault(x => x.BookId == id);


                if (authorBook != null)
                {
                    _context.Authors.Remove(authorBook);
                }
                _context.SaveChanges();

                if (book == null)
                {
                    throw new ArgumentException("Model doesn't exist");
                }
                _context.Books.Remove(book);
                _context.SaveChanges();

                return(new OperationResult <BookResponse>()
                {
                    IsSuccess = true
                });
            }
            catch (ArgumentOutOfRangeException exc)
            {
                return(new OperationResult <BookResponse>()
                {
                    IsSuccess = false,
                    ErrorMessages = { exc.Message }
                });
            }
        }
Example #5
0
        public OperationResult <BookResponse> Execute(BookRequest book)
        {
            try
            {
                if (_context.Books.FirstOrDefault(x => x.Id == book.Id) == null)
                {
                    _context.Books.Add(_bookMapper.Map(book));
                    _context.SaveChanges();
                    _context.Authors.Add(_authorBookMapper.Map(book));
                    _context.SaveChanges();

                    return(new OperationResult <BookResponse>()
                    {
                        IsSuccess = true
                    });
                }
                else
                {
                    throw new ArgumentException("This id already exists");
                }
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.Message);
                return(new OperationResult <BookResponse>()
                {
                    IsSuccess = false,
                    ErrorMessages = { exc.Message }
                });
            }
        }
Example #6
0
 public IActionResult AddBook(Book newBook)
 {
     if (ModelState.IsValid)
     {
         _context.Books.Add(newBook);
         _context.SaveChanges();
     }
     return(RedirectToAction("BooksList", new { id = newBook.Id }));
 }
        public ActionResult Create(Book book)
        {
            if (ModelState.IsValid)
            {
                context.Books.Add(book);
                context.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(book));
        }
        public ActionResult Create([Bind(Include = "ID,FirstName,LastName,PhoneNumber,Title,Author,Genre,ISBN,Fullfilled")] Request request)
        {
            if (ModelState.IsValid)
            {
                db.Requests.Add(request);
                db.SaveChanges();
                return(RedirectToAction("RequestConfirmation"));
            }

            return(View(request));
        }
Example #9
0
        public ActionResult Create([Bind(Include = "BookId,Title,Price,Publisher")] Book book)
        {
            if (ModelState.IsValid)
            {
                db.Books.Add(book);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(book));
        }
        public ActionResult Create([Bind(Include = "Id,Name,Price,Author,Publisher,RowVersion")] Book book)
        {
            if (ModelState.IsValid)
            {
                db.Books.Add(book);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(book));
        }
        public IHttpActionResult PostBook([FromBody] BookModel bookModel)
        {
            try
            {
                _booksDbContext.BooksList.Add(bookModel);
                _booksDbContext.SaveChanges();

                return(Ok(_booksDbContext.BooksList));
            }
            catch (Exception err)
            {
                return(BadRequest(err.Message));
            }
        }
Example #12
0
        private static void AddBooks()
        {
            using (var db = new BooksDbContext())
            {
                var author1 = db.Authors.Single(a => a.Name == "애거사 크리스티");
                var book1   = new Book
                {
                    Title         = "그리고 아무도 없었다.",
                    PublishedYear = 1939,
                    Author        = author1
                };

                db.Books.Add(book1);

                var author2 = db.Authors.Single(a => a.Name == "찰스 디킨스");
                var book2   = new Book
                {
                    Title         = "두 도시 이야기",
                    PublishedYear = 1859,
                    Author        = author2
                };

                db.Books.Add(book2);

                db.SaveChanges();
            }
        }
Example #13
0
                             > Post([FromBody] AddBorrowing borrowing)
        {
            var newBorrowing = new Borrowing
            {
                BookId   = borrowing.BookId,
                AuthorId = borrowing.AuthorId
            };

            var book = _BooksDbContext.Books.FirstOrDefault(b => b.Id == borrowing.BookId);

            if (book == null || book.Copies <= 3)
            {
                return(NotFound("El libro no existe o no hay suficientes copias disponibles."));
            }

            var bookTmp = _BooksDbContext.Books.FirstOrDefault(b => b.Id == book.Id);

            if (bookTmp == null)
            {
                return(NotFound("El libro no existe o no hay suficientes copias disponibles."));
            }
            else
            {
                bookTmp.Copies -= 1;
                _BooksDbContext.Entry(bookTmp).CurrentValues.SetValues(bookTmp);
            }

            _BooksDbContext.Borrowings.Add(newBorrowing);
            _BooksDbContext.SaveChanges();

            return(newBorrowing);
        }
Example #14
0
 static void InsertBooks()
 {
     using (var db = new BooksDbContext()) {
         var book1 = new Book {
             Title        = "坊ちゃん",
             PublshedYear = 2003,
             Author       = new Author {
                 Birthday = new DateTime(1867, 2, 9),
                 Gender   = "M",
                 Name     = "夏目漱石"
             }
         };
         db.Books.Add(book1);
         var book2 = new Book {
             Title        = "人間失格",
             PublshedYear = 1990,
             Author       = new Author {
                 Birthday = new DateTime(1909, 6, 19),
                 Gender   = "M",
                 Name     = "太宰治"
             }
         };
         db.Books.Add(book2);
         db.SaveChanges();
     }
 }
Example #15
0
        private static void AddAuthors()
        {
            using (var db = new BooksDbContext())
            {
                var author1 = new Author()
                {
                    BirthDay = new DateTime(1890, 09, 15),
                    Gender   = "F",
                    Name     = "애거사 크리스티"
                };

                db.Authors.Add(author1);

                var author2 = new Author()
                {
                    BirthDay = new DateTime(1812, 02, 07),
                    Gender   = "M",
                    Name     = "찰스 디킨스"
                };

                db.Authors.Add(author2);

                db.SaveChanges();
            }
        }
Example #16
0
 // List 13-5
 static void InsertBooks()
 {
     using (var db = new BooksDbContext()) {
         var book1 = new Book {
             Title         = "별의 계승자",
             PublishedYear = 1977,
             Author        = new Author {
                 Birthday = new DateTime(1941, 06, 27),
                 Gender   = "M",
                 Name     = "제임스 P. 호건",
             }
         };
         db.Books.Add(book1);
         var book2 = new Book {
             Title         = "타임머신",
             PublishedYear = 1895,
             Author        = new Author {
                 Birthday = new DateTime(1866, 09, 21),
                 Gender   = "M",
                 Name     = "허버트 조지 웰즈",
             }
         };
         db.Books.Add(book2);
         db.SaveChanges();
     }
 }
Example #17
0
 // List 13-11
 private static void UpdateBook()
 {
     using (var db = new BooksDbContext()) {
         var book = db.Books.Single(x => x.Title == "별의 계승자");
         book.PublishedYear = 2016;
         db.SaveChanges();
     }
 }
Example #18
0
        /// <summary>
        /// Seeds the database with default book data
        /// </summary>
        public bool TrySeed()
        {
            _logger.LogDebug("Attempting to seed database");

            if (_context.Books.Any())
            {
                _logger.LogDebug("The database already contains data and does not need to be seeded");
                return(false);
            }

            var defaultDataFilePath = Path.Combine(_hosting.ContentRootPath, "Data", "BookData.json");
            var books = File.ReadAllText(defaultDataFilePath);

            var bookImports = JsonConvert.DeserializeObject <List <Book> >(books);

            var oldImageDirectory = Path.Combine(_hosting.WebRootPath, "images", "userResources");

            if (Directory.Exists(oldImageDirectory))
            {
                Directory.Delete(oldImageDirectory, true);
            }

            for (int i = 0; i < bookImports.Count; i++)
            {
                var imagePath       = bookImports[i].ImagePath;
                var sourceImagePath = Path.Combine(_hosting.WebRootPath, "images", "appResources", imagePath);

                if (!Directory.Exists(Path.GetDirectoryName(sourceImagePath)))
                {
                    _logger.LogError("The Directory for the @{sourceImagePath} does not exist", sourceImagePath);
                    return(false);
                }

                var generatedImagePath = _bookImageService.GenerateImagePath(imagePath);
                var targetImagePath    = Path.Combine(_hosting.WebRootPath, "images", "userResources", generatedImagePath);

                Directory.CreateDirectory(Path.GetDirectoryName(targetImagePath));

                File.Copy(sourceImagePath, targetImagePath);
                _logger.LogDebug("File was copied from @{sourcePath} to @{targetPath}", sourceImagePath, targetImagePath);

                bookImports[i].ImagePath = generatedImagePath;
            }

            _context.Books.AddRange(bookImports);
            _context.SaveChanges();

            if (bookImports.Count() != _context.Books.Count())
            {
                _logger.LogError("Could not seed the database with all of the Seed Data");
                return(false);
            }

            _logger.LogDebug("Seeded the database with default book data from @{filePath}", defaultDataFilePath);
            return(true);
        }
        public void UpdateBookName(int id, Book book)
        {
            _context = new BooksDbContext();
            Book foundBook = _context.Books.SingleOrDefault <Book>(b => b.BookId == id);

            if (foundBook != null)
            {
                foundBook.BookName = book.BookName;
            }
            _context.SaveChanges();
        }
Example #20
0
        private static void Ex1_1()
        {
            using (var db = new BooksDbContext())
            {
                var natsume = db.Authors.Single(a => a.Name == "夏目漱石");
                var book1   = new Book
                {
                    Title        = "こころ",
                    PublshedYear = 1991,
                    Author       = natsume,
                };
                db.Books.Add(book1);
                var author2 = db.Authors.Single(a => a.Name == "宮沢賢治");
                var book2   = new Book
                {
                    Title        = "注文の多い料理店",
                    PublshedYear = 2000,
                    Author       = author2,
                };
                db.Books.Add(book2);
                db.SaveChanges();
            }
            using (var db = new BooksDbContext())
            {
                var kikuchi = new Author
                {
                    Birthday = new DateTime(1888, 12, 26),
                    Gender   = "M",
                    Name     = "菊池寛"
                };
                var book1 = new Book
                {
                    Title        = "真珠夫人",
                    PublshedYear = 2003,
                    Author       = kikuchi
                };
                db.Books.Add(book1);

                var kawabata = new Author
                {
                    Birthday = new DateTime(1899, 6, 14),
                    Gender   = "M",
                    Name     = "川端康成"
                };
                var book2 = new Book
                {
                    Title        = "伊豆の踊子",
                    PublshedYear = 2003,
                    Author       = kawabata
                };
                db.Books.Add(book2);
                db.SaveChanges();
            }
        }
Example #21
0
        public void AddBook(Book book, string authorFullName)
        {
            var author = booksDbContext.Add <Author>(new Author()
            {
                FullName = authorFullName
            });

            booksDbContext.SaveChanges();
            book.AuthorId = author.Entity.Id;
            booksDbContext.Add <Book>(book);
        }
Example #22
0
 // List 13-12
 private static void DeleteBook()
 {
     using (var db = new BooksDbContext()) {
         var book = db.Books.SingleOrDefault(x => x.Id == 10);
         if (book != null)
         {
             db.Books.Remove(book);
             db.SaveChanges();
         }
     }
 }
Example #23
0
        public void Seed(IEnumerable <Book> boooooks)
        {
            // Don't add if the books are already theree...
            if (ctx.Books.Count() > 0)
            {
                return;
            }
            var dbBooks = boooooks.ToDb();

            ctx.AddRange(dbBooks);
            ctx.SaveChanges();
        }
        public IActionResult Create(NewBookVM model)
        {
            if (!ModelState.IsValid)
            {
                View(model);
            }

            context.Books.Add(mapper.Map <Book>(model));
            context.SaveChanges();

            return(RedirectToAction(nameof(HomeController.Index)));
        }
Example #25
0
        public ActionResult <Author> Post([FromBody] AddAuthor author)
        {
            var newAuthor = new Author
            {
                Age  = author.Age,
                Name = author.Name
            };

            _BooksDbContext.Authors.Add(newAuthor);
            _BooksDbContext.SaveChanges();

            return(newAuthor);
        }
Example #26
0
 /// <summary>
 /// 書籍情報削除
 /// </summary>
 static void DeleteBook()
 {
     using (var db = new BooksDbContext())
     {
         var book = db.Books.SingleOrDefault(x => x.Id == 1);
         //.SingleOrDefaultの場合、対象が存在しなければ規定値(?)が返る
         //検索対象が存在すればbookはnullではないので削除実行
         if (book != null)
         {
             db.Books.Remove(book);
             db.SaveChanges();
         }
     }
 }
        public IActionResult Delete(int id)
        {
            var book = _db.Books.FirstOrDefault(u => u.Id == id);

            if (book == null)
            {
                return(Json(new { success = false, message = "Error Deleting!" }));
            }

            _db.Books.Remove(book);
            _db.SaveChanges();

            return(Json(new { success = true, message = "Delete Completed!" }));
        }
Example #28
0
        public ActionResult <Book> Post([FromBody] AddBooks book)
        {
            var newBook = new Book
            {
                Name        = book.Name,
                PublishDate = DateTime.Now,
                Copies      = 5,
                AuthorId    = book.AuthorId
            };

            _BooksDbContext.Books.Add(newBook);
            _BooksDbContext.SaveChanges();

            return(newBook);
        }
Example #29
0
 /// <summary>
 /// 書籍発行年更新
 /// </summary>
 /// <param name="bookName">更新対象の書籍名</param>
 /// <param name="publishedYear">発行年</param>
 static void UpdateBook(string bookName, int publishedYear)
 {
     using (var db = new BooksDbContext())
     {
         //.Singleにて唯一の一致するレコードを返す
         //var book = db.Books.Single(x => x.Title == "沈まぬ太陽");
         //.SingleOrDefault レコードが存在しない場合規定値を返す。
         var book = db.Books.SingleOrDefault(x => x.Title == bookName);
         if (book != null)
         {
             //ありえない数字を入れる
             book.PublishedYear = publishedYear;
             db.SaveChanges();
         }
     }
 }
Example #30
0
 public ActionResult Upsert()
 {
     if (ModelState.IsValid)
     {
         if (Book.Id == 0)
         {
             _db.Books.Add(Book);
         }
         else
         {
             _db.Books.Update(Book);
         }
         _db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(Book));
 }