public async Task <IActionResult> Edit(int id, [Bind("Id,GenreId,BookId")] BookGenres bookGenres)
        {
            if (id != bookGenres.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(bookGenres);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!BookGenresExists(bookGenres.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["BookId"]  = new SelectList(_context.Books, "Id", "Name", bookGenres.BookId);
            ViewData["GenreId"] = new SelectList(_context.Genres, "Id", "Name", bookGenres.GenreId);
            return(View(bookGenres));
        }
Exemplo n.º 2
0
 public void AddGenres(IEnumerable <Int32> genres)
 {
     foreach (var genreId in genres)
     {
         BookGenres.Add(new BookGenre(this, genreId));
     }
 }
Exemplo n.º 3
0
        public Task <int> Create(BookGenres article)
        {
            var dbPara = new DynamicParameters();

            dbPara.Add("BookGenreName", article.BookGenreName, DbType.String);
            dbPara.Add("MoreLikeBookGenreId", null, DbType.Int32);
            var articleId = Task.FromResult(_dapperManager.Insert <int>("[dbo].[SP_Add_BookGenres]",
                                                                        dbPara,
                                                                        commandType: CommandType.StoredProcedure));

            return(articleId);
        }
Exemplo n.º 4
0
        public Task <int> Update(BookGenres article)
        {
            var dbPara = new DynamicParameters();

            dbPara.Add("Id", article.BookGenreId);
            dbPara.Add("BookGenreName", article.BookGenreName, DbType.String);
            dbPara.Add("MoreLikeBookGenreId", article.MoreLikeBookGenreId, DbType.Int32);

            var updateArticle = Task.FromResult(_dapperManager.Update <int>("[dbo].[SP_Update_BookGenres]",
                                                                            dbPara,
                                                                            commandType: CommandType.StoredProcedure));

            return(updateArticle);
        }
        public async Task <IActionResult> Create(int genreId, [Bind("Id,BookId")] BookGenres bookGenres)
        {
            BookGenres bg = new BookGenres();

            bg.GenreId         = genreId;
            bookGenres.GenreId = genreId;
            bg.BookId          = bookGenres.BookId;

            if (ModelState.IsValid)
            {
                _context.Add(bg);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index", new { id = genreId }));
            }
            //ViewData["BookId"] = new SelectList(_context.Books, "Id", "Name", bookGenres.BookId);
            //ViewData["GenreId"] = new SelectList(_context.Genres, "Id", "Name", bookGenres.GenreId);
            return(RedirectToAction("Index", new { id = genreId }));
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            // Перечисления, не помеченные атрибутом Flags
            const BookBinding binding       = BookBinding.Hardcover;
            const BookBinding doubleBinding = BookBinding.Hardcover | BookBinding.Paperback;

            Console.WriteLine("Binding: {0}", binding);
            Console.WriteLine("Double Binding: {0}", doubleBinding); // 3

            // Перечисления, помеченные атрибутом Flags
            const BookGenres genres = BookGenres.Vampire | BookGenres.Fantasy | BookGenres.Romance;

            Console.WriteLine("Genres: {0}", genres); // Ok

            // NOTE: Выяснение, установлен ли флаг
            const BookGenres bookGenres = BookGenres.Vampire | BookGenres.Fantasy;
            //bool isVampire = ((bookGenres & BookGenres.Vampire) != 0);
            bool isVampireAlt = bookGenres.HasFlag(BookGenres.Vampire); // Вариант для .NET 4+

            Console.WriteLine(isVampireAlt);
            Console.ReadKey();
        }
Exemplo n.º 7
0
        //public BookDto(string bookName, Genre type, IEnumerable<AuthorDto> authors):base()
        //{
        //	Name=bookName;
        //	Authors=new List<AuthorDto>(authors);
        //}

        //public BookDto(string bookName, Genre type, AuthorDto author)
        //	: this(bookName, type, new AuthorDto[] {author})
        //{}
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder($"[{Id}]:\"{Name}\"");

            if (BookAuthors != null && BookAuthors.Count() > 0)
            {
                sb.Append(": {");
                foreach (var bookAuthor in BookAuthors)
                {
                    sb.AppendFormat("{0},", bookAuthor.Author != null ? bookAuthor.Author.Name : "not-loaded");
                }
                sb.Remove(sb.Length - 1, 1);
                sb.Append("}, ");
            }
            else
            {
                sb.Append(": {unkonwn author(s)}, ");
            }

            if (BookGenres != null && BookGenres.Count() > 0)
            {
                sb.Append(": {");
                foreach (var bookGenre in BookGenres)
                {
                    sb.AppendFormat("{0},", bookGenre.Genre != null ? bookGenre.Genre.Name : "not-loaded");
                }
                sb.Remove(sb.Length - 1, 1);
                sb.Append("}, ");
            }
            else
            {
                sb.Append(": {unkonwn author(s)}, ");
            }

            sb.Append($"Raiting:{Raiting ?? 0}, ");
            //sb.Append($"Genre:{Genre}");
            return(sb.ToString());
        }
Exemplo n.º 8
0
        public void TestGenre()
        {
            LibraryModel lm = new LibraryModel();
            Book         b1 = new Book();
            Book         b2 = new Book();

            b1.BookGenres = new List <BookGenres>();
            b2.BookGenres = new List <BookGenres>();

            BookGenres bg1 = new BookGenres();
            Genre      g1  = new Genre();

            g1.GenreName = "testGenre";
            bg1.Genres   = g1;

            BookGenres bg2 = new BookGenres();
            Genre      g2  = new Genre();

            g2.GenreName = "Sci-Fi";
            bg2.Genres   = g2;

            b1.BookGenres.Add(bg1);
            b2.BookGenres.Add(bg2);

            lm.library = new List <Book>();
            List <Book> Expectedlib = new List <Book>();

            lm.library.Add(b1);
            lm.library.Add(b2);

            Expectedlib.Add(b1);

            lm.SearchGenre("Sci-Fi");

            Assert.NotNull(lm.library);
            Assert.IsType <List <Book> >(lm.library);
            Assert.True(Expectedlib.Any() == lm.library.Any());
        }
Exemplo n.º 9
0
 public void AddGenre(Int32 genreId)
 => BookGenres.Add(new BookGenre(this, genreId));
Exemplo n.º 10
0
 public void AddGenre(Genre genre)
 {
     BookGenres.Add(new BookGenre(this, genre));
 }
Exemplo n.º 11
0
        public async Task <IActionResult> CreatePOST()
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var book = BooksVM.Book;
                    if (SameBookExist(book))
                    {
                        ModelState.AddModelError("SameBook", "Sách đã có trong hệ thống");
                        ViewData["AuthorId"]    = new SelectList(_context.Authors, "Id", "Signed", BooksVM.Book.AuthorId);
                        ViewData["PublisherId"] = new SelectList(_context.Publishers, "Id", "PublisherName", BooksVM.Book.PublisherId);
                        return(View(BooksVM));
                    }
                    _context.Add(BooksVM.Book);
                    await _context.SaveChangesAsync();

                    if (BooksVM.BareTag != null)
                    {
                        List <string> tags = HandingBareTag(BooksVM.BareTag);
                        foreach (var tag in tags)
                        {
                            int id;
                            Tag bTag = new Tag()
                            {
                                Tagname = tag
                            };
                            var getTag = await _context.Tag.Where(u => u.Tagname == bTag.Tagname).FirstOrDefaultAsync();

                            if (getTag == null)
                            {
                                _context.Tag.Add(bTag);
                                await _context.SaveChangesAsync();

                                id = bTag.Id;
                            }
                            else
                            {
                                id = getTag.Id;
                            }
                            BookTag bookTag = new BookTag()
                            {
                                BookId = BooksVM.Book.Id,
                                TagId  = id
                            };
                            _context.BookTags.Add(bookTag);
                            await _context.SaveChangesAsync();
                        }
                    }
                    var bookFromDb  = _context.Books.Find(BooksVM.Book.Id);
                    var webRootPath = _hostEnvironment.WebRootPath;
                    var files       = HttpContext.Request.Form.Files;
                    if (files.Count != 0)
                    {
                        var uploads   = Path.Combine(webRootPath, SD.BookImageFolder);
                        var extension = Path.GetExtension(files[0].FileName);
                        using (var fileStream = new FileStream(Path.Combine(uploads, BooksVM.Book.Id + extension), FileMode.Create))
                        {
                            files[0].CopyTo(fileStream);
                        }
                        bookFromDb.BookImage = @"\" + SD.BookImageFolder + @"\" + BooksVM.Book.Id + extension;
                    }
                    else
                    {
                        var uploads = Path.Combine(webRootPath, SD.BookDefaultImage);
                        System.IO.File.Copy(uploads, webRootPath + @"\" + SD.BookImageFolder + @"\" + BooksVM.Book.Id + ".png");
                        bookFromDb.BookImage = @"\" + SD.BookImageFolder + @"\" + BooksVM.Book.Id + ".png";
                    }
                    if (BooksVM.GenresViewModels != null)
                    {
                        foreach (var genresView in BooksVM.GenresViewModels)
                        {
                            if (genresView.Selected)
                            {
                                BookGenres bookGenres = new BookGenres()
                                {
                                    BookId   = bookFromDb.Id,
                                    GenresId = genresView.Genres.Id
                                };
                                _context.BookGenres.Add(bookGenres);
                            }
                        }
                    }
                    bookFromDb.Approved = false;
                    await _context.SaveChangesAsync();

                    _iSingleton.LogException("Thêm sách Id: " + bookFromDb.Id + " " + bookFromDb.BookName);
                    return(RedirectToAction(nameof(Index)));
                }
                ViewData["AuthorId"]    = new SelectList(_context.Authors, "Id", "Id", BooksVM.Book.AuthorId);
                ViewData["PublisherId"] = new SelectList(_context.Publishers, "Id", "Id", BooksVM.Book.PublisherId);
                return(View(BooksVM.Book));
            }
            catch (Exception e)
            {
                _iSingleton.LogException(e.Message);
                return(RedirectToAction("Error", "Log"));
            }
        }
