public ActionResult AddAuthor(string bookId, string newAuthor, string addAuthor)
        {
            Author addThisAuthor;

            int bookIdInt    = int.Parse(bookId);
            int addAuthorInt = int.Parse(addAuthor);

            if (newAuthor != null)
            {
                addThisAuthor = new Author(newAuthor);
                addThisAuthor.Save();
            }
            else
            {
                addThisAuthor = Author.Find(addAuthorInt);
            }
            Book foundBook = Book.Find(bookIdInt);

            foundBook.AddAuthor(addThisAuthor);

            return(RedirectToAction("Details", new { id = bookId }));
        }
Beispiel #2
0
        public async Task AddAsync(Guid bookId, string title, string originalTitle,
                                   string description, string isbn, string cover, int pages, string publisher,
                                   DateTime publishedAt, IEnumerable <Guid> authorsId, Guid userId)
        {
            var book = await _bookRepository.GetByIsbnAsync(isbn);

            if (book != null)
            {
                throw new ServiceException(ErrorCodes.BookAlreadyExist, $"Book with ISBN '{isbn}' already exist");
            }

            try
            {
                book = new Book(bookId, title, originalTitle, description, isbn, cover.SetUpDefaultCoverWhenEmpty(), pages, publisher, publishedAt, userId);
            }
            catch (DomainException ex)
            {
                throw new ServiceException(ex, ErrorCodes.InvalidInput, ex.Message);
            }

            foreach (var id in authorsId)
            {
                var author = await _authorRepository.GetByIdAsync(id);

                if (author == null)
                {
                    throw new ServiceException(ErrorCodes.AuthorNotFound, $"Author with id '{id}' not exist");
                }

                var mappedAuthor = _mapper.Map <AuthorShortcut>(author);

                book.AddAuthor(mappedAuthor);
            }

            await _bookRepository.AddAsync(book);

            _logger.LogInformation($"Book '{book.Title}' with id '{book.BookId} was created at '{book.CreatedAt}'");
        }
Beispiel #3
0
        public void GetAuthors_ReturnAllBooksAuthors_AuthorList()
        {
            //Arrange
            Book testBook = new Book("Mow the lawn");

            testBook.Save();
            Author testAuthor1 = new Author("Home Stuff");

            testAuthor1.Save();
            Author testAuthor2 = new Author("Work Stuff");

            testAuthor2.Save();

            //Act
            testBook.AddAuthor(testAuthor1);
            List <Author> result   = testBook.GetAuthors();
            List <Author> testList = new List <Author> {
                testAuthor1
            };

            //Assert
            CollectionAssert.AreEqual(testList, result);
        }
Beispiel #4
0
        public void GetAuthors_ReturnsAllBookAuthors_AuthorList()
        {
            //Arrange
            Book testBook = new Book("1984");

            testBook.Save();
            Author testAuthor1 = new Author("Sylvia Plath");

            testAuthor1.Save();
            Author testAuthor2 = new Author("JK Rowling");

            testAuthor2.Save();

            //Act
            testBook.AddAuthor(testAuthor1);
            List <Author> result   = testBook.GetAuthors();
            List <Author> testList = new List <Author> {
                testAuthor1
            };

            //Assert
            CollectionAssert.AreEqual(testList, result);
        }
        public void Delete_DeletesBookAssociationsFromDatabase_BookList()
        {
            //Arrange
            Author testAuthor = new Author("Fyodor Dostoyevsky");

            testAuthor.Save();

            string testBookName  = "Mow the lawn";
            string testBookGenre = "Tale";
            Book   testBook      = new Book(testBookName, testBookGenre);

            testBook.Save();

            //Act
            testBook.AddAuthor(testAuthor);
            testBook.Delete();

            List <Book> resultAuthorBooks = testAuthor.GetBooks();
            List <Book> testAuthorBooks   = new List <Book> {
            };

            //Assert
            CollectionAssert.AreEqual(testAuthorBooks, resultAuthorBooks);
        }
