Skip to content

Commit

Permalink
Summary (required)
Browse files Browse the repository at this point in the history
  • Loading branch information
someone624 committed Nov 22, 2024
1 parent c7ab772 commit 871c905
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 1 deletion.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 someone624
Copyright (c) <year> <person>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
25 changes: 25 additions & 0 deletions src/models/db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# db.py
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from .models import Base

# Database URL (adjust accordingly)
DATABASE_URL = 'postgresql://username:password@localhost/dbname'

# Create engine
engine = create_engine(DATABASE_URL, echo=True)

# Create a session
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

# Create tables in the database
def init_db():
Base.metadata.create_all(bind=engine)

# Dependency to get DB session
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
32 changes: 32 additions & 0 deletions src/models/migrations/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# env.py (Generated by Alembic)
from __future__ import with_statement
from alembic import context
from sqlalchemy import engine_from_config, pool
from logging.config import fileConfig
import os
import sys

# Add model path to sys.path for access
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../src')))

from models import Base # Ensure your models are imported here

# Configuring Alembic context
config = context.config
fileConfig(config.config_file_name)

def run_migrations_online():
connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix='sqlalchemy.',
poolclass=pool.NullPool,
)

with connectable.connect() as connection:
context.configure(connection=connection)

with context.begin_transaction():
context.run_migrations()

if __name__ == '__main__':
run_migrations_online()
19 changes: 19 additions & 0 deletions src/models/migrations/versions/xxxx_add_user_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# xxxx_add_user_table.py (Generated by Alembic)
from alembic import op
import sqlalchemy as sa

def upgrade():
op.create_table(
'users',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('username', sa.String(length=100), nullable=False),
sa.Column('email', sa.String(length=100), nullable=False),
sa.Column('password_hash', sa.String(length=255), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('email'),
sa.UniqueConstraint('username')
)

def downgrade():
op.drop_table('users')
22 changes: 22 additions & 0 deletions src/models/product_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from sqlalchemy import Column, Integer, String, Float, DateTime
from sqlalchemy.ext.declarative import declarative_base
import datetime

Base = declarative_base()

class Product(Base):
__tablename__ = 'products'

id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(100), nullable=False)
description = Column(String(255))
price = Column(Float, nullable=False)
created_at = Column(DateTime, default=datetime.datetime.utcnow)

def __repr__(self):
return f"<Product(id={self.id}, name={self.name}, price={self.price})>"

def __init__(self, name, description, price):
self.name = name
self.description = description
self.price = price
23 changes: 23 additions & 0 deletions src/models/queries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from sqlalchemy.orm import Session
from .user_model import User
from .product_model import Product

def get_user_by_id(db: Session, user_id: int):
return db.query(User).filter(User.id == user_id).first()

def get_all_products(db: Session):
return db.query(Product).all()

def create_user(db: Session, username: str, email: str, password_hash: str):
db_user = User(username=username, email=email, password_hash=password_hash)
db.add(db_user)
db.commit()
db.refresh(db_user)
return db_user

def create_product(db: Session, name: str, description: str, price: float):
db_product = Product(name=name, description=description, price=price)
db.add(db_product)
db.commit()
db.refresh(db_product)
return db_product

0 comments on commit 871c905

Please sign in to comment.