Skip to content

Commit

Permalink
feat: used task_history table to get task_comment and add task_comment
Browse files Browse the repository at this point in the history
Using task_history table maintained action row as COMMENT and added comment
  • Loading branch information
varun2948 committed Feb 11, 2024
1 parent 5f0b92e commit 42384b7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 67 deletions.
76 changes: 11 additions & 65 deletions src/backend/app/tasks/tasks_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,15 +320,14 @@ async def get_task_comments(db: Session, project_id: int, task_id: int):
"""Get a list of tasks id for a project."""
query = text(
f"""
SELECT task_comment.id,users.username,task_comment.comment_text,task_comment.created_at FROM task_comment
LEFT JOIN users ON task_comment.commented_by = users.id
where project_id = {project_id} AND task_id = {task_id}
SELECT task_history.id,users.username,task_history.action_text,task_history.action_date FROM task_history
LEFT JOIN users ON task_history.user_id = users.id
where project_id = {project_id} AND task_id = {task_id} AND action = 'COMMENT'
"""
)

# Then execute the query with the desired parameter
result = db.execute(query)
print("result----")
print(result.__dict__)

# Convert the result to a list of dictionaries
result_dict_list = [
Expand All @@ -352,19 +351,18 @@ async def add_task_comments(
Returns:
- Dictionary with the details of the added comment
"""
currentdate= datetime.now()
# Construct the query to insert the comment and retrieve the details of the inserted comment
query = text(
f"""
WITH inserted_comment AS ( INSERT INTO task_comment(task_id,project_id,comment_text,commented_by)
VALUES({comment.task_id},{comment.project_id},'{comment.comment}',{user_data.id})
RETURNING task_comment.id, task_comment.comment_text, task_comment.created_at, task_comment.commented_by )
SELECT ic.id,username as commented_by,comment_text,created_at FROM inserted_comment ic
LEFT JOIN users u ON ic.commented_by = u.id;
WITH inserted_comment AS (
INSERT INTO task_history(project_id,task_id,"action",action_text,action_date,user_id)
VALUES({comment.project_id},{comment.task_id},'COMMENT','{comment.action_text}','{currentdate}',{user_data.id})
RETURNING task_history.id, task_history.action_text, task_history.action_date, task_history.user_id )
SELECT ic.id,username as user_id,action_text,action_date FROM inserted_comment ic
LEFT JOIN users u ON ic.user_id = u.id;
"""
)

print(query)

# Execute the query and commit the transaction
result = db.execute(query)
db.commit()
Expand All @@ -381,58 +379,6 @@ async def add_task_comments(
}


async def get_task_comment_info_by_id(db: Session, comment_id: int):
"""Get the project info only by id."""
db_project_info = (
db.query(db_models.TaskComment)
.filter(db_models.TaskComment.id == comment_id)
.order_by(db_models.TaskComment.id)
.first()
)
return db_project_info


async def delete_task_comment_by_id(
db: Session, task_comment_id: int, user_data: AuthUser
):
# Query to get the comment by its ID
get_comment_query = text(
"""
SELECT id, commented_by
FROM task_comment
WHERE id = :task_comment_id
"""
)

# Execute the query and commit the transaction
comment = db.execute(
get_comment_query, {"task_comment_id": task_comment_id}
).fetchone()
if comment is None:
raise HTTPException(status_code=404, detail="Task Comment not found")
# check for user
if comment.commented_by != user_data.id:
raise HTTPException(
status_code=404, detail="Cannot delete Task Comment. You are not the owner."
)

# Query to delete the comment by its ID and the authenticated user ID
delete_query = text(
"""
DELETE FROM task_comment
WHERE id = :task_comment_id AND commented_by = :user_id
"""
)
# Execute the query to delete the comment
result = db.execute(
delete_query, {"task_comment_id": task_comment_id, "user_id": user_data.id}
)
db.commit()

# Return the details of the added comment as a dictionary
return f"Task Comment {task_comment_id} deleted"


async def update_task_history(
tasks: List[tasks_schemas.Task], db: Session = Depends(database.get_db)
):
Expand Down
13 changes: 11 additions & 2 deletions src/backend/app/tasks/tasks_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,18 @@ def decrypt_password(self, value: str) -> str:
class TaskCommentRequest(BaseModel):
"""Task mapping history."""

comment: str
project_id: int
action_text: Any = Field(exclude=True)

task_id: int
project_id: int
comment: Optional[str] = None

@field_serializer("comment")
def convert_action_to_comment(self, value: str) -> Optional[str]:
"""Get the task history comment."""
if self.action_text:
return self.action_text
return None


class TaskCommentBase(BaseModel):
Expand Down

0 comments on commit 42384b7

Please sign in to comment.