-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Updates📢] Added comments to recipes
- Loading branch information
[esekyi]
committed
Sep 4, 2024
1 parent
f4f91f5
commit f295a62
Showing
5 changed files
with
344 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from app import db | ||
from app.models.comment import Comment | ||
|
||
class CommentService: | ||
@staticmethod | ||
def add_comment(recipe_id, user_id, text): | ||
"""Add a comment to a recipe.""" | ||
new_comment = Comment(recipe_id=recipe_id, user_id=user_id, text=text) | ||
db.session.add(new_comment) | ||
db.session.commit() | ||
return new_comment | ||
|
||
|
||
@staticmethod | ||
def get_comment_by_recipe(recipe_id): | ||
"""Retrieve all comments for a given recipe.""" | ||
return Comment.query.filter_by(recipe_id=recipe_id).order_by(Comment.created_at.desc()).all() | ||
|
||
|
||
@staticmethod | ||
def update_comment(comment_id, user_id, text): | ||
"""Updates a comments.""" | ||
comment = Comment.query.get_or_404(comment_id) | ||
if comment and comment.user_id == user_id: | ||
comment.text = text | ||
db.session.commit() | ||
return comment | ||
return None | ||
|
||
@staticmethod | ||
def delete_comment(comment_id, user_id): | ||
comment = Comment.query.get_or_404(comment_id) | ||
if comment and comment.user_id == user_id: | ||
db.session.delete(comment) | ||
db.session.commit() | ||
return True | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.