Beispiel #6
0
        /// <summary>
        /// load record from xml file to memory
        /// 2021-01-31 17:27
        /// </summary>
        internal static bool LoadRecord()
        {
            if (xml is null)
            {
                Logger.v(TAG, "The 'xml' is null");
                return(false);
            }

            XmlElement root = xml.DocumentElement;

            if (root is null)
            {
                Logger.v(TAG, "No root node found");
                return(false);
            }

            records = new List <Record>(50);
            XmlNode     node = root.FirstChild;
            XmlNode     node2;
            XmlNodeList nodes;
            XmlNodeList nodes2;
            Record      rco;
            Book        book;

            while (node != null)
            {
                if (!node.Name.Equals(Record.NODE_RECORD_NAME))
                {
                    continue;
                }

                nodes = node.ChildNodes;
                Logger.v(TAG, "Record child count:" + nodes.Count);
                if (nodes.Count != 3)                //2021-01-31 17:58
                {
                    continue;
                }

                rco = new Record();
                for (byte i = 0; i < 3; i++)
                {
                    node2 = nodes[i];
                    if (node2 is null)
                    {
                        rco = null;
                        break;
                    }

                    Logger.v(TAG, "name:" + node2.Name + ", inner text:" + node2.InnerText);
                    if (node2.Name.Equals(Record.NODE_ID_NAME))
                    {
                        try
                        {
                            int id = int.Parse(node2.InnerText);
                            Logger.v(TAG, "ID:" + id);
                            rco.ID = id;
                        }
                        catch (Exception)
                        {
                            rco = null;
                            break;
                        }
                    }
                    else if (node2.Name.Equals(Record.NODE_BOOK_NAME))
                    {
                        nodes2 = node2.ChildNodes;
                        Logger.v(TAG, "'book' child count:" + nodes2.Count);

                        book = new Book();
                        for (byte j = 0; j < nodes2.Count; j++)
                        {
                            switch (nodes2[j].Name)
                            {
                            case Record.NODE_MTITLE_NAME:
                                book.MainTitle = nodes2[j].InnerText;
                                break;

                            case Record.NODE_STITLE_NAME:
                                book.SubTitle = nodes2[j].InnerText;
                                break;

                            case Record.NODE_AUTHORS_NAME:
                                XmlNodeList nodes3 = nodes2[j].ChildNodes;
                                for (byte k = 0; k < nodes3.Count; k++)
                                {
                                    book.AddAuthor(nodes3[k].InnerText);
                                }
                                break;

                            case Record.NODE_TRANSLATORS_NAME:
                                XmlNodeList nodes4 = nodes2[j].ChildNodes;
                                for (byte k = 0; k < nodes4.Count; k++)
                                {
                                    book.AddTranslator(nodes4[k].InnerText);
                                }
                                break;

                            case Record.NODE_PRESS_NAME:
                                book.Press = nodes2[j].InnerText;
                                break;

                            case Record.NODE_PRESSSN_NAME:
                                book.PressSn = nodes2[j].InnerText;
                                break;

                            case Record.NODE_WORDCOUNT_NAME:
                                book.WordCount = nodes2[j].InnerText;
                                break;
                            }                             //switch -- end

                            if (book is null)
                            {
                                break;
                            }
                        }                         //for -- end

                        if (book is null)
                        {
                            rco = null;
                            break;
                        }
                        else
                        {
                            rco.Book = book;
                        }
                    }
                    else if (node2.Name.Equals(Record.NODE_RINFO_NAME))
                    {
                        nodes2 = node2.ChildNodes;
                        Logger.v(TAG, "'read-info' child count:" + nodes2.Count);
                        for (byte j = 0; j < nodes2.Count; j++)
                        {
                            switch (nodes2[j].Name)
                            {
                            case Record.NODE_STATUS_NAME:
                                if (nodes2[j].InnerText.Equals("0"))
                                {
                                    rco.Status = Record.STATUS_READING;
                                }
                                else
                                {
                                    rco.Status = Record.STATUS_READ;
                                }
                                break;

                            case Record.NODE_BEGINDATE_NAME:
                                rco.BeginDate = nodes2[j].InnerText;
                                break;

                            case Record.NODE_ENDDATE_NAME:
                                rco.EndDate = nodes2[j].InnerText;
                                break;

                            case Record.NODE_STAR_NAME:
                                try
                                {
                                    rco.Star = byte.Parse(nodes2[j].InnerText);
                                }
                                catch (Exception)
                                {
                                    rco = null;
                                }
                                break;

                            case Record.NODE_COMMENT_NAME:
                                rco.Comment = nodes2[j].InnerText;
                                break;
                            }                             //switch -- end.

                            if (rco is null)
                            {
                                break;
                            }
                        }                         //for -- end
                    }
                    else
                    {
                        Logger.v(TAG, "Invalid node in 'record' found");
                        rco = null;
                        break;
                    }
                }                 //for -- end

                if (!IsRecordExisted(rco))
                {
                    records.Add(rco);
                }

                node = node.NextSibling;
            }             //while -- end

            return(true);
        }
