Skip to content

Commit

Permalink
Merge branch 'feat/#64' of https://github.com/SVTeamJ/DeepBlue into c…
Browse files Browse the repository at this point in the history
…hore/#67
  • Loading branch information
kckoh committed Jan 19, 2023
2 parents 217ad13 + 3f95e6d commit 09c3009
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 1 deletion.
37 changes: 37 additions & 0 deletions backend/api/endpoints/fish.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from fastapi import APIRouter,Depends
from database import SessionLocal

from database import SessionLocal,engine,Base
from schemas import fish_schema
from crud import fish_crud
import model
from sqlalchemy.orm import Session
from model import fish
fish.Base.metadata.create_all(bind=engine)

def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()

router=APIRouter(
prefix="/api/fish",
)



@router.post("/",response_model=fish_schema.FishCreate)
def post_fish(fish:fish_schema.FishCreate,db:Session=Depends(get_db)):
fish_crud.create_fish(db,fish)
# return fish_crud.create_fish(db, fish=fish)



@router.get("/{fish_id}",response_model=fish_schema.Fish)
def get_fish(fish_id:int,db:Session=Depends(get_db)):
fish_information=db.query(fish.Fish).filter(fish.Fish.fish_id==fish_id).first()
return fish_information


18 changes: 18 additions & 0 deletions backend/crud/fish_crud.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from sqlalchemy.orm import Session
from schemas import fish_schema
from model.fish import Fish

def create_fish(db:Session,fish:fish_schema.FishCreate):
db_fish=Fish(
fish_id=fish.fish_id,
fish_type=fish.fish_type,
open_season=fish.open_season,
closed_season=fish.closed_season,
fish_url=fish.fish_url,
description=fish.description,
toxicity=fish.toxicity
)
db.add(db_fish)
db.commit()
db.refresh(db_fish)
return db_fish
3 changes: 2 additions & 1 deletion backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from database import engine

from api.api import api_router

from api.endpoints import fish
Base.metadata.create_all(bind=engine)

# cors
Expand All @@ -25,3 +25,4 @@
allow_headers=["*"],
)
app.include_router(api_router, prefix="/api")
app.include_router(fish.router)
23 changes: 23 additions & 0 deletions backend/model/fish.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from sqlalchemy import Boolean, Column,Integer, String
from sqlalchemy.types import TIMESTAMP,DateTime
from sqlalchemy.sql import text, func


from database import Base
from sqlalchemy import func

class Fish(Base):
__tablename__="fish"

fish_id=Column(Integer,primary_key=True,index=True)
fish_type=Column(String(64),nullable=False,index=True)
toxicity=Column(String(16),nullable=False,index=True)
open_season=Column(String(64),nullable=False,index=True)
closed_season=Column(String(64),nullable=False,index=True)
description=Column(String(255),nullable=False,index=True)
fish_url=Column(String(100),nullable=False,index=True)
created_at=Column(TIMESTAMP,server_default=func.now())
updated_at=Column(TIMESTAMP,server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"))
is_active=Column(Boolean,nullable=False,default=True)


36 changes: 36 additions & 0 deletions backend/schemas/fish_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from pydantic import BaseModel

class FishBase(BaseModel):


class Config:
orm_mode=True




class Fish(FishBase):
fish_id:int
description:str
toxicity:str
fish_type:str
open_season:str
closed_season:str
fish_url:str
is_active:bool



class FishCreate(FishBase):
fish_id:int
description:str
toxicity:str
fish_type:str
open_season:str
closed_season:str
fish_url:str

class FishRead(FishCreate):
fish_id:int


0 comments on commit 09c3009

Please sign in to comment.