From 2299be0cb7b14333f243b2a287d710624a04fd86 Mon Sep 17 00:00:00 2001 From: slepp22 <102992553+slepp22@users.noreply.github.com> Date: Sat, 27 Apr 2024 14:17:25 +0200 Subject: [PATCH] Get User implemented --- README.md | 8 ++++ .../dd5d09078881_updated_user_model.py | 38 +++++++++++++++++++ app/api/endpoints/user.py | 25 ++++++++---- app/api/routers.py | 5 ++- app/models/user.py | 7 +++- db/config.py | 1 - 6 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 alembic/versions/dd5d09078881_updated_user_model.py diff --git a/README.md b/README.md index 890ab51..4f24f00 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,15 @@ docker tag sleppp/inventory-backend gcr.io/inventory-database-420916/inventory-b ```bash docker push gcr.io/inventory-database-420916/inventory-backend:latest ``` +## SQL Scripts for Test Data +```sql +INSERT INTO users (firstname, lastname, email, password, role) +VALUES ('Firstname1', 'Lastname1', 'user1@example.com', 'password1', 'student'); +INSERT INTO users (firstname, lastname, email, password, role) +VALUES ('Firstname2', 'Lastname2', 'user2@example.com', 'password2', 'admin'); + +``` If you encounter any errors during the deployment process, make sure you are authenticated with Google Cloud CLI (gcloud auth login) and have the necessary permissions to push images to Google Container Registry. For troubleshooting tips or common errors, refer to the Google Cloud Run documentation. diff --git a/alembic/versions/dd5d09078881_updated_user_model.py b/alembic/versions/dd5d09078881_updated_user_model.py new file mode 100644 index 0000000..3f8d6ef --- /dev/null +++ b/alembic/versions/dd5d09078881_updated_user_model.py @@ -0,0 +1,38 @@ +"""updated user model + +Revision ID: dd5d09078881 +Revises: 8e8c4627a4df +Create Date: 2024-04-27 13:50:02.573313 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = 'dd5d09078881' +down_revision: Union[str, None] = '8e8c4627a4df' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('users', sa.Column('firstname', sa.String(), nullable=True)) + op.add_column('users', sa.Column('lastname', sa.String(), nullable=True)) + op.add_column('users', sa.Column('password', sa.String(), nullable=True)) + op.add_column('users', sa.Column('role', sa.String(), nullable=True)) + op.drop_column('users', 'name') + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('users', sa.Column('name', sa.VARCHAR(), autoincrement=False, nullable=True)) + op.drop_column('users', 'role') + op.drop_column('users', 'password') + op.drop_column('users', 'lastname') + op.drop_column('users', 'firstname') + # ### end Alembic commands ### diff --git a/app/api/endpoints/user.py b/app/api/endpoints/user.py index 134d703..ef5ef60 100644 --- a/app/api/endpoints/user.py +++ b/app/api/endpoints/user.py @@ -1,11 +1,20 @@ -from sqlalchemy import Column, Integer, String -from sqlalchemy.ext.declarative import declarative_base +from fastapi import APIRouter, Depends, HTTPException +from sqlalchemy.orm import Session +from db.config import SessionLocal +from app.models import user as user_model -Base = declarative_base() +router = APIRouter() -class User(Base): - __tablename__ = 'users' +def get_db(): + db = SessionLocal() + try: + yield db + finally: + db.close() - id = Column(Integer, primary_key=True) - name = Column(String) - email = Column(String, unique=True) +@router.get("/users/{user_id}") +def get_user_by_id(user_id: int, db: Session = Depends(get_db)): + user = db.query(user_model.User).filter(user_model.User.id == user_id).first() + if user is None: + raise HTTPException(status_code=404, detail="User not found") + return user diff --git a/app/api/routers.py b/app/api/routers.py index ac40ea0..238afe5 100644 --- a/app/api/routers.py +++ b/app/api/routers.py @@ -1,6 +1,7 @@ import random from fastapi import APIRouter +from app.api.endpoints import user router = APIRouter() @@ -11,4 +12,6 @@ async def read_root(): @router.get("/pin") async def generate_pin(): pin = random.randint(1000, 9999) - return {pin} \ No newline at end of file + return {pin} + +router.include_router(user.router, tags=["users"]) diff --git a/app/models/user.py b/app/models/user.py index 146cf69..f83bc48 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -5,5 +5,8 @@ class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) - name = Column(String) - email = Column(String) \ No newline at end of file + firstname = Column(String) + lastname = Column(String) + email = Column(String) + password = Column(String) + role = Column(String) \ No newline at end of file diff --git a/db/config.py b/db/config.py index b9bdfb2..7376ff1 100644 --- a/db/config.py +++ b/db/config.py @@ -17,6 +17,5 @@ def get_database_url(): return DB_URL engine = create_engine(DB_URL) - SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)