|
|
|
@ -479,4 +479,91 @@ def my_courses(): |
|
|
|
|
'description': item.category.description |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
return jsonify(course_list), 200 |
|
|
|
|
return jsonify(course_list), 200 |
|
|
|
|
|
|
|
|
|
@course.route('/seed', methods=['POST']) |
|
|
|
|
def seed_categories(): |
|
|
|
|
"""Seed the database with 10 predefined categories.""" |
|
|
|
|
categories = [ |
|
|
|
|
{"name": "Programming", "description": "Learn coding and software development.", "isActive": True}, |
|
|
|
|
{"name": "History", "description": "Explore historical events and figures.", "isActive": True}, |
|
|
|
|
{"name": "Mathematics", "description": "Understand mathematical concepts and theories.", "isActive": True}, |
|
|
|
|
{"name": "Science", "description": "Dive into the world of science and discovery.", "isActive": True}, |
|
|
|
|
{"name": "Art", "description": "Appreciate and create beautiful works of art.", "isActive": True}, |
|
|
|
|
{"name": "Music", "description": "Discover music theory and practice.", "isActive": True}, |
|
|
|
|
{"name": "Sports", "description": "Learn about various sports and fitness activities.", "isActive": True}, |
|
|
|
|
{"name": "Health", "description": "Focus on wellness and healthy living.", "isActive": True}, |
|
|
|
|
{"name": "Technology", "description": "Stay updated with the latest tech trends.", "isActive": True}, |
|
|
|
|
{"name": "Business", "description": "Understand business strategies and management.", "isActive": True}, |
|
|
|
|
] |
|
|
|
|
|
|
|
|
|
# Insert categories into the database |
|
|
|
|
try: |
|
|
|
|
for category in categories: |
|
|
|
|
new_category = Category( |
|
|
|
|
name=category['name'], |
|
|
|
|
description=category['description'], |
|
|
|
|
isActive=category['isActive'], |
|
|
|
|
courses=[] |
|
|
|
|
) |
|
|
|
|
db.session.add(new_category) |
|
|
|
|
db.session.commit() |
|
|
|
|
return jsonify({"message": "Categories seeded successfully."}), 201 |
|
|
|
|
except IntegrityError: |
|
|
|
|
db.session.rollback() |
|
|
|
|
return jsonify({"message": "Some categories already exist."}), 400 |
|
|
|
|
except Exception as e: |
|
|
|
|
return jsonify({"message": f"An error occurred: {str(e)}"}), 500 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@course.route('/seed_courses', methods=['POST']) |
|
|
|
|
def seed_courses(): |
|
|
|
|
"""Seed the database with courses for existing categories.""" |
|
|
|
|
try: |
|
|
|
|
# Fetch all categories from the database |
|
|
|
|
categories = Category.query.all() |
|
|
|
|
if not categories: |
|
|
|
|
return jsonify({"message": "No categories found. Please seed categories first."}), 400 |
|
|
|
|
|
|
|
|
|
# Predefined courses with associated category names |
|
|
|
|
courses = [ |
|
|
|
|
{"name": "Python Programming", "category": "Programming", "description": "Learn Python from scratch."}, |
|
|
|
|
{"name": "World History", "category": "History", "description": "Explore key historical events."}, |
|
|
|
|
{"name": "Algebra Basics", "category": "Mathematics", "description": "Understand fundamental algebra concepts."}, |
|
|
|
|
{"name": "Physics 101", "category": "Science", "description": "Dive into the principles of physics."}, |
|
|
|
|
{"name": "Digital Painting", "category": "Art", "description": "Learn the basics of digital art."}, |
|
|
|
|
{"name": "Guitar for Beginners", "category": "Music", "description": "Master the basics of playing guitar."}, |
|
|
|
|
{"name": "Fitness Fundamentals", "category": "Sports", "description": "Achieve your fitness goals."}, |
|
|
|
|
{"name": "Healthy Living", "category": "Health", "description": "Tips for a healthier lifestyle."}, |
|
|
|
|
{"name": "AI Basics", "category": "Technology", "description": "Introduction to Artificial Intelligence."}, |
|
|
|
|
{"name": "Business Strategies", "category": "Business", "description": "Learn how to develop business strategies."}, |
|
|
|
|
] |
|
|
|
|
|
|
|
|
|
# Create and add courses to the database |
|
|
|
|
for course_data in courses: |
|
|
|
|
# Find the category by name |
|
|
|
|
category = next((cat for cat in categories if cat.name == course_data["category"]), None) |
|
|
|
|
if not category: |
|
|
|
|
continue # Skip if the category doesn't exist |
|
|
|
|
|
|
|
|
|
# Create a new course |
|
|
|
|
new_course = Course( |
|
|
|
|
name=course_data["name"], |
|
|
|
|
categoryID=category.id, |
|
|
|
|
description=course_data["description"], |
|
|
|
|
authorID=None, # Set an appropriate author ID here |
|
|
|
|
totalPages=10, # Example value |
|
|
|
|
totalEnrolled=0, |
|
|
|
|
isActive=True, |
|
|
|
|
publishedStatus=1, |
|
|
|
|
enrollments=[], |
|
|
|
|
quizzes=[], |
|
|
|
|
chats=[] |
|
|
|
|
) |
|
|
|
|
db.session.add(new_course) |
|
|
|
|
|
|
|
|
|
db.session.commit() |
|
|
|
|
return jsonify({"message": "Courses seeded successfully."}), 201 |
|
|
|
|
except Exception as e: |
|
|
|
|
db.session.rollback() |
|
|
|
|
return jsonify({"message": f"An error occurred: {str(e)}"}), 500 |
|
|
|
|