示例#1
0
        public bool DeleteBook(int authorId, int id)
        {
            ValidateAuthor(authorId);
            var bookToDelete = authorsRepository.GetBooks().SingleOrDefault(b => b.Id == id);

            if (bookToDelete == null)
            {
                throw new NotFoundException("invalid book to delete");
            }
            return(authorsRepository.DeleteBook(id));
        }
        public IEnumerable <Author> GetAuthors(string orderBy, bool showBooks)
        {
            orderBy = orderBy.ToLower();
            if (!allowedOrderByQueries.Contains(orderBy))
            {
                throw new InvalidOperationException($"Invalid \" {orderBy} \" orderBy query param. The allowed values are {string.Join(",", allowedOrderByQueries)}");
            }

            var authors = authorsRepository.GetAuthors();

            foreach (var author in authors)
            {
                if (showBooks)
                {
                    author.books = authorsRepository.GetBooks().Where(b => b.AuthorId == author.Id);
                }
                else
                {
                    author.books = null;
                }
            }

            switch (orderBy)
            {
            case "id":
                return(authors.OrderBy(a => a.Id));

            case "name":
                return(authors.OrderBy(a => a.Name));

            case "lastname":
                return(authors.OrderBy(a => a.LastName));

            case "nationallity":
                return(authors.OrderBy(a => a.Name));

            default:
                return(authors);
            }
        }