Skip to content

Commit

Permalink
Merge pull request #897 from JoshuaOloton/fix/help-center
Browse files Browse the repository at this point in the history
fix: fixed help center to integrate with frontend
  • Loading branch information
Goketech authored Aug 15, 2024
2 parents f534dce + 305ee5a commit d807d9d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
24 changes: 15 additions & 9 deletions api/v1/routes/topic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import Optional
from fastapi import (
APIRouter,
Depends,
Response,
status,
)
from fastapi.encoders import jsonable_encoder
Expand Down Expand Up @@ -37,7 +39,7 @@ async def retrieve_all_topics(
data=jsonable_encoder(topics)
)

@topic.get('/topic/{topic_id}', response_model=TopicData)
@topic.get('/topics/{topic_id}', response_model=TopicData)
async def retrieve_topic(
topic_id: str,
db: Session = Depends(get_db)
Expand All @@ -63,7 +65,8 @@ async def retrieve_topic(

@topic.get('/search', response_model=TopicList)
async def search_for_topic(
schema: TopicSearchSchema,
title: Optional[str] = None,
content: Optional[str] = None,
db: Session = Depends(get_db)
):
"""
Expand All @@ -76,17 +79,17 @@ async def search_for_topic(
Returns:
Response: a response object containing details if successful or appropriate errors if not
"""
topics = topic_service.search(db=db,search_query=schema.query)
topics = topic_service.search(db=db,title_query=title, content_query=content)

return success_response(
status_code=status.HTTP_200_OK,
message='Topics fetched successfully',
data=jsonable_encoder(topics)
)

@topic.delete('/topics', status_code=status.HTTP_204_NO_CONTENT)
@topic.delete('/topics/{topic_id}')
async def delete_a_topic(
id: TopicDeleteSchema,
topic_id: str,
db: Session = Depends(get_db),
current_user: User = Depends(user_service.get_current_super_admin),
):
Expand All @@ -102,11 +105,14 @@ async def delete_a_topic(
Response: a response object containing details if successful or appropriate errors if not
"""

topic_service.delete(db, id.id)
topic_service.delete(db, topic_id)

return Response(status_code=status.HTTP_204_NO_CONTENT)

@topic.patch("/topics")

@topic.patch("/topics/{topic_id}")
async def update_a_topic(
topic_id: str,
schema: TopicUpdateSchema,
db: Session = Depends(get_db),
current_user: User = Depends(user_service.get_current_super_admin),
Expand All @@ -123,11 +129,11 @@ async def update_a_topic(
Returns:
Response: a response object containing details if successful or appropriate errors if not
"""
updated_topic = topic_service.update(db, schema)
updated_topic = topic_service.update(db, schema, topic_id)
return success_response(
status_code=status.HTTP_200_OK,
message='Topic updated successfully!',
# data=jsonable_encoder(updated_topic)
data=jsonable_encoder(updated_topic)
)

@topic.post("/topics")
Expand Down
1 change: 0 additions & 1 deletion api/v1/schemas/topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class TopicList(BaseModel):
data: List[TopicData]

class TopicUpdateSchema(BaseModel):
id: str
title: Optional[str] = None
content: Optional[str] = None
tags: Optional[List[str]] = None
Expand Down
13 changes: 5 additions & 8 deletions api/v1/services/topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ def fetch(self, db: Session, id: str):
topic = check_model_existence(db, Topic, id)
return topic

def update(self, db: Session, schema: TopicUpdateSchema):
def update(self, db: Session, schema: TopicUpdateSchema, topic_id: str):
'''Updates a topic'''

topic = self.fetch(db=db, id=schema.id)
topic = self.fetch(db=db, id=topic_id)

update_data = schema.dict(exclude_unset=True, exclude={"id"})
for key, value in update_data.items():
Expand All @@ -60,17 +60,14 @@ def delete(self, db: Session, id: str):
topic = self.fetch(db=db, id=id)
db.delete(topic)
db.commit()
return True

def search(self, db: Session, search_query: str):
def search(self, db: Session, title_query: str, content_query: str):
"""
Search for topics based on title, content, tags, or topic IDs.
"""
query = db.query(Topic).filter(
(Topic.title.ilike(f'%{search_query}%')) |
(Topic.content.ilike(f'%{search_query}%')) |
(Topic.tags.any(search_query)) |
(Topic.id == search_query) # Include searching by topic ID
(Topic.title.ilike(f'%{title_query}%')) |
(Topic.content.ilike(f'%{content_query}%'))
)
return query.all()

Expand Down
7 changes: 3 additions & 4 deletions tests/v1/topic/test_topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,9 @@ def mock_get(model, ident):

headers = {'Authorization': f'Bearer {access_token_user1}'}
data = {
"id": test_topic.id,
"title": "Uploading profile picture."
}
response = client.patch(f"/api/v1/help-center/topics", headers=headers, json=data)
response = client.patch(f"/api/v1/help-center/topics/{test_topic.id}", headers=headers, json=data)
assert response.status_code == 200

def test_delete_topic(
Expand All @@ -119,7 +118,7 @@ def mock_get(model, ident):
mock_db_session.query.return_value.filter_by.return_value.first.return_value = [test_topic]

headers = {'Authorization': f'Bearer {access_token_user1}'}
response = client.request("DELETE",f"/api/v1/help-center/topics", headers=headers, json={"id":test_topic.id})
response = client.delete(f"/api/v1/help-center/topics/{test_topic.id}", headers=headers)
assert response.status_code == 204

def test_search_topic(
Expand All @@ -138,7 +137,7 @@ def mock_get(model, ident):
mock_db_session.query.return_value.filter.return_value.first.return_value = test_user

mock_db_session.query.return_value.filter_by.return_value.first.return_value = [test_topic]
response = client.request("GET","/api/v1/help-center/search", json={"query":test_topic.title})
response = client.get(f"/api/v1/help-center/search?title={test_topic.title}")
assert response.status_code == 200

def test_fetch_a_topic(
Expand Down

0 comments on commit d807d9d

Please sign in to comment.