Exemplo n.º 12
0
        public async Task <IActionResult> Import(IFormFile fileExcel)
        {
            if (ModelState.IsValid)
            {
                if (fileExcel != null)
                {
                    using (var stream = new FileStream(fileExcel.FileName, FileMode.Create))
                    {
                        await fileExcel.CopyToAsync(stream);

                        using (XLWorkbook workBook = new XLWorkbook(stream, XLEventTracking.Disabled))
                        {
                            var lang = _context.BookGenres.Include(b => b.Book).Include(b => b.Book.Language).ToList();
                            //перегляд усіх листів (в даному випадку категорій)
                            foreach (IXLWorksheet worksheet in workBook.Worksheets)
                            {
                                //worksheet.Name - назва категорії. Пробуємо знайти в БД, якщо відсутня, то створюємо нову
                                Genres newgen;
                                var    c = (from gen in _context.Genres
                                            where gen.Name.Contains(worksheet.Name)
                                            select gen).ToList();
                                if (c.Count > 0)
                                {
                                    newgen = c[0];
                                }
                                else
                                {
                                    newgen      = new Genres();
                                    newgen.Name = worksheet.Name;
                                    _context.Genres.Add(newgen);
                                }
                                //перегляд усіх рядків
                                foreach (IXLRow row in worksheet.RowsUsed().Skip(1))
                                {
                                    Books book    = new Books();
                                    int   counter = 0;
                                    foreach (var bo in _context.Books)
                                    {
                                        if (bo.Name == row.Cell(1).Value.ToString())
                                        {
                                            counter++; book = bo; break;
                                        }
                                    }
                                    if (counter > 0)
                                    {
                                        int count = 0;
                                        foreach (var gen in _context.BookGenres)
                                        {
                                            if ((book.Id == gen.BookId) && (newgen.Id == gen.GenreId))
                                            {
                                                count++; break;
                                            }
                                        }
                                        if (count > 0)
                                        {
                                            goto link1;// якщо такий книжко-жанр вже існує, переходимо до наступного рядка
                                        }
                                        else
                                        {
                                            BookGenres bookg = new BookGenres();
                                            bookg.Book  = book;
                                            bookg.Genre = newgen;
                                            _context.BookGenres.Add(bookg);
                                            goto link1;// переходимо до наступного рядка, бо вже маємо інформацію про цю книжку
                                        }
                                    }
                                    else
                                    {
                                        book      = new Books();
                                        book.Name = row.Cell(1).Value.ToString();
                                        _context.Books.Add(book);
                                    }

                                    book.PagesNum = Convert.ToInt32(row.Cell(3).Value);

                                    Language newlang; int?Lid = 1;
                                    counter = 0;
                                    foreach (var la in _context.Language)
                                    {
                                        if (la.Lname == row.Cell(2).Value.ToString())
                                        {
                                            counter++; Lid = la.Id; break;
                                        }
                                    }
                                    if (counter > 0)
                                    {
                                        book.LanguageId = Lid;
                                    }
                                    else
                                    {
                                        newlang       = new Language();
                                        newlang.Lname = row.Cell(2).Value.ToString();
                                        _context.Language.Add(newlang);
                                        book.LanguageId = newlang.Id;
                                    }

                                    BookGenres bg = new BookGenres();
                                    bg.Book  = book;
                                    bg.Genre = newgen;
                                    _context.BookGenres.Add(bg);

                                    int i = 4;
                                    while (row.Cell(i).Value.ToString().Length > 0)
                                    {
                                        BookAuthors ba = new BookAuthors();
                                        Authors     author; Authors auth = new Authors();
                                        counter = 0;
                                        foreach (var au in _context.Authors)
                                        {
                                            if (au.AuthorName == row.Cell(i).Value.ToString())
                                            {
                                                counter++; auth = au; break;
                                            }
                                        }

                                        if (counter > 0)
                                        {
                                            ba.Author = auth;
                                        }
                                        else
                                        {
                                            author            = new Authors();
                                            author.AuthorName = row.Cell(i).Value.ToString();
                                            ba.Author         = author;
                                            _context.Add(author);
                                        }
                                        ba.Book = book;
                                        _context.BookAuthors.Add(ba);
                                        i++;
                                    }

                                    link1 :;
                                    await _context.SaveChangesAsync();
                                }
                            }
                        }
                    }
                }
            }
            return(RedirectToAction(nameof(Index)));
        }
Exemplo n.º 13
0
 /// <summary>
 /// A constructor for a Book object that takes in a title, an Author, and a genre.
 /// </summary>
 /// <param name="title">
 /// string: a title of a book
 /// </param>
 /// <param name="author">
 /// Author: an Author object
 /// </param>
 /// <param name="genre">
 /// Book.BookGenres: an enum signifying the genre of the book
 /// </param>
 public Book(string title, Author author, BookGenres genre)
 {
     Title  = title;
     Author = author;
     Genre  = genre;
 }