Beispiel #7
0
        public void Should_Save_Book()
        {
            var booksPath    = _projectDir + "/Resources/SaveTest/Full/book/books.xml";
            var bookMetaPath = _projectDir + "/Resources/SaveTest/Full/book/meta-inf.xml";

            var authorsPath    = _projectDir + "/Resources/SaveTest/Full/author/authors.xml";
            var authorMetaPath = _projectDir + "/Resources/SaveTest/Full/author/meta-inf.xml";

            var dhb = new DocumentHolder(booksPath, bookMetaPath);
            var dha = new DocumentHolder(authorsPath, authorMetaPath);

            var bookDao = DaoFactory.BookDao(dhb, dha);

            var b1 = new Book()
            {
                Title       = "b1",
                Description = "Test"
            };

            var b2 = new Book()
            {
                Title       = "b2",
                Rating      = 7.9f,
                Description = "Test Book"
            };

            var b3 = new Book()
            {
                Title       = "b3",
                Rating      = 9.5f,
                Description = "Book for test purpose"
            };

            var b4 = new Book()
            {
                Title = "b4"
            };

            var b5 = new Book()
            {
                Title = "b5"
            };

            var a1 = new Author()
            {
                FirstName = "a1",
                LastName  = "Smith"
            };

            var a2 = new Author()
            {
                FirstName = "a2",
                LastName  = "Miller"
            };

            var a3 = new Author()
            {
                FirstName = "a3",
                LastName  = "Cat"
            };

            b1.AddAuthor(a1);

            b2.AddAuthor(a1);
            b2.AddAuthor(a2);

            b3.AddAuthor(a2);

            b4.AddAuthor(a1);

            b5.AddAuthor(a2);
            b5.AddAuthor(a3);

            bookDao.Save(b1, SaveOption.UPDATE_IF_EXIST);
        }
Beispiel #8
0
        private static void LoadData(string q, IBookRepository bookRepo, IErrorlogRepository errorlogRepository)
        {
            var result = new FindBooks().Retrieve(q);

            if (!result.Success && string.IsNullOrEmpty(result.ErrorMessage) || result.GoogleBooks.Count() == 0)
            {
                errorlogRepository.AddLog(new Errorlog {
                    Message = result.ErrorMessage, Stacktrace = "", Created = DateTime.Now
                });
            }

            foreach (var gb in result.GoogleBooks)
            {
                Book book = null;

                try
                {
                    book = new Book
                    {
                        Title               = gb.Title ?? "",
                        ItemId              = gb.ItemId ?? "",
                        SubTitle            = gb.SubTitle ?? "",
                        Publisher           = gb.Publisher ?? "",
                        PublishedDate       = gb.PublishedDate ?? "",
                        Description         = gb.Description ?? "",
                        PageCount           = gb.PageCount ?? "",
                        PrintType           = gb.PrintType ?? "",
                        AverageRating       = gb.AverageRating ?? "",
                        ThumbNail           = gb.ThumbNail ?? "",
                        Language            = gb.Language ?? "",
                        CanonicalVolumeLink = gb.CanonicalVolumeLink ?? "",
                        WebReaderLink       = gb.WebReaderLink ?? "",
                        ISBN10              = gb.ISBN10 ?? "",
                        ISBN13              = gb.ISBN13 ?? "",
                        Created             = DateTime.Now,
                        Updated             = DateTime.Now
                    };
                }
                catch (Exception exception)
                {
                    errorlogRepository.AddLog(exception);
                }


                try
                {
                    var authors = gb.Authors;

                    if (authors != null && authors.Length > 0)
                    {
                        foreach (var author in authors.Select(a => new Author
                        {
                            Name = a,
                            Created = DateTime.Now,
                            Updated = DateTime.Now
                        }))
                        {
                            book.AddAuthor(author);
                        }
                    }
                }
                catch (Exception exception)
                {
                    errorlogRepository.AddLog(exception);
                }


                try
                {
                    var categories = gb.Categories;

                    if (categories != null && categories.Length > 0)
                    {
                        foreach (var category in categories.Select(c => new Category
                        {
                            Name = c,
                            Created = DateTime.Now,
                            Updated = DateTime.Now
                        }))
                        {
                            book.AddCategory(category);
                        }
                    }
                }
                catch (Exception exception)
                {
                    errorlogRepository.AddLog(exception);
                }


                if (book != null)
                {
                    bookRepo.AddOrUpdate(book);
                }
            }
        }
Beispiel #9
0
        public void Init_Data()
        {
            var b1 = new Book()
            {
                Title   = "Fast Recipes",
                Rating  = 7.8f,
                Section = BookSection.COOKERY,
            };

            var b2 = new Book()
            {
                Title   = "Ten Ways To Fake Your Death",
                Rating  = 8.8f,
                Section = BookSection.FICTION,
            };

            var b3 = new Book()
            {
                Title   = "Need Help?",
                Rating  = 7.8f,
                Section = BookSection.FICTION,
            };

            var b4 = new Book()
            {
                Title   = "Billy Milligan",
                Rating  = 9.1f,
                Section = BookSection.DOCUMENTARY,
            };

            var b5 = new Book()
            {
                Title   = "True Detective",
                Rating  = 10.0f,
                Section = BookSection.FICTION,
            };

            var b6 = new Book()
            {
                Title   = "Hold On",
                Rating  = 7.8f,
                Section = BookSection.ART,
            };

            var b7 = new Book()
            {
                Title   = "The Art Of Programming",
                Rating  = 9.0f,
                Section = BookSection.PROGRAMMING,
            };

            var b8 = new Book()
            {
                Title   = "English",
                Rating  = 8.5f,
                Section = BookSection.FOREIGN_LANGUAGES,
            };

            var b9 = new Book()
            {
                Title   = "Harry Potter",
                Rating  = 9.2f,
                Section = BookSection.FICTION,
            };

            var b10 = new Book()
            {
                Title   = "Shine",
                Rating  = 9.1f,
                Section = BookSection.FICTION,
            };

            var b11 = new Book()
            {
                Title   = "Not True Detective",
                Rating  = 9.5f,
                Section = BookSection.DOCUMENTARY
            };

            var a1 = new Author()
            {
                FirstName = "Nicolas",
                LastName  = "Claus"
            };

            var a2 = new Author()
            {
                FirstName = "Stephen",
                LastName  = "Nye"
            };

            var a3 = new Author()
            {
                FirstName = "Valerian",
                LastName  = "Hollo"
            };

            var a4 = new Author()
            {
                FirstName = "Roman",
                LastName  = "Sonniy"
            };

            var a5 = new Author()
            {
                FirstName = "Eugene",
                LastName  = "Colbert"
            };

            var a6 = new Author()
            {
                FirstName = "Haruko",
                LastName  = "Kodjima"
            };

            b1.AddAuthor(a1);
            b1.AddAuthor(a2);

            b2.AddAuthor(a3);

            b3.AddAuthor(a4);

            b5.AddAuthor(a5);
            b5.AddAuthor(a1);

            b6.AddAuthor(a1);

            b7.AddAuthor(a2);
            b7.AddAuthor(a6);

            b8.AddAuthor(a4);

            b9.AddAuthor(a2);

            b10.AddAuthor(a3);
            b10.AddAuthor(a4);
            b10.AddAuthor(a5);

            b11.AddAuthor(a5);
            b11.AddAuthor(a1);

            BookDao.Save(b1, SaveOption.UPDATE_IF_EXIST);
            BookDao.Save(b2, SaveOption.UPDATE_IF_EXIST);
            BookDao.Save(b3, SaveOption.UPDATE_IF_EXIST);
            BookDao.Save(b4, SaveOption.UPDATE_IF_EXIST);
            BookDao.Save(b5, SaveOption.UPDATE_IF_EXIST);
            BookDao.Save(b6, SaveOption.UPDATE_IF_EXIST);
            BookDao.Save(b7, SaveOption.UPDATE_IF_EXIST);
            BookDao.Save(b8, SaveOption.UPDATE_IF_EXIST);
            BookDao.Save(b9, SaveOption.UPDATE_IF_EXIST);
            BookDao.Save(b10, SaveOption.UPDATE_IF_EXIST);
            BookDao.Save(b11, SaveOption.UPDATE_IF_EXIST);
        }
