Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/webapi/crud.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from sqlalchemy.orm import Session
from . import models, schemas


def get_bot(db: Session, user_id: int):
return db.query(models.DiscordBot).filter(models.DiscordBot.user_id == user_id).first()


def get_bots(db: Session, skip: int = 0, limit: int = 5):
return db.query(models.DiscordBot).offset(skip).limit(limit).all()


def create_user(db: Session, user: schemas.DiscordBotCreate):
fake_hashed_password = user.password + "notreallyhashed"
db_user = models.User(email=user.email, hashed_password=fake_hashed_password)
db.add(db_user)
db.commit()
db.refresh(db_user)
return db_user

def create_user(db: Session, user: schemas.UserCreate):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

F811: redefinition of unused 'create_user' from line 13

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E302: expected 2 blank lines, found 1

fake_hashed_password = user.password + "notreallyhashed"
db_user = models.DiscordBot(email=user.email, hashed_password=fake_hashed_password)
db.add(db_user)
db.commit()
db.refresh(db_user)
return db_user


def get_items(db: Session, skip: int = 0, limit: int = 100):
return db.query(models.Item).offset(skip).limit(limit).all()


def create_user_item(db: Session, item: schemas.ItemCreate, user_id: int):
db_item = models.Item(**item.dict(), owner_id=user_id)
db.add(db_item)
db.commit()
db.refresh(db_item)
return db_item
11 changes: 11 additions & 0 deletions src/webapi/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import os

DATABASE_URL = os.environ.get('DATABASE_URL')

engine = create_engine(DATABASE_URL, connect_args={'check_same_thread': False})
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()
10 changes: 3 additions & 7 deletions src/webapi/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
from fastapi import FastAPI, status
from pydantic import BaseModel
from starlette.responses import RedirectResponse


class Bot(BaseModel):
name: str
from .schemas import DiscordBot


app = FastAPI(
Expand All @@ -24,12 +20,12 @@ def get_bot(id: int):


@app.post('/api/bots', status_code=status.HTTP_201_CREATED)
def post_bots(bot: Bot):
def post_bots(bot: DiscordBot):
return {'message': 'Hello World'}


@app.put('/api/bots/{id}', status_code=status.HTTP_200_OK)
def put_bots(id: int, bot: Bot):
def put_bots(id: int, bot: DiscordBot):
return {'message': 'Hello World'}


Expand Down
11 changes: 11 additions & 0 deletions src/webapi/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from sqlalchemy import Column, Integer, String
from .database import Base


class DiscordBot(Base):
__tablename__ = 'discord_bots'

user_id = Column(
Integer, primary_key=True, autoincrement=False, unique=True, index=True
)
name = Column(String)
15 changes: 15 additions & 0 deletions src/webapi/schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from pydantic import BaseModel


class DiscordBotBase(BaseModel):
user_id: int
name: str


class DiscordBotCreate(DiscordBotBase):
pass


class DiscordBot(DiscordBotBase):
class Config:
orm_mode = True