Skip to content

Commit

Permalink
feat: GET /moodle/files endpoint, MinioRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
raydenoir committed Jun 30, 2024
1 parent a240fda commit 23bbcb0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/modules/moodle/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
from fastapi.responses import Response, StreamingResponse
from minio.deleteobjects import DeleteObject
from pymongo import UpdateOne
from typing import List, Dict

from src.api.dependencies import VerifiedDep
from src.modules.moodle.schemas import InCourses, InSections, InContents
from src.modules.moodle.utils import content_to_minio_object, module_to_minio_prefix, checker
from src.storages.minio import minio_client
from src.repositories.minio.repository import minio_repository
from src.storages.mongo import MoodleCourse, MoodleEntry
from src.storages.mongo.moodle import MoodleEntrySchema, MoodleContentSchema

Expand All @@ -34,6 +36,16 @@ async def preview_moodle(course_id: int, module_id: int, filename: str):
)


@router.get(
"/files",
response_model=List[Dict[str, any]],
responses={200: {"description": "Success"}},
)
async def get_moodle_files(_: VerifiedDep) -> List[Dict[str, any]]:
moodle_objects = minio_repository.get_moodle_objects()
return moodle_objects


@router.post(
"/batch-courses",
responses={200: {"description": "Success"}},
Expand Down
Empty file added src/repositories/__init__.py
Empty file.
Empty file.
37 changes: 37 additions & 0 deletions src/repositories/minio/repository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from typing import List, Dict
from minio import Minio
from minio.error import S3Error
from src.api.logging_ import logger
from src.storages.minio import minio_client as client


class MinioRepository:
def __init__(self, minio_client: Minio):
self.minio_client = minio_client

def get_moodle_objects(self) -> List[Dict[str, any]]:
try:
# List all objects with the "moodle/" prefix
objects = self.minio_client.list_objects(bucket_name="search", prefix="moodle/")
moodle_objects = []
for obj in objects:
# would it work like this?
parts = obj.object_name.split("/")
if len(parts) >= 3:
course_id = int(parts[1])
module_id = int(parts[2])
filename = parts[-1]
moodle_object = {
"course_id": course_id,
"module_id": module_id,
"filename": filename,
"minio_data": obj,
}
moodle_objects.append(moodle_object)
return moodle_objects
except S3Error as e:
logger.error(f"An error occurred while listing Moodle objects: {e}")
return []


minio_repository: MinioRepository = MinioRepository(client)

0 comments on commit 23bbcb0

Please sign in to comment.