Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
find_author_by_name,
view_authors_posts,
view_all_posts,
view_posts_by_category
view_posts_by_category,
find_number_of_posts_by_author
)


Expand Down Expand Up @@ -82,6 +83,8 @@ def view_menu():
elif choice == "6":
view_posts_by_category()
elif choice == "7":
find_number_of_posts_by_author()
elif choice == "8":
main()
else:
print("Invalid choice")
Expand All @@ -96,7 +99,8 @@ def view_menu_options():
print("4. View all post by specific author")
print("5. View all posts")
print("6. View all posts by category")
print("7. Return to main menu")
print("7. Find number of posts by author")
print("8. Return to main menu")

def edit_menu():
while True:
Expand Down
29 changes: 24 additions & 5 deletions lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,37 @@ def write_post():

def view_authors():
authors = Author.get_all()
for author in authors:
print(f"{author.name} id:{author.id}")
if authors:
for author in authors:
print(f"{author.name} id:{author.id}")
else :
print("No authors found")

def find_number_of_posts_by_author():
authors = [f"{author.id}: {author.name}" for author in Author.get_all()]
id_ = input(f"Input author's id to see number of posts. {authors}: ")
try:
author = Author.find_by_id(id_)
authors_posts = author.find_all_posts()
print(f"Author {author.name} has {len(authors_posts)} posts.")
except Exception as exc:
print(f"Author not found. {exc}")
find_number_of_posts_by_author()


def find_author_by_id():
id_ = input("Enter the author's id: ")
author = Author.find_by_id(id_)
print(f"--Author: {author.name}--") if author else print(f'Author {id_} not found')
if author:
print(f"\nAuthor found: --{author.name}--\n") if author else print(f'Author {id_} not found')
else:
print(f"\nAuthor {id_} not found\n")
find_author_by_id()

def find_author_by_name():
name = input("Enter the author's name: ")
author = Author.find_by_name(name)
print(f"--Author: {author.name}--") if author else print(
print(f"Author found: --{author.name}--") if author else print(
f'Author {name} not found')

def view_authors_posts():
Expand All @@ -63,7 +82,7 @@ def view_authors_posts():
try:
posts = author_object.find_all_posts()
for post in posts:
print(f"Title: {post.title}, Category: {post.category} || {post.content}")
print(f"Title: {post.title}, Category: {post.category} || {post.content}\n")
except Exception as exc:
print("Error finding posts: ", exc)
else :
Expand Down
4 changes: 3 additions & 1 deletion lib/models/author.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,6 @@ def find_by_name(cls, name):

def find_all_posts(self):
"""Return a list of Post objects associated with this Author"""
return [post for post in Post.get_all() if post.author_id == self.id]
try:
return [post for post in Post.get_all() if post.author_id == self.id]
except: Exception("No posts found by author")