Skip to content

Commit fa738cf

Browse files
orabeCopilot
andcommitted
Add backend scripts for certificate management and database seeding
Co-authored-by: Copilot <copilot@github.com>
1 parent ece4844 commit fa738cf

8 files changed

Lines changed: 117 additions & 2 deletions

File tree

backend/.dockerignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fly.toml
2+
.git/
3+
__pycache__/
4+
.envrc
5+
.venv/

backend/app/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from . import models, schemas, crud, database, security
88

99
app = FastAPI(title="MathCodeLab Certificate Verification API")
10+
models.Base.metadata.create_all(bind=database.engine)
1011

1112
# CORS
1213
ALLOWED_ORIGINS = os.getenv("ALLOWED_ORIGINS", "*").split(",")
@@ -24,6 +25,7 @@ def root():
2425
"service": "MathCodeLab Certificate Verification API",
2526
"status": "running",
2627
"health": "/health",
28+
"verify": "/verify/{certificate_id}",
2729
"docs": "/docs"
2830
}
2931

backend/render.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ services:
33
name: mathcodelab.github.io
44
env: python
55
buildCommand: "pip install -r requirements.txt"
6-
startCommand: "uvicorn app.main:app --host 0.0.0.0 --port 10000"
6+
startCommand: "uvicorn app.main:app --host 0.0.0.0 --port $PORT"
77
plan: free
88
envVars:
99
- key: DATABASE_URL

backend/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ uvicorn
33
sqlalchemy
44
pydantic
55
python-dotenv
6-
psycopg2
6+
psycopg2
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import sys
2+
import os
3+
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
4+
from app import database, models, crud, schemas
5+
from sqlalchemy.orm import Session
6+
7+
def main():
8+
db = next(database.get_db())
9+
print("Enter certificate details:")
10+
student_name = input("Student name: ")
11+
course_title = input("Course title: ")
12+
completion_date = input("Completion date (YYYY-MM-DD): ")
13+
duration_hours = int(input("Duration (hours): "))
14+
cert_in = schemas.CertificateCreate(
15+
student_name=student_name,
16+
course_title=course_title,
17+
completion_date=completion_date,
18+
duration_hours=duration_hours
19+
)
20+
cert = crud.create_certificate(db, cert_in)
21+
print(f"Created certificate: {cert.certificate_id}")
22+
23+
if __name__ == "__main__":
24+
main()

backend/scripts/seed_demo_data.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sys
2+
import os
3+
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
4+
from app import database, models, crud, schemas
5+
from sqlalchemy.orm import Session
6+
from datetime import datetime
7+
8+
def seed():
9+
db = next(database.get_db())
10+
# Valid certificate
11+
cert1 = schemas.CertificateCreate(
12+
student_name="Alice Example",
13+
course_title="Python Programming Basics",
14+
completion_date="2026-04-20",
15+
duration_hours=20
16+
)
17+
c1 = crud.create_certificate(db, cert1)
18+
# Revoke a certificate
19+
cert2 = schemas.CertificateCreate(
20+
student_name="Bob Revoked",
21+
course_title="Data Science Intro",
22+
completion_date="2026-03-15",
23+
duration_hours=15
24+
)
25+
c2 = crud.create_certificate(db, cert2)
26+
crud.revoke_certificate(db, c2.certificate_id, reason="Academic misconduct")
27+
print(f"Seeded: {c1.certificate_id} (valid), {c2.certificate_id} (revoked)")
28+
29+
if __name__ == "__main__":
30+
seed()

backend/seed_database.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from app.models import Base, Certificate
2+
from app.database import engine, SessionLocal
3+
from app.crud import create_certificate
4+
from app.schemas import CertificateCreate
5+
6+
# Create the database schema
7+
Base.metadata.create_all(bind=engine)
8+
9+
def seed_database():
10+
db = SessionLocal()
11+
try:
12+
certificates = [
13+
CertificateCreate(
14+
student_name="John Doe",
15+
course_title="Python Programming",
16+
completion_date="2026-04-01",
17+
duration_hours=40,
18+
issuer="MathCodeLab",
19+
instructor="Mohammad Orabe"
20+
),
21+
CertificateCreate(
22+
student_name="Jane Smith",
23+
course_title="Data Science Basics",
24+
completion_date="2026-03-15",
25+
duration_hours=30,
26+
issuer="MathCodeLab",
27+
instructor="Mohammad Orabe"
28+
),
29+
CertificateCreate(
30+
student_name="Alice Johnson",
31+
course_title="Advanced Java",
32+
completion_date="2026-02-20",
33+
duration_hours=50,
34+
issuer="MathCodeLab",
35+
instructor="Mohammad Orabe"
36+
)
37+
]
38+
39+
for cert in certificates:
40+
create_certificate(db, cert)
41+
42+
print("Database seeded successfully!")
43+
except Exception as e:
44+
print(f"An error occurred while seeding the database: {e}")
45+
finally:
46+
db.close()
47+
48+
if __name__ == "__main__":
49+
seed_database()

backend/view_db.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import sqlite3
2+
conn = sqlite3.connect("app.db")
3+
for row in conn.execute("SELECT * FROM certificates"):
4+
print(row)
5+
conn.close()

0 commit comments

Comments
 (0)