in microservices/course_ingestion/services/learning_content_inference.py [0:0]
def get_learning_contents(skip: int, limit: int, sort_by: str,
order_by: str, search_query: str) -> Union[Any, str]:
"""
Function for get all learning contents
:param skip: int
:param limit: int
:param sort_by: str
:param order_by: str
:param search_query: str
:return: dict
"""
l_contents = []
ord_by = order_by
if sort_by == "ascending" and order_by != "title":
ord_by = order_by
elif sort_by == "descending" and order_by != "title":
ord_by = "-{}".format(order_by)
try:
contents = LearningContentItem.collection.order(ord_by).fetch()
if contents:
for content in contents:
data = content.get_fields(reformat_datetime=True)
data["content_id"] = content.id
l_contents.append(data)
if search_query is not None:
l_contents = [content for content in l_contents if
search_query.lower() in content["title"].lower()]
if sort_by == "ascending" and order_by == "title":
l_contents = sorted(l_contents, key=lambda i: i["title"].lower())
elif sort_by == "descending" and order_by == "title":
l_contents = sorted(l_contents, key=lambda i: i["title"].lower(),
reverse=True)
if skip >= 0 and limit > 0:
result = pagination(payload=l_contents, skip=skip, limit=limit)
res = {
"data": result,
"total_rec": len(l_contents)
}
elif skip < 0 or limit < 0:
raise Exception("The skip and limit value should be a positive number")
elif skip == 0 and limit == 0:
return l_contents
else:
res = {
"data": l_contents,
"total_rec": len(l_contents)
}
return res
else:
raise ResourceNotFoundException("No learning_contents found")
except Exception as e:
Logger.error("No Learning content found: {}".format(e))
return False