Beispiel #10
0
    static void Main(string[] args)
    {
        var container = new UnityContainer();

        container.RegisterType <AppDataContext, AppDataContext>(new HierarchicalLifetimeManager());
        container.RegisterType <IProductRepository, ProductRepository>(new HierarchicalLifetimeManager());
        container.RegisterType <ICategoryRepository, CategoryRepository>(new HierarchicalLifetimeManager());
        container.RegisterType <IBookRepository, BookRepository>(new HierarchicalLifetimeManager());
        container.RegisterType <IAuthorRepository, AuthorRepository>(new HierarchicalLifetimeManager());

        var productRepository  = container.Resolve <IProductRepository>();
        var categoryRepository = container.Resolve <ICategoryRepository>();
        var bookRepository     = container.Resolve <IBookRepository>();
        var authorRepository   = container.Resolve <IAuthorRepository>();

        var book     = new Book("Livro 01");
        var author   = new Author("Autor 01");
        var product  = new Product("Produto 01");
        var category = new Category("Categoria 01");

        #region Adiciona Itens
        book.AddAuthor(author);
        category.AddProduct(product);

        bookRepository.SaveOrUpdate(book);
        categoryRepository.SaveOrUpdate(category);
        #endregion

        #region Mostra Itens
        Console.WriteLine("\nLivros");
        bookRepository.Get().ToList().ForEach(x => Console.WriteLine(x.Name));

        Console.WriteLine("\nAutores");
        authorRepository.Get().ToList().ForEach(x => Console.WriteLine(x.Name));

        Console.WriteLine("\nProdutos");
        productRepository.Get().ToList().ForEach(x => Console.WriteLine(x.Name));

        Console.WriteLine("\nCategorias");
        categoryRepository.Get().ToList().ForEach(x => Console.WriteLine(x.Name));
        #endregion

        Console.WriteLine("\n-----");
        Console.WriteLine("Objetos aninhados");
        Console.WriteLine("-----");

        #region Mostra Itens Aninhados
        Console.WriteLine("\nLivros");
        bookRepository.GetWithAuthors().ToList().ForEach(x =>
        {
            Console.WriteLine("\t" + x.Name);
            x.Authors.ToList().ForEach(y =>
            {
                Console.WriteLine("\t\t" + y.Name);
            });
        });

        Console.WriteLine("\nCategorias");
        categoryRepository.GetWithProducts().ToList().ForEach(x =>
        {
            Console.WriteLine("\t" + x.Name);
            x.Products.ToList().ForEach(y =>
            {
                Console.WriteLine("\t\t" + y.Name);
            });
        });
        #endregion

        #region Exclui os itens
        bookRepository.Delete(book.Id);
        authorRepository.Delete(author.Id);
        categoryRepository.Delete(category.Id); // Exclui os produtos também
        #endregion

        #region Mostra Itens
        Console.WriteLine("\nLivros");
        bookRepository.Get().ToList().ForEach(x => Console.WriteLine(x.Name));

        Console.WriteLine("\nAutores");
        authorRepository.Get().ToList().ForEach(x => Console.WriteLine(x.Name));

        Console.WriteLine("\nProdutos");
        productRepository.Get().ToList().ForEach(x => Console.WriteLine(x.Name));

        Console.WriteLine("\nCategorias");
        categoryRepository.Get().ToList().ForEach(x => Console.WriteLine(x.Name));
        #endregion

        Console.Read();
    }
Beispiel #11
0
        static void Main(string[] args)
        {
            Book      test    = new Book();
            Book      livre2  = new Book();
            Publisher editeur = new Publisher {
                Ca = 523698.4, Name = "Edition M2i"
            };

            Author guillaume = new Author {
                FirstName = "Guillaume", LastName = "Tel"
            };
            Author arthur = new Author {
                FirstName = "Arthur", LastName = "Le Roi"
            };
            Author maurice = new Author {
                FirstName = "Maurice", LastName = "Momo"
            };

            test.Title  = "ceci est le titre de mon livre";
            test.NbPage = 957;
            test.Editor = editeur;
            test.DisplayAuthor();
            try
            {
                test.AddAuthor(guillaume);
                test.AddAuthor(arthur);
                for (int prout = 0; prout < 11; prout++)
                {
                    livre2.AddAuthor(maurice);
                }
            }
            catch (MediaException e)
            {
                Console.WriteLine(e.StackTrace);
            }


            Console.WriteLine(test.Title);
            Console.WriteLine("Il possede a : " + test.NbPage + " pages");
            Console.WriteLine("Editor name : " + test.Editor.Name);
            test.DisplayAuthor();
            Console.ReadKey();

            Console.WriteLine(Counter.increment());
            Console.WriteLine(Counter.increment());
            Console.ReadKey();

            test.Category = BookCategory.Poem;
            Console.WriteLine(test.Category);
            Console.ReadKey();

            Point3D p3d = new Point3D();

            p3d.X = 5;
            p3d.Z = 9;

            Media b2 = new Book();

            b2.Price = 10.0;
            Console.WriteLine(((Book)b2).NbPage);
            Console.WriteLine(b2.VATPrice);
            Console.ReadKey();
        }