Skip to content

Commit

Permalink
Merge pull request #10 from slepp22/user-api
Browse files Browse the repository at this point in the history
Get User implemented
  • Loading branch information
slepp22 authored Apr 27, 2024
2 parents 50bac16 + 2299be0 commit 4bfeca7
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 12 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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', '[email protected]', 'password1', 'student');

INSERT INTO users (firstname, lastname, email, password, role)
VALUES ('Firstname2', 'Lastname2', '[email protected]', '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.
Expand Down
38 changes: 38 additions & 0 deletions alembic/versions/dd5d09078881_updated_user_model.py
Original file line number Diff line number Diff line change
@@ -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 ###
25 changes: 17 additions & 8 deletions app/api/endpoints/user.py
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion app/api/routers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import random

from fastapi import APIRouter
from app.api.endpoints import user

router = APIRouter()

Expand All @@ -11,4 +12,6 @@ async def read_root():
@router.get("/pin")
async def generate_pin():
pin = random.randint(1000, 9999)
return {pin}
return {pin}

router.include_router(user.router, tags=["users"])
7 changes: 5 additions & 2 deletions app/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ class User(Base):
__tablename__ = 'users'

id = Column(Integer, primary_key=True)
name = Column(String)
email = Column(String)
firstname = Column(String)
lastname = Column(String)
email = Column(String)
password = Column(String)
role = Column(String)
1 change: 0 additions & 1 deletion db/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ def get_database_url():
return DB_URL

engine = create_engine(DB_URL)

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

0 comments on commit 4bfeca7

Please sign in to comment.