|
| 1 | +from typing import Dict, List, Optional |
| 2 | +from models.tutorial_models import Tutorial, TutorialStep, Quiz, QuizOption |
| 3 | + |
| 4 | +# Mock database of tutorials |
| 5 | +TUTORIALS_DB: Dict[str, Tutorial] = { |
| 6 | + "sorting": Tutorial( |
| 7 | + id="sorting", |
| 8 | + title="Sorting Algorithms Mastery", |
| 9 | + description="Learn about different sorting algorithms and their implementations", |
| 10 | + difficulty="Beginner", |
| 11 | + estimatedTime="30 minutes", |
| 12 | + prerequisites=["basic-arrays"], |
| 13 | + category="sorting", |
| 14 | + icon="🔄", |
| 15 | + steps=[ |
| 16 | + TutorialStep( |
| 17 | + id="intro", |
| 18 | + type="explanation", |
| 19 | + title="Introduction to Sorting", |
| 20 | + content="Sorting is a fundamental operation in computer science...", |
| 21 | + interactive=True |
| 22 | + ), |
| 23 | + TutorialStep( |
| 24 | + id="bubble-sort", |
| 25 | + type="explanation", |
| 26 | + title="Bubble Sort Algorithm", |
| 27 | + content="Bubble Sort is the simplest sorting algorithm...", |
| 28 | + codeExample="""def bubble_sort(arr): |
| 29 | + n = len(arr) |
| 30 | + for i in range(n): |
| 31 | + for j in range(0, n-i-1): |
| 32 | + if arr[j] > arr[j+1]: |
| 33 | + arr[j], arr[j+1] = arr[j+1], arr[j] |
| 34 | + return arr""", |
| 35 | + interactive=True, |
| 36 | + quiz=Quiz( |
| 37 | + question="What is the time complexity of Bubble Sort?", |
| 38 | + options=[ |
| 39 | + QuizOption(text="O(n)", isCorrect=False), |
| 40 | + QuizOption(text="O(n²)", isCorrect=True), |
| 41 | + QuizOption(text="O(n log n)", isCorrect=False), |
| 42 | + QuizOption(text="O(1)", isCorrect=False) |
| 43 | + ], |
| 44 | + explanation="Bubble Sort uses nested loops, leading to O(n²) time complexity" |
| 45 | + ) |
| 46 | + ) |
| 47 | + ] |
| 48 | + ), |
| 49 | + "graph": Tutorial( |
| 50 | + id="graph", |
| 51 | + title="Graph Algorithms", |
| 52 | + description="Master graph traversal and pathfinding algorithms", |
| 53 | + difficulty="Intermediate", |
| 54 | + estimatedTime="45 minutes", |
| 55 | + prerequisites=["basic-arrays", "sorting"], |
| 56 | + category="graph", |
| 57 | + icon="🕸️", |
| 58 | + steps=[ |
| 59 | + TutorialStep( |
| 60 | + id="intro", |
| 61 | + type="explanation", |
| 62 | + title="Introduction to Graphs", |
| 63 | + content="Graphs are versatile data structures...", |
| 64 | + interactive=True |
| 65 | + ) |
| 66 | + ] |
| 67 | + ) |
| 68 | +} |
| 69 | + |
| 70 | +class TutorialService: |
| 71 | + @staticmethod |
| 72 | + def get_all_tutorials() -> List[Tutorial]: |
| 73 | + """Get all available tutorials.""" |
| 74 | + return list(TUTORIALS_DB.values()) |
| 75 | + |
| 76 | + @staticmethod |
| 77 | + def get_tutorial_by_id(tutorial_id: str) -> Optional[Tutorial]: |
| 78 | + """Get a specific tutorial by ID.""" |
| 79 | + return TUTORIALS_DB.get(tutorial_id) |
| 80 | + |
| 81 | + @staticmethod |
| 82 | + def get_tutorials_by_difficulty(difficulty: str) -> List[Tutorial]: |
| 83 | + """Get tutorials filtered by difficulty level.""" |
| 84 | + return [ |
| 85 | + tutorial for tutorial in TUTORIALS_DB.values() |
| 86 | + if tutorial.difficulty.lower() == difficulty.lower() |
| 87 | + ] |
| 88 | + |
| 89 | + @staticmethod |
| 90 | + def get_tutorials_by_category(category: str) -> List[Tutorial]: |
| 91 | + """Get tutorials filtered by category.""" |
| 92 | + return [ |
| 93 | + tutorial for tutorial in TUTORIALS_DB.values() |
| 94 | + if tutorial.category.lower() == category.lower() |
| 95 | + ] |
0 commit comments