Skip to content

Commit

Permalink
Added a delete route and delete post analysis collection function
Browse files Browse the repository at this point in the history
  • Loading branch information
JmScherer committed Jan 12, 2024
1 parent 006b10e commit 802f419
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
29 changes: 29 additions & 0 deletions backend/src/repository/analysis_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
from uuid import uuid4

from fastapi import HTTPException, status
from pymongo import ReturnDocument
from ..models.event import Event
from ..enums import EventType
Expand Down Expand Up @@ -497,3 +498,31 @@ def add_discussion_post(self, analysis_name: str, discussion_post: object):
updated_document.pop("_id", None)

return updated_document['discussions']

def delete_discussion_post(
self,
discussion_post_id: str,
client_id: str,
analysis_name: str,
):
""" Removes a discussion post from an analysis """

found_document = self.collection.find_one({"name": analysis_name})

found_document.pop("_id", None)

for discussion in found_document['discussions']:
if discussion['post_id'] == discussion_post_id:
if not discussion['author_id'] == client_id:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, detail="User cannot delete post they did not author."
)

updated_document = self.collection.find_one_and_update({"name": analysis_name}, {
"$pull": {"discussions": {"post_id": discussion_post_id}}
},
return_document=ReturnDocument.AFTER)

updated_document.pop("_id", None)

return updated_document['discussions']
14 changes: 13 additions & 1 deletion backend/src/routers/analysis_discussion_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def get_analysis_discussions(analysis_name: str):


@router.post("/{analysis_name}/discussions")
def add_analysis_discussions(
def add_analysis_discussion(
analysis_name: str,
discussion_content: str = Form(...),
repositories=Depends(database),
Expand All @@ -65,3 +65,15 @@ def add_analysis_discussions(
}

return repositories['analysis'].add_discussion_post(analysis_name, new_discussion_post)


@router.delete("/{analysis_name}/discussions")
def delete_analysis_discussion(
analysis_name: str,
discussion_post_id: str,
repositories=Depends(database),
client_id: VerifyUser = Security(get_current_user)
):
logger.info("Deleting post %s by user '%s' from the analysis '%s'", discussion_post_id, client_id, analysis_name)

return repositories['analysis'].delete_discussion_post(discussion_post_id, client_id, analysis_name)

0 comments on commit 802f419

Please sign in to comment.