示例#1
0
        protected void AddBook(object sender, EventArgs e)
        {
            try
            {
                var imageUrl = this.BookImageInput.Value;

                if (string.IsNullOrEmpty(imageUrl))
                {
                    imageUrl = DefaultImageUrl;
                }

                var book = new Book
                {
                    Title = this.TitleInput.Value,
                    Description = this.DescriptionInput.Value,
                    ImageUrl = imageUrl,
                    AuthorId = int.Parse(this.BookAuthorInput.SelectedValue),
                    CategoryId = int.Parse(CategoryInput.SelectedValue),
                    DateAdded = DateTime.Now
                };

                if (this.FileInput.HasFile)
                {
                    var fileName = Guid.NewGuid();
                    var extension = this.FileInput.FileName.Substring(this.FileInput.FileName.LastIndexOf("."));
                    var saveToPath = "~/Uploads/books/" + fileName + extension;
                    this.FileInput.SaveAs(MapPath(saveToPath));
                    book.FileUrl = saveToPath;
                }

                this.books.Add(book);
                this.books.SaveChanges();
                ErrorSuccessNotifier.AddSuccessMessage("Book successfully added!");
                Response.Redirect("/books/details?id=" + book.Id);
                return;
            }
            catch (DbEntityValidationException ex)
            {
                ErrorSuccessNotifier.AddErrorMessage(ex.Message);
            }
        }
        private void InsertBook(string imgPath)
        {
            Validator validator = new Validator();
            
            string title = this.TitleTextBox.Text;
            validator.ValidateTitleLength(title,3);



            int pagesCount = validator.TryParsePageCountInput(this.PageCountTextBox.Text);
            validator.ValidatePageCount(pagesCount, 0, 5000);

            string genreName = this.GenreDownList.SelectedValue;
            Genre genre = this.GenreServices.GetGenre(genreName);

            var book = new Book()
            {
                Title = title,
                PageCount = pagesCount,
                Genre = genre,
                CreationDate = DateTime.Now,
                Url = imgPath
            };

            this.BooksServices.AddBook(book);
            this.ErrorTextBox.Visible = false;
        }
示例#3
0
        public string GetFileUrl(Book book)
        {
            if (book.FileUrl == null) return "#";

            return book.FileUrl;
        }
 public void AddBook(Book book)
 {
     var repo = this.books.Get<Book>();
     repo.Add(book);
     repo.SaveChanges();
 }