mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: get courses data in collections view
This commit is contained in:
parent
ce785fd078
commit
39dc5ab880
4 changed files with 59 additions and 18 deletions
|
|
@ -6,6 +6,7 @@ import styled from "styled-components";
|
||||||
import { Title } from "../../../../components/UI/Elements/Styles/Title";
|
import { Title } from "../../../../components/UI/Elements/Styles/Title";
|
||||||
import { deleteCollection, getOrgCollections } from "../../../../services/collections";
|
import { deleteCollection, getOrgCollections } from "../../../../services/collections";
|
||||||
import { getOrganizationContextInfo } from "../../../../services/orgs";
|
import { getOrganizationContextInfo } from "../../../../services/orgs";
|
||||||
|
import { getBackendUrl } from "../../../../services/config";
|
||||||
|
|
||||||
function Collections() {
|
function Collections() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
@ -48,6 +49,13 @@ function Collections() {
|
||||||
{collections.map((collection: any) => (
|
{collections.map((collection: any) => (
|
||||||
<CollectionItem key={collection.collection_id}>
|
<CollectionItem key={collection.collection_id}>
|
||||||
<Link href={"/org/" + orgslug + "/collections/" + collection.collection_id}>{collection.name}</Link>
|
<Link href={"/org/" + orgslug + "/collections/" + collection.collection_id}>{collection.name}</Link>
|
||||||
|
<CourseMiniThumbnail>
|
||||||
|
{collection.courses.map((course: any) => (
|
||||||
|
<Link key={course.course_id} href={"/org/" + orgslug + "/course/" + course.course_id.substring(7)}>
|
||||||
|
<img key={course.course_id} src={`${getBackendUrl()}content/uploads/img/${course.thumbnail}`} alt={course.name} />
|
||||||
|
</Link>
|
||||||
|
))}
|
||||||
|
</CourseMiniThumbnail>
|
||||||
<button onClick={() => deleteCollectionAndFetch(collection.collection_id)}>Delete</button>
|
<button onClick={() => deleteCollectionAndFetch(collection.collection_id)}>Delete</button>
|
||||||
</CollectionItem>
|
</CollectionItem>
|
||||||
))}
|
))}
|
||||||
|
|
@ -59,7 +67,7 @@ function Collections() {
|
||||||
|
|
||||||
const CollectionItem = styled.div`
|
const CollectionItem = styled.div`
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: row;
|
||||||
place-items: center;
|
place-items: center;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
|
|
@ -75,4 +83,19 @@ const CollectionItem = styled.div`
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
const CourseMiniThumbnail = styled.div`
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
img {
|
||||||
|
width: 20px;
|
||||||
|
height: 20px;
|
||||||
|
border-radius: 5px;
|
||||||
|
margin: 5px;
|
||||||
|
transition: all 0.2s ease-in-out;
|
||||||
|
}
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
opacity: 0.8;
|
||||||
|
}
|
||||||
|
`;
|
||||||
export default Collections;
|
export default Collections;
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ const CoursesIndexPage = () => {
|
||||||
<Layout title="Courses">
|
<Layout title="Courses">
|
||||||
<Header></Header>
|
<Header></Header>
|
||||||
<Title>
|
<Title>
|
||||||
{orgslug} courses :{" "}
|
{orgslug} Courses :{" "}
|
||||||
<Link href={"/org/" + orgslug + "/courses/new"}>
|
<Link href={"/org/" + orgslug + "/courses/new"}>
|
||||||
|
|
||||||
<button>+</button>
|
<button>+</button>
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ async def api_get_collection(collection_id: str, current_user: PublicUser = Depe
|
||||||
|
|
||||||
|
|
||||||
@router.get("/page/{page}/limit/{limit}")
|
@router.get("/page/{page}/limit/{limit}")
|
||||||
async def api_get_collection_by(page: int, limit: int, current_user: PublicUser = Depends(get_current_user)):
|
async def api_get_collections_by(page: int, limit: int, current_user: PublicUser = Depends(get_current_user)):
|
||||||
"""
|
"""
|
||||||
Get collections by page and limit
|
Get collections by page and limit
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,8 @@ from datetime import datetime
|
||||||
class Collection(BaseModel):
|
class Collection(BaseModel):
|
||||||
name: str
|
name: str
|
||||||
description: str
|
description: str
|
||||||
courses: List[str] # course_id
|
courses: List[str] # course_id
|
||||||
org_id: str # org_id
|
org_id: str # org_id
|
||||||
|
|
||||||
|
|
||||||
class CollectionInDB(Collection):
|
class CollectionInDB(Collection):
|
||||||
|
|
@ -36,7 +36,7 @@ async def get_collection(collection_id: str, current_user: PublicUser):
|
||||||
|
|
||||||
# verify collection rights
|
# verify collection rights
|
||||||
await verify_collection_rights(collection_id, current_user, "read")
|
await verify_collection_rights(collection_id, current_user, "read")
|
||||||
|
|
||||||
if not collection:
|
if not collection:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_409_CONFLICT, detail="Collection does not exist")
|
status_code=status.HTTP_409_CONFLICT, detail="Collection does not exist")
|
||||||
|
|
@ -50,9 +50,10 @@ async def create_collection(collection_object: Collection, current_user: PublicU
|
||||||
collections = learnhouseDB["collections"]
|
collections = learnhouseDB["collections"]
|
||||||
|
|
||||||
# find if collection already exists using name
|
# find if collection already exists using name
|
||||||
isCollectionNameAvailable = collections.find_one({"name": collection_object.name})
|
isCollectionNameAvailable = collections.find_one(
|
||||||
|
{"name": collection_object.name})
|
||||||
# TODO
|
|
||||||
|
# TODO
|
||||||
# await verify_collection_rights("*", current_user, "create")
|
# await verify_collection_rights("*", current_user, "create")
|
||||||
|
|
||||||
if isCollectionNameAvailable:
|
if isCollectionNameAvailable:
|
||||||
|
|
@ -62,7 +63,8 @@ async def create_collection(collection_object: Collection, current_user: PublicU
|
||||||
# generate collection_id with uuid4
|
# generate collection_id with uuid4
|
||||||
collection_id = str(f"collection_{uuid4()}")
|
collection_id = str(f"collection_{uuid4()}")
|
||||||
|
|
||||||
collection = CollectionInDB(collection_id=collection_id, **collection_object.dict())
|
collection = CollectionInDB(
|
||||||
|
collection_id=collection_id, **collection_object.dict())
|
||||||
|
|
||||||
collection_in_db = collections.insert_one(collection.dict())
|
collection_in_db = collections.insert_one(collection.dict())
|
||||||
|
|
||||||
|
|
@ -83,7 +85,6 @@ async def update_collection(collection_object: Collection, collection_id: str, c
|
||||||
|
|
||||||
collection = collections.find_one({"collection_id": collection_id})
|
collection = collections.find_one({"collection_id": collection_id})
|
||||||
|
|
||||||
|
|
||||||
if not collection:
|
if not collection:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_409_CONFLICT, detail="Collection does not exist")
|
status_code=status.HTTP_409_CONFLICT, detail="Collection does not exist")
|
||||||
|
|
@ -91,7 +92,8 @@ async def update_collection(collection_object: Collection, collection_id: str, c
|
||||||
updated_collection = CollectionInDB(
|
updated_collection = CollectionInDB(
|
||||||
collection_id=collection_id, **collection_object.dict())
|
collection_id=collection_id, **collection_object.dict())
|
||||||
|
|
||||||
collections.update_one({"collection_id": collection_id}, {"$set": updated_collection.dict()})
|
collections.update_one({"collection_id": collection_id}, {
|
||||||
|
"$set": updated_collection.dict()})
|
||||||
|
|
||||||
return Collection(**updated_collection.dict())
|
return Collection(**updated_collection.dict())
|
||||||
|
|
||||||
|
|
@ -99,7 +101,7 @@ async def update_collection(collection_object: Collection, collection_id: str, c
|
||||||
async def delete_collection(collection_id: str, current_user: PublicUser):
|
async def delete_collection(collection_id: str, current_user: PublicUser):
|
||||||
await check_database()
|
await check_database()
|
||||||
|
|
||||||
await verify_collection_rights(collection_id, current_user,"delete")
|
await verify_collection_rights(collection_id, current_user, "delete")
|
||||||
|
|
||||||
collections = learnhouseDB["collections"]
|
collections = learnhouseDB["collections"]
|
||||||
|
|
||||||
|
|
@ -121,20 +123,36 @@ async def delete_collection(collection_id: str, current_user: PublicUser):
|
||||||
# Misc
|
# Misc
|
||||||
####################################################
|
####################################################
|
||||||
|
|
||||||
|
|
||||||
async def get_collections(page: int = 1, limit: int = 10):
|
async def get_collections(page: int = 1, limit: int = 10):
|
||||||
## TODO : auth
|
## TODO : auth
|
||||||
await check_database()
|
await check_database()
|
||||||
collections = learnhouseDB["collections"]
|
collections = learnhouseDB["collections"]
|
||||||
|
|
||||||
# get all collections from database
|
# get all collections from database without ObjectId
|
||||||
all_collections = collections.find().sort("name", 1).skip(10 * (page - 1)).limit(limit)
|
all_collections = collections.find({}).sort(
|
||||||
|
"name", 1).skip(10 * (page - 1)).limit(limit)
|
||||||
# TODO : Check rights for each collection
|
|
||||||
return [json.loads(json.dumps(collection, default=str)) for collection in all_collections]
|
|
||||||
|
|
||||||
|
# create list of collections and include courses in each collection
|
||||||
|
collections_list = []
|
||||||
|
for collection in all_collections:
|
||||||
|
collection = CollectionInDB(**collection)
|
||||||
|
collections_list.append(collection)
|
||||||
|
|
||||||
|
collection_courses = [course for course in collection.courses]
|
||||||
|
# add courses to collection
|
||||||
|
courses = learnhouseDB["courses"]
|
||||||
|
collection.courses = []
|
||||||
|
collection.courses = courses.find(
|
||||||
|
{"course_id": {"$in": collection_courses}}, {'_id': 0})
|
||||||
|
|
||||||
|
collection.courses = [course for course in collection.courses]
|
||||||
|
|
||||||
|
return collections_list
|
||||||
|
|
||||||
#### Security ####################################################
|
#### Security ####################################################
|
||||||
|
|
||||||
|
|
||||||
async def verify_collection_rights(collection_id: str, current_user: PublicUser, action: str):
|
async def verify_collection_rights(collection_id: str, current_user: PublicUser, action: str):
|
||||||
await check_database()
|
await check_database()
|
||||||
collections = learnhouseDB["collections"]
|
collections = learnhouseDB["collections"]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue