Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions example/database-app/app/http/controllers/auth_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from app.models.profile import Profile
from app.http.schemas.auth import StudentRegistrationRequest, TeacherRegistrationRequest


class AuthController:
@staticmethod
async def register_teacher(data: TeacherRegistrationRequest):
Expand Down Expand Up @@ -37,6 +38,7 @@ async def register_teacher(data: TeacherRegistrationRequest):
profile.video_url = data.video_url
profile.hourly_rate = data.hourly_rate
import json

profile.languages_spoken = json.dumps(data.languages_spoken)
profile.subjects = json.dumps(data.subjects)
await profile.save()
Expand Down
2 changes: 2 additions & 0 deletions example/database-app/app/http/schemas/auth.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from pydantic import BaseModel, EmailStr, Field


class StudentRegistrationRequest(BaseModel):
name: str = Field(..., min_length=2, max_length=255)
email: EmailStr
password: str = Field(..., min_length=8)


class TeacherRegistrationRequest(StudentRegistrationRequest):
country: str = Field(..., min_length=2)
phone_number: str
Expand Down
15 changes: 10 additions & 5 deletions example/database-app/app/models/course.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from typing import TYPE_CHECKING

from fastapi_startkit.masoniteorm.models import Model
from fastapi_startkit.masoniteorm.relationships import BelongsTo, HasMany, BelongsToMany, MorphMany
from fastapi_startkit.masoniteorm.relationships import (
BelongsTo,
HasMany,
BelongsToMany,
MorphMany,
)

if TYPE_CHECKING:
from app.models.category import Category
Expand All @@ -20,11 +25,11 @@ class Course(Model):
category = BelongsTo("Category")
lessons = HasMany("Lesson")
students = BelongsToMany(
"User",
local_foreign_key="course_id",
other_foreign_key="user_id",
"User",
local_foreign_key="course_id",
other_foreign_key="user_id",
table="course_user",
with_timestamps=True,
with_fields=["progress", "completed_at"]
with_fields=["progress", "completed_at"],
)
reviews = MorphMany("Review", "reviewable_type", "reviewable_id")
5 changes: 4 additions & 1 deletion example/database-app/app/models/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
from fastapi_startkit.masoniteorm.models import Model
from fastapi_startkit.masoniteorm.relationships import MorphTo


class Review(Model):
__table__ = "reviews"

reviewable_type: str
content: str

reviewable = MorphTo("Review", morph_key="reviewable_type", morph_id="reviewable_id")
reviewable = MorphTo(
"Review", morph_key="reviewable_type", morph_id="reviewable_id"
)
8 changes: 4 additions & 4 deletions example/database-app/app/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class User(Model):

profile = HasOne("Profile")
courses = BelongsToMany(
"Course",
local_foreign_key="user_id",
other_foreign_key="course_id",
"Course",
local_foreign_key="user_id",
other_foreign_key="course_id",
table="course_user",
with_timestamps=True,
with_fields=["progress", "completed_at"]
with_fields=["progress", "completed_at"],
)
14 changes: 8 additions & 6 deletions example/database-app/app/students/controllers/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ async def register(request: StudentRegistrationRequest):
existing_user = await User.where("email", request.email).first()
if existing_user:
raise RequestValidationError(
errors=[{
"loc": ("body", "email"),
"msg": "Email already registered",
"type": "value_error",
"input": request.email,
}]
errors=[
{
"loc": ("body", "email"),
"msg": "Email already registered",
"type": "value_error",
"input": request.email,
}
]
)

password = hashlib.md5(request.password.encode()).hexdigest()
Expand Down
1 change: 1 addition & 0 deletions example/database-app/bootstrap/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class AppExceptionHandler(ExceptionHandler):
def register(self):
pass


