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
12 changes: 9 additions & 3 deletions lib/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
delete_author
)


#? Main Menu
def main():
while True:
menu()
Expand All @@ -35,7 +35,6 @@ def main():
else:
print("Invalid choice")


def menu():
print("Welcome to BLOGGER!")
print("Please select an option:")
Expand All @@ -45,6 +44,8 @@ def menu():
print("3. Edit Author or Post")
print("4. Delete Author or Post")


#? Create Menu
def create_menu():
while True:
create_menu_options()
Expand All @@ -66,6 +67,8 @@ def create_menu_options():
print("2. Write a post")
print("3. Return to Main Menu")


#? View Menu
def view_menu():
while True:
view_menu_options()
Expand Down Expand Up @@ -103,7 +106,9 @@ def view_menu_options():
print("6. View all posts by category")
print("7. Find number of posts by author")
print("8. Return to main menu")



#? Edit Menu
def edit_menu():
while True:
edit_menu_options()
Expand All @@ -123,6 +128,7 @@ def edit_menu_options():
print("3. Return to Menu")


#? Delete Menu
def delete_menu():
while True:
delete_menu_options()
Expand Down
10 changes: 5 additions & 5 deletions lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def write_post():
from cli import create_menu
author_ids = [f"{object.id}: {object.name}" for object in Author.get_all()]
if len(author_ids) > 0:
author_id = input(f"Enter an author id from this list {author_ids} ")
author_id = input(f"Enter an author id from this list {author_ids}: ")
while author_id not in [str(object.id) for object in Author.get_all()]:
print("name not in list")
author_id = input(f"Enter an author id from this list {author_ids} ")
Expand Down Expand Up @@ -135,7 +135,7 @@ def edit_post():
from cli import edit_menu
all_posts = [f"{post.id}: {post.content}" for post in Post.get_all()]
if len(all_posts) > 0:
post_id = input(f"Enter a post id from this list {all_posts}:")
post_id = input(f"Enter a post id from this list {all_posts}: ")
while post_id not in [str(post.id) for post in Post.get_all()]:
print("Post id is not in list")
post_id = input(f"Enter a post id from this list {all_posts}")
Expand All @@ -162,7 +162,7 @@ def delete_post():
print(f"\nPost -{title}- not found\n")

def delete_author():
from cli import edit_menu
from cli import delete_menu
author_ids = [f"{object.id}: {object.name}" for object in Author.get_all()]
if len(author_ids) > 0:
author_id = input(f"To remove an author, select an author id from this list {author_ids} ")
Expand All @@ -173,5 +173,5 @@ def delete_author():
print(f"You deleted {selected_author.name}")
selected_author.delete()
else:
print("There are no posts to edit")
edit_menu
print("There are no authors to delete")
delete_menu()
9 changes: 9 additions & 0 deletions lib/models/author.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def drop_table(cls):
CURSOR.execute(sql)
CONN.commit()

#? Saves an author to the database
def save(self):
""" Insert a new row with the the name of the author object.
Update object id attribute using the primary key value of new row.
Expand All @@ -59,13 +60,15 @@ def save(self):
self.id = CURSOR.lastrowid
type(self).all[self.id] = self

#? Creates and saves author to the database
@classmethod
def create(cls, name):
""" Initialize a new Author instance and save the object to the database """
author = cls(name)
author.save()
return author

#? Updates author and saves to database
def update(self):
"""Update the table row corresponding to the current Author instance."""
sql = """
Expand All @@ -76,6 +79,7 @@ def update(self):
CURSOR.execute(sql, (self.name, self.id))
CONN.commit()

#? Deltes author from database
def delete(self):
"""Delete the table row corresponding to the current Author instance,
delete the author entry, and reassign id attribute"""
Expand All @@ -95,6 +99,7 @@ def delete(self):
self.id = None


#? Checks to see if an author exists in database
@classmethod
def instance_from_db(cls, row):
"""Return an Author object having the attribute values from the table row."""
Expand All @@ -111,6 +116,7 @@ def instance_from_db(cls, row):
cls.all[author.id] = author
return author

#? Returns list of all authors from database
@classmethod
def get_all(cls):
"""Return a list containing an Author object per row in the table"""
Expand All @@ -123,6 +129,7 @@ def get_all(cls):

return [cls.instance_from_db(row) for row in rows]

#? Finds an author from database by their id
@classmethod
def find_by_id(cls, id):
"""Return a Author object corresponding to the table row matching the specified primary key"""
Expand All @@ -135,6 +142,7 @@ def find_by_id(cls, id):
row = CURSOR.execute(sql, (id,)).fetchone()
return cls.instance_from_db(row) if row else None

#? Finds author from database by their name
@classmethod
def find_by_name(cls, name):
"""Return an Author object corresponding to first table row matching specified name"""
Expand All @@ -147,6 +155,7 @@ def find_by_name(cls, name):
row = CURSOR.execute(sql, (name,)).fetchone()
return cls.instance_from_db(row) if row else None

#? Finds all posts of author instance from database
def find_all_posts(self):
"""Return a list of Post objects associated with this Author"""
try:
Expand Down
1 change: 1 addition & 0 deletions lib/models/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def find_by_title(cls, title):
row = CURSOR.execute(sql, (title,)).fetchone()
return cls.instance_from_db(row) if row else None

#? Returns list of posts by a specific category
@classmethod
def find_by_category(cls, category):
"""Return list of Post objects corresponding to table rows matching specified category"""
Expand Down