Skip to content

Commit f9ee409

Browse files
committed
feat: database apps
1 parent 31ed357 commit f9ee409

49 files changed

Lines changed: 1331 additions & 355 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

example/database-app/.env.testing

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
APP_NAME="Masonite Testing"
22
APP_ENV=testing
33

4-
DB_HOST=localhost
5-
DB_DATABASE=postgres_testing
6-
DB_USER=postgres
7-
DB_PASSWORD=postgres
8-
DB_PORT=5432
4+
DB_HOST=127.0.0.1
5+
DB_DATABASE=database_app_test
6+
DB_USERNAME=app
7+
DB_PASSWORD=secret
8+
DB_PORT=3306
99

1010
LOG_CHANNEL=syslog
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from fastapi import HTTPException
2+
import hashlib
3+
4+
from app.models.user import User
5+
from app.models.profile import Profile
6+
from app.http.schemas.auth import StudentRegistrationRequest, TeacherRegistrationRequest
7+
8+
class AuthController:
9+
@staticmethod
10+
async def register_teacher(data: TeacherRegistrationRequest):
11+
# Check if user exists
12+
existing_user = await User.where("email", data.email).first()
13+
if existing_user:
14+
raise HTTPException(status_code=400, detail="Email already registered")
15+
16+
# Hash password
17+
hashed_password = hashlib.md5(data.password.encode()).hexdigest()
18+
19+
# Create user
20+
user = User()
21+
user.name = data.name
22+
user.email = data.email
23+
user.password = hashed_password
24+
user.role = "teacher"
25+
await user.save()
26+
27+
# Workaround for asyncpg insert bug in masoniteorm returning dict to primary key
28+
actual_user_id = user.id.get("id") if isinstance(user.id, dict) else user.id
29+
30+
# Create teacher profile
31+
profile = Profile()
32+
profile.user_id = actual_user_id
33+
profile.country = data.country
34+
profile.phone_number = data.phone_number
35+
profile.headline = data.headline
36+
profile.description = data.description
37+
profile.video_url = data.video_url
38+
profile.hourly_rate = data.hourly_rate
39+
import json
40+
profile.languages_spoken = json.dumps(data.languages_spoken)
41+
profile.subjects = json.dumps(data.subjects)
42+
await profile.save()
43+
44+
return {"message": "Teacher registered successfully", "user_id": actual_user_id}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from pydantic import BaseModel, EmailStr, Field
2+
3+
class StudentRegistrationRequest(BaseModel):
4+
name: str = Field(..., min_length=2, max_length=255)
5+
email: EmailStr
6+
password: str = Field(..., min_length=8)
7+
8+
class TeacherRegistrationRequest(StudentRegistrationRequest):
9+
country: str = Field(..., min_length=2)
10+
phone_number: str
11+
headline: str = Field(..., min_length=5, max_length=255)
12+
description: str = Field(..., min_length=50)
13+
video_url: str
14+
hourly_rate: int = Field(..., gt=0)
15+
languages_spoken: list[str]
16+
subjects: list[str]
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from .user import User
2-
from .post import Post
3-
from .tag import Tag
4-
from .media import Media
5-
from .post_tag import PostTag
2+
from .profile import Profile
3+
from .lesson import Lesson
4+
from .course import Course
5+
from .category import Category
6+
from .review import Review
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import TYPE_CHECKING
2+
3+
from fastapi_startkit.masoniteorm.models import Model
4+
from fastapi_startkit.masoniteorm.relationships import HasMany, HasManyThrough
5+
6+
if TYPE_CHECKING:
7+
from app.models.course import Course
8+
from app.models.lesson import Lesson
9+
10+
11+
class Category(Model):
12+
__table__ = "categories"
13+
14+
name: str
15+
description: str | None
16+
17+
courses = HasMany("Course")
18+
lessons = HasManyThrough(["Lesson", "Course"], "category_id", "course_id")
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import TYPE_CHECKING
2+
3+
from fastapi_startkit.masoniteorm.models import Model
4+
from fastapi_startkit.masoniteorm.relationships import BelongsTo, HasMany, BelongsToMany, MorphMany
5+
6+
if TYPE_CHECKING:
7+
from app.models.category import Category
8+
from app.models.lesson import Lesson
9+
from app.models.user import User
10+
from app.models.review import Review
11+
12+
13+
class Course(Model):
14+
__table__ = "courses"
15+
16+
title: str
17+
description: str | None
18+
price: int
19+
20+
category = BelongsTo("Category")
21+
lessons = HasMany("Lesson")
22+
students = BelongsToMany(
23+
"User",
24+
local_foreign_key="course_id",
25+
other_foreign_key="user_id",
26+
table="course_user",
27+
with_timestamps=True,
28+
with_fields=["progress", "completed_at"]
29+
)
30+
reviews = MorphMany("Review", "reviewable_type", "reviewable_id")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import TYPE_CHECKING
2+
3+
from fastapi_startkit.masoniteorm.models import Model
4+
from fastapi_startkit.masoniteorm.relationships import BelongsTo, MorphMany
5+
6+
if TYPE_CHECKING:
7+
from app.models.course import Course
8+
from app.models.review import Review
9+
10+
11+
class Lesson(Model):
12+
__table__ = "lessons"
13+
14+
title: str
15+
16+
course = BelongsTo("Course")
17+
reviews = MorphMany("Review", "reviewable_type", "reviewable_id")

example/database-app/app/models/media.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

example/database-app/app/models/post.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

example/database-app/app/models/post_tag.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)