feat: adding get user
This commit is contained in:
parent
73167c15f6
commit
c582a94027
|
@ -47,3 +47,13 @@ def get_quiz_recommendation(
|
||||||
controller: QuizController = Provide[Container.quiz_controller],
|
controller: QuizController = Provide[Container.quiz_controller],
|
||||||
):
|
):
|
||||||
return controller.get_quiz_recommendation()
|
return controller.get_quiz_recommendation()
|
||||||
|
|
||||||
|
|
||||||
|
@quiz_bp.route("/user/<user_id>", methods=["GET"])
|
||||||
|
@inject
|
||||||
|
def get_user_quiz(controller: QuizController = Provide[Container.quiz_controller]):
|
||||||
|
page = request.args.get("page", default=1, type=int)
|
||||||
|
page_size = request.args.get("page_size", default=10, type=int)
|
||||||
|
return controller.get_user_quiz(
|
||||||
|
user_id=request.view_args["user_id"], page=page, page_size=page_size
|
||||||
|
)
|
||||||
|
|
|
@ -82,3 +82,14 @@ class QuizController:
|
||||||
print("yps")
|
print("yps")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return make_error_response(e)
|
return make_error_response(e)
|
||||||
|
|
||||||
|
def get_user_quiz(self, user_id, page=1, page_size=10):
|
||||||
|
try:
|
||||||
|
result = self.quiz_service.get_user_quiz(
|
||||||
|
user_id=user_id, page=page, page_size=page_size
|
||||||
|
)
|
||||||
|
return make_response(
|
||||||
|
message="User quizzes retrieved successfully", data=result
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
return make_error_response(e)
|
||||||
|
|
|
@ -41,8 +41,8 @@ def createApp() -> Flask:
|
||||||
app.register_blueprint(user_blueprint, url_prefix="/api")
|
app.register_blueprint(user_blueprint, url_prefix="/api")
|
||||||
app.register_blueprint(quiz_bp, url_prefix="/api/quiz")
|
app.register_blueprint(quiz_bp, url_prefix="/api/quiz")
|
||||||
|
|
||||||
# for rule in app.url_map.iter_rules():
|
for rule in app.url_map.iter_rules():
|
||||||
# print(f"Route: {rule} -> Methods: {rule.methods}")
|
print(f"Route: {rule} -> Methods: {rule.methods}")
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
from bson import ObjectId
|
from bson import ObjectId
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
from models import QuizEntity
|
from models import QuizEntity
|
||||||
|
from pymongo.database import Database
|
||||||
|
|
||||||
|
|
||||||
class QuizRepository:
|
class QuizRepository:
|
||||||
def __init__(self, db):
|
def __init__(self, db: Database):
|
||||||
self.collection = db.quiz
|
self.collection = db.quiz
|
||||||
|
|
||||||
def create(self, quiz: QuizEntity) -> str:
|
def create(self, quiz: QuizEntity) -> str:
|
||||||
|
@ -18,6 +19,17 @@ class QuizRepository:
|
||||||
return QuizEntity(**data)
|
return QuizEntity(**data)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def get_by_user_id(
|
||||||
|
self, user_id: str, page: int = 1, page_size: int = 10
|
||||||
|
) -> List[QuizEntity]:
|
||||||
|
skip = (page - 1) * page_size
|
||||||
|
cursor = (
|
||||||
|
self.collection.find({"user_id": ObjectId(user_id)})
|
||||||
|
.skip(skip)
|
||||||
|
.limit(page_size)
|
||||||
|
)
|
||||||
|
return [QuizEntity(**doc) for doc in cursor]
|
||||||
|
|
||||||
def get_all(self, skip: int = 0, limit: int = 10) -> List[QuizEntity]:
|
def get_all(self, skip: int = 0, limit: int = 10) -> List[QuizEntity]:
|
||||||
cursor = self.collection.find().skip(skip).limit(limit)
|
cursor = self.collection.find().skip(skip).limit(limit)
|
||||||
return [QuizEntity(**doc) for doc in cursor]
|
return [QuizEntity(**doc) for doc in cursor]
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
from typing import List
|
||||||
from repositories import QuizRepository
|
from repositories import QuizRepository
|
||||||
from schemas import QuizGetSchema
|
from schemas import QuizGetSchema
|
||||||
from exception import DataNotFoundException
|
from exception import DataNotFoundException
|
||||||
|
@ -15,6 +16,12 @@ class QuizService:
|
||||||
|
|
||||||
return map_quiz_entity_to_schema(data)
|
return map_quiz_entity_to_schema(data)
|
||||||
|
|
||||||
|
def get_user_quiz(
|
||||||
|
self, user_id: str, page: int = 1, page_size: int = 10
|
||||||
|
) -> List[QuizGetSchema]:
|
||||||
|
quizzes = self.quiz_repository.get_by_user_id(user_id, page, page_size)
|
||||||
|
return [QuizGetSchema.model_validate(quiz) for quiz in quizzes]
|
||||||
|
|
||||||
def create_quiz(self, quiz_data):
|
def create_quiz(self, quiz_data):
|
||||||
return self.quiz_repository.create(quiz_data)
|
return self.quiz_repository.create(quiz_data)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue