public List <BookView> GetBookList(List <Book> books)
        {
            List <BookView> booksViews = new List <BookView>();

            foreach (Book item in books)
            {
                Author author = db.Authors.FirstOrDefault(a => a.Id == item.AuthorId);

                string authorName = NameRefactorer
                                    .GetFullName(author.FirstName, author.MiddleName, author.LastName);

                BookView book = new BookView
                {
                    Id         = item.Id,
                    Title      = item.Title,
                    Genre      = item.Genre,
                    AuthorName = authorName,
                    Year       = item.Year,
                    SavedTimes = GetBookSavedTimes(item)
                };

                booksViews.Add(book);
            }

            return(booksViews);
        }
        public IActionResult Edit(EditAuthorDTO newData)
        {
            string fullName = NameRefactorer
                              .GetFullName(newData.FirstName, newData.MiddleName, newData.LastName);

            Author newAuthor = authorServices.FindAuthor(fullName);
            Author oldAuthor = authorServices.FindAuthor(newData.Id);

            string oldFullName = NameRefactorer
                                 .GetFullName(oldAuthor.FirstName, oldAuthor.MiddleName, oldAuthor.LastName);

            if (newAuthor != null && fullName != oldFullName)
            {
                ViewData.Add("NameRepeatingError", "An author with this name already exists!");
                return(View(newData));
            }

            try
            {
                authorServices.UpdateAuthor(newData);
            }
            catch (ArgumentException ae)
            {
                ViewData.Add("ShortName", ae.Message);
                return(View(newData));
            }

            return(RedirectToAction(nameof(Authors)));
        }
        public Author FindAuthor(string fullName)
        {
            Author author = GetAllAuthors().FirstOrDefault(
                a => NameRefactorer.GetFullName(
                    a.FirstName, a.MiddleName, a.LastName) == fullName);

            return author;
        }
        public int GetAuthorIdFromFullName(string authorFullName)
        {
            Author author = GetAllAuthors().FirstOrDefault(
                a => NameRefactorer.GetFullName(
                    a.FirstName, a.MiddleName, a.LastName) == authorFullName);

            return author.Id;
        }
        public void IsFindingCorrectAuthorById()
        {
            Author author = authorServices.FindAuthor(1);

            string fullName = NameRefactorer
                              .GetFullName(author.FirstName, author.MiddleName, author.LastName);

            Assert.AreEqual("FirstName1 LastName1", fullName);
            //Assert.IsNotNull(author);
        }
        public void IsFindingCorrectSavedTimesOfBook()
        {
            Book   book   = db.Books.FirstOrDefault(b => b.Id == 1);
            Author author = bookServices.GetBookAuthor(book);

            string name   = "firstName1 lastName1";
            string result = NameRefactorer
                            .GetFullName(author.FirstName, author.MiddleName, author.LastName);

            Assert.AreEqual(name, result);
        }
Exemple #7
0
        public async Task <IActionResult> Create(BookDTO book)
        {
            if (ValidateProperties(book))
            {
                return(View());
            }

            if (bookServices.FindBook(book.Title) != null)
            {
                ViewData.Add("RepeatingTitle", "Book already exists!");

                return(View());
            }

            string fullName = NameRefactorer
                              .GetFullName(book.FirstName, null, book.LastName);

            Author author    = authorServices.FindAuthor(fullName);
            Book   bookToAdd = null;

            try
            {
                if (author == null)
                {
                    author = new Author
                    {
                        FirstName = book.FirstName,
                        LastName  = book.LastName
                    };

                    authorServices.AddAuthor(author);
                }

                bookToAdd = new Book
                {
                    Title    = book.Title,
                    Genre    = book.Genre,
                    AuthorId = author.Id
                };
            }
            catch (ArgumentException ae)
            {
                ViewData.Add("ShortName", ae.Message);

                return(View());
            }

            bookServices.AddBook(bookToAdd);
            await AddBookToUser(bookToAdd.Id);

            return(RedirectToAction(nameof(Books)));
        }
        public List<AuthorView> GetAuthorList(List<Author> authors)
        {
            List<AuthorView> result = new List<AuthorView>();

            foreach (var item in authors)
            {
                AuthorView author = new AuthorView
                {
                    Id = item.Id,
                    FullName = NameRefactorer.GetFullName(item.FirstName, item.MiddleName, item.LastName),
                    BooksCount = GetAuthorBooksCount(item),
                    Country = GetAuthorCountry(item)
                };

                result.Add(author);
            }

            return result;
        }
        public List <BookView> GetBooksOfUserViews(List <Book> books)
        {
            List <BookView> result = new List <BookView>();

            foreach (var item in books)
            {
                Author author = db.Authors.FirstOrDefault(a => a.Id == item.AuthorId);

                BookView bookView = new BookView
                {
                    Id         = item.Id,
                    Title      = item.Title,
                    Genre      = item.Genre,
                    AuthorName = NameRefactorer
                                 .GetFullName(author.FirstName, author.MiddleName, author.LastName)
                };

                result.Add(bookView);
            }

            return(result);
        }
        private FullAuthorView GetDetails(Author author)
        {
            if (author == null)
            {
                return(null);
            }

            string name = NameRefactorer.GetFullName(author.FirstName, author.MiddleName, author.LastName);

            string countryName = authorServices.GetAuthorCountry(author);
            string birthday    = author.Birthday.ToString() != "" ? ((DateTime)author.Birthday).ToLongDateString() : "Unknown";

            FullAuthorView result = new FullAuthorView
            {
                Id        = author.Id,
                Name      = name,
                BookCount = authorServices.GetAuthorBooksCount(author),
                Birthday  = birthday,
                Nickname  = author.Nickname != null ? author.Nickname : "No/Unknown",
                Country   = countryName
            };

            return(result);
        }
Exemple #11
0
        private FullBookView GetDetails(Book book)
        {
            if (book == null)
            {
                return(null);
            }

            Author author         = authorServices.FindAuthor(book.AuthorId);
            string authorFullName = NameRefactorer
                                    .GetFullName(author.FirstName, author.MiddleName, author.LastName);

            FullBookView result = new FullBookView
            {
                Id         = book.Id,
                Title      = book.Title,
                Genre      = book.Genre,
                AuthorName = authorFullName,
                Year       = book.Year,
                Pages      = book.Pages,
                SavedTimes = bookServices.GetBookSavedTimes(book)
            };

            return(result);
        }
Exemple #12
0
        public void ReturnTwoNamesIfMiddleIsNull()
        {
            string result = NameRefactorer.GetFullName("Vladimir", null, "Stoyanov");

            Assert.That(result, Is.EqualTo("Vladimir Stoyanov"));
        }
Exemple #13
0
        public void ReturnThreeNumbersWhenMiddleNameIsNotNull()
        {
            string result = NameRefactorer.GetFullName("Vladimir", "Rumenov", "Stoyanov");

            Assert.That(result, Is.EqualTo("Vladimir Rumenov Stoyanov"));
        }