app: Application[AppConfig] = Application(
base_path=Path(__file__).parent.parent,
config=AppConfig,
Expand Down
1 change: 1 addition & 0 deletions example/database-app/config/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from config.database import DatabaseConfig
from dataclasses import field


@dataclass
class AppConfig(BaseConfig):
database: DatabaseConfig = field(default_factory=DatabaseConfig)
34 changes: 17 additions & 17 deletions example/database-app/config/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
class DatabaseConfig:
default: str = field(default_factory=lambda: env("DB_CONNECTION", "mysql"))

connections: dict[str, Dict[str, Any]] = field(default_factory=lambda: {
"sqlite": SQLiteConfig(
driver="sqlite",
database=env("DB_DATABASE", "database.sqlite"),
),
"mysql": MySQLConfig(
driver="mysql",
host=env("DB_HOST", "127.0.0.1"),
database=env("DB_DATABASE", "laravel"),
username=env("DB_USERNAME", "root"),
password=env("DB_PASSWORD", ""),
port=env("DB_PORT", "3306"),
options={
"charset": "utf8mb4"
}
),
})
connections: dict[str, Dict[str, Any]] = field(
default_factory=lambda: {
"sqlite": SQLiteConfig(
driver="sqlite",
database=env("DB_DATABASE", "database.sqlite"),
),
"mysql": MySQLConfig(
driver="mysql",
host=env("DB_HOST", "127.0.0.1"),
database=env("DB_DATABASE", "laravel"),
username=env("DB_USERNAME", "root"),
password=env("DB_PASSWORD", ""),
port=env("DB_PORT", "3306"),
options={"charset": "utf8mb4"},
),
}
)
29 changes: 15 additions & 14 deletions example/database-app/config/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@

@dataclasses.dataclass
class LoggingConfig:
default: str = dataclasses.field(default_factory=lambda: env('LOG_CHANNEL', 'terminal'))
default: str = dataclasses.field(
default_factory=lambda: env("LOG_CHANNEL", "terminal")
)

channels: dict = dataclasses.field(default_factory=lambda: {
'stack': StackChannel(
driver='stack',
channels=['daily', 'terminal']
),
'daily': DailyChannel(
level=env('LOG_DAILY_LEVEL', 'info'),
path=env('LOG_DAILY_PATH', 'storage/logs'),
),
'terminal': TerminalChannel(
level=env('LOG_TERMINAL_LEVEL', 'info'),
),
})
channels: dict = dataclasses.field(
default_factory=lambda: {
"stack": StackChannel(driver="stack", channels=["daily", "terminal"]),
"daily": DailyChannel(
level=env("LOG_DAILY_LEVEL", "info"),
path=env("LOG_DAILY_PATH", "storage/logs"),
),
"terminal": TerminalChannel(
level=env("LOG_TERMINAL_LEVEL", "info"),
),
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ async def up(self):
table.increments("id")
table.string("title")
table.integer("instructor_id").unsigned()
table.foreign("instructor_id").references("id").on("users").on_delete("cascade")
table.foreign("instructor_id").references("id").on("users").on_delete(
"cascade"
)
table.integer("category_id").unsigned().nullable()
table.foreign("category_id").references("id").on("categories").on_delete("set null")
table.foreign("category_id").references("id").on("categories").on_delete(
"set null"
)
table.timestamps()

async def down(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ async def up(self):
table.integer("user_id").unsigned()
table.foreign("user_id").references("id").on("users").on_delete("cascade")
table.integer("course_id").unsigned()
table.foreign("course_id").references("id").on("courses").on_delete("cascade")
table.foreign("course_id").references("id").on("courses").on_delete(
"cascade"
)
table.integer("progress").default(0)
table.timestamp("completed_at").nullable()
table.timestamps()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ async def up(self):
async with await self.schema.create("lessons") as table:
table.increments("id")
table.integer("course_id").unsigned()
table.foreign("course_id").references("id").on("courses").on_delete("cascade")
table.foreign("course_id").references("id").on("courses").on_delete(
"cascade"
)
table.string("title")
table.timestamps()

Expand Down
2 changes: 1 addition & 1 deletion example/database-app/databases/seeds/category_seeder.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ async def run(self):
"Business",
]
for name in categories:
await Category.first_or_create({"name": name})
await Category.first_or_create({"name": name})
4 changes: 1 addition & 3 deletions example/database-app/databases/seeds/course_seeder.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,4 @@ async def run(self):
for data in courses:
course, _ = await Course.first_or_create({"title": data["title"]}, data)
for title in lesson_map[data["title"]]:
await Lesson.first_or_create(
{"title": title, "course_id": course.id}
)
await Lesson.first_or_create({"title": title, "course_id": course.id})
2 changes: 1 addition & 1 deletion example/database-app/databases/seeds/database_seeder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ async def run(self):
await self.call(CategorySeeder)
await self.call(UserSeeder)
await self.call(CourseSeeder)
await self.call(ReviewSeeder)
await self.call(ReviewSeeder)
24 changes: 14 additions & 10 deletions example/database-app/databases/seeds/post_seeder.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,26 @@ async def run(self):
tag_database = await Tag.first_or_create({"name": "database"})

# Create First Blog Post
post1 = await Post.create({
"user_id": user.id,
"title": "Laravel and Databases",
"content": "This is a post about Laravel framework and its database capabilities."
})
post1 = await Post.create(
{
"user_id": user.id,
"title": "Laravel and Databases",
"content": "This is a post about Laravel framework and its database capabilities.",
}
)

# Attach tags to first post
await PostTag.first_or_create({"post_id": post1.id, "tag_id": tag_laravel.id})
await PostTag.first_or_create({"post_id": post1.id, "tag_id": tag_database.id})

# Create Second Blog Post
post2 = await Post.create({
"user_id": user.id,
"title": "FastAPI and Databases",
"content": "This is a post about FastAPI performance and database handling."
})
post2 = await Post.create(
{
"user_id": user.id,
"title": "FastAPI and Databases",
"content": "This is a post about FastAPI performance and database handling.",
}
)

# Attach tags to second post
await PostTag.first_or_create({"post_id": post2.id, "tag_id": tag_fastapi.id})
Expand Down
8 changes: 6 additions & 2 deletions example/database-app/databases/seeds/review_seeder.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,9 @@ async def run(self):
contents = reviews_by_title.get(course.title, [])
for content in contents:
await Review.first_or_create(
{"reviewable_type": "courses", "reviewable_id": course.id, "content": content}
)
{
"reviewable_type": "courses",
"reviewable_id": course.id,
"content": content,
}
)
6 changes: 4 additions & 2 deletions example/database-app/databases/seeds/user_seeder.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,7 @@ async def run(self):

for data in users:
profile_data = data.pop("profile")
user= await User.first_or_create({"email": data["email"]}, data)
await Profile.first_or_create({"user_id": user.id}, {"user_id": user.id, **profile_data})
user = await User.first_or_create({"email": data["email"]}, data)
await Profile.first_or_create(
{"user_id": user.id}, {"user_id": user.id, **profile_data}
)
1 change: 1 addition & 0 deletions example/database-app/providers/console_provider.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from fastapi_startkit.providers import Provider


class ConsoleProvider(Provider):
def register(self) -> None:
pass
1 change: 1 addition & 0 deletions example/database-app/providers/fastapi_provider.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from fastapi_startkit.fastapi import FastAPIProvider


class FastAPIServiceProvider(FastAPIProvider):
def boot(self) -> None:
super().boot()
Expand Down
Loading
Loading