diff --git a/LICENSE b/LICENSE index cbc871d..a6afae2 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2024 someone624 +Copyright (c) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/models/db.py b/src/models/db.py new file mode 100644 index 0000000..6379993 --- /dev/null +++ b/src/models/db.py @@ -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() diff --git a/src/models/migrations/env.py b/src/models/migrations/env.py new file mode 100644 index 0000000..829e8fe --- /dev/null +++ b/src/models/migrations/env.py @@ -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() \ No newline at end of file diff --git a/src/models/migrations/versions/xxxx_add_user_table.py b/src/models/migrations/versions/xxxx_add_user_table.py new file mode 100644 index 0000000..8dfe26b --- /dev/null +++ b/src/models/migrations/versions/xxxx_add_user_table.py @@ -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') \ No newline at end of file diff --git a/src/models/product_model.py b/src/models/product_model.py new file mode 100644 index 0000000..0ef6b9d --- /dev/null +++ b/src/models/product_model.py @@ -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"" + + def __init__(self, name, description, price): + self.name = name + self.description = description + self.price = price diff --git a/src/models/queries.py b/src/models/queries.py new file mode 100644 index 0000000..62681f1 --- /dev/null +++ b/src/models/queries.py @@ -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 \ No newline at end of file