This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: '3.9' | |
services: | |
db: | |
container_name: mysql_database | |
image: mysql:latest | |
ports: | |
- "3306:3306" | |
environment: | |
MYSQL_ROOT_PASSWORD: password |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pull official base image | |
FROM python:3.10 | |
# set work directory | |
WORKDIR /app | |
# copy requirements file | |
COPY requirements.txt requirements.txt | |
# install dependencies |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# crud.py | |
from typing import List | |
from sqlalchemy.orm import Session | |
from exceptions import UserAlreadyExistError, UserNotFoundError | |
from models import User | |
from schemas import CreateAndUpdateUser | |
# Function to get list of users |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# api.py | |
from typing import List | |
from fastapi import APIRouter, Depends, HTTPException | |
from sqlalchemy.orm import Session | |
from crud import get_all_users, create_user, update_user_by_id, delete_user_by_id, get_user_by_id | |
from database import get_db | |
from exceptions import UserException | |
from schemas import Users, CreateAndUpdateUser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# main.py | |
from fastapi import FastAPI | |
import api | |
from crud import create_db | |
# initialize mysql database | |
create_db() | |
# initialize the app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# api.py | |
from typing import List | |
from fastapi import APIRouter, Depends, HTTPException | |
from sqlalchemy.orm import Session | |
from crud import get_all_users, create_user, update_user, delete_user, get_user_by_id | |
from database import get_db | |
from exceptions import UserException | |
from schemas import Users, CreateAndUpdateUser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# exceptions.py | |
class UserException(Exception): | |
... | |
class UserNotFoundError(UserException): | |
def __init__(self): | |
self.status_code = 404 | |
self.detail = "User Not Found" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# crud.py | |
from typing import List | |
from sqlalchemy.orm import Session | |
from exceptions import UserAlreadyExistError, UserNotFoundError | |
from models import User | |
from schemas import CreateAndUpdateUser | |
# Function to get list of users |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# schemas.py | |
from pydantic import BaseModel | |
# To support creation and update APIs | |
class CreateAndUpdateUser(BaseModel): | |
name: str | |
email: str | |
password: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# models.py | |
from sqlalchemy.schema import Column | |
from sqlalchemy.types import String, Integer | |
from database import Base | |
class User(Base): | |
__tablename__ = "users" | |
id = Column(Integer, primary_key=True, index=True, autoincrement=True) |
NewerOlder