Skip to content

Commit

Permalink
Merge pull request #24 from MPMG-DCC-UFMG/feature/new-many-to-many-re…
Browse files Browse the repository at this point in the history
…lations

Feature/new many to many relations
  • Loading branch information
walterjgsp authored Nov 16, 2020
2 parents 349b7ae + dfa2a28 commit eeb0247
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 25 deletions.
10 changes: 10 additions & 0 deletions app/src/application/associations/database/association_tw_ws.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from application.core.database import Base
from sqlalchemy import Column, Integer, ForeignKey


class AssociationTWWSDB(Base):
__tablename__ = "association_tw_ws"
__versioned__ = {}

type_work_flag = Column(Integer, ForeignKey("typework.flag"), primary_key=True)
work_status_flag = Column(Integer, ForeignKey("workstatus.flag"), primary_key=True)
5 changes: 5 additions & 0 deletions app/src/application/associations/database/repository.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import List

from application.associations.database.association_tw_ws import AssociationTWWSDB
from sqlalchemy import desc
from sqlalchemy.orm import Session
from sqlalchemy_continuum.utils import version_class
Expand All @@ -16,6 +17,10 @@ def get_all_associations(db: Session) -> List[AssociationTypePhPW]:
return db.query(AssociationTypePhPWDB).all()


def get_all_work_status_associations(db: Session) -> List[AssociationTWWSDB]:
return db.query(AssociationTWWSDB).all()


def get_table_version(db: Session) -> int:
version = version_class(AssociationTypePhPWDB)
last_changed = db.query(version).order_by(desc(version.transaction_id)).limit(1)
Expand Down
8 changes: 8 additions & 0 deletions app/src/application/associations/routes.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from typing import Dict, List

from application.shared.base_router import BaseRouter
from application.typephoto.models.typePhoto import TypePhoto
from fastapi import APIRouter, Depends, HTTPException, FastAPI
from sqlalchemy.orm import Session

from application.core.database import get_db
from application.associations.database import repository
from application.associations.models.association import AssociationTypePhPW

from application.typephoto.database import repository as typePhotoRepository


class AssociationRouter(BaseRouter):
association_router = APIRouter()
Expand Down Expand Up @@ -44,6 +47,11 @@ async def delete_association(type_work_flag: int, type_photo_flag: int,
async def get_all_associations(db: Session = Depends(get_db)) -> list:
return repository.get_all_associations(db)

@staticmethod
@association_router.get("/twws/all")
async def get_all_associations(db: Session = Depends(get_db)) -> list:
return repository.get_all_work_status_associations(db)

@staticmethod
@association_router.get("/tptw/version")
async def get_table_version(db: Session = Depends(get_db)) -> Dict[str, int]:
Expand Down
2 changes: 1 addition & 1 deletion app/src/application/mp_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
mpApi = FastAPI(
title='F05 Backend API',
description='API backend for the project F05',
version="1.6.1",
version="1.7.0",
openapi_prefix=config.settings.api_prefix,
docs_url=None,
redoc_url=None,
Expand Down
5 changes: 3 additions & 2 deletions app/src/application/typephoto/database/typePhotoDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from application.core.database import Base

from application.typephoto.models.typePhoto import TypePhoto
from sqlalchemy.orm import relationship


class TypePhotoDB(Base):
Expand All @@ -13,6 +14,8 @@ class TypePhotoDB(Base):
name = Column(String)
description = Column(String, nullable=True)

type_works = relationship("TypeWorkDB", secondary='association_type_ph_pw')

@classmethod
def from_model(cls, type_photo: TypePhoto):
type_photo_db = TypePhotoDB(
Expand All @@ -28,5 +31,3 @@ def update(self, type_photo: TypePhoto):
self.flag = type_photo.flag
self.name = type_photo.name
self.description = type_photo.description


56 changes: 56 additions & 0 deletions app/src/application/typework/database/repository.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from typing import List

from application.typephoto.database.typePhotoDB import TypePhotoDB
from application.workstatus.database.workStatusDB import WorkStatusDB
from sqlalchemy import desc
from sqlalchemy.orm import Session

Expand All @@ -12,6 +16,16 @@ def get_type_work(db: Session) -> list:
return list(map(lambda type_work: type_work.parse_to_type_work(), db_type_works))


def get_type_work_type_photos(db: Session, type_work_id: int) -> list:
db_type_work = db.query(TypeWorkDB).filter(TypeWorkDB.flag == type_work_id).first()
return db_type_work.type_photos


def get_type_work_work_status(db: Session, type_work_id: int) -> list:
db_type_work = db.query(TypeWorkDB).filter(TypeWorkDB.flag == type_work_id).first()
return db_type_work.work_statuses


def add_type_work(db: Session, type_work: TypeWork) -> TypeWork:
db_type_work = TypeWorkDB.from_model(type_work)
db.add(db_type_work)
Expand Down Expand Up @@ -44,3 +58,45 @@ def get_table_version(db: Session) -> int:
return last_changed[0].transaction_id
else:
return -1


def add_type_photo_to_type_work(db: Session, type_work_id: int, type_photo_id: int) -> bool:
db_type_work = db.query(TypeWorkDB).filter(TypeWorkDB.flag == type_work_id).first()
db_type_photo = db.query(TypePhotoDB).filter(TypePhotoDB.flag == type_photo_id).first()
if db_type_work and db_type_photo:
db_type_work.type_photos.append(db_type_photo)
db.commit()
db.refresh(db_type_work)
return True
else:
return False


def update_type_photos_to_type_work(db: Session, type_work_id: int, type_photos: List[int]) -> bool:
db_type_work = db.query(TypeWorkDB).filter(TypeWorkDB.flag == type_work_id).first()
db_type_photos = db.query(TypePhotoDB).filter(TypePhotoDB.flag.in_(type_photos))
if db_type_work and db_type_photos.count() > 0:
db_type_work.type_photos = []
db.commit()
for type_photo in list(db_type_photos):
db_type_work.type_photos.append(type_photo)
db.commit()
db.refresh(db_type_work)
return True
else:
return False


def update_work_statuses_of_type_work(db: Session, type_work_id: int, work_statuses: List[int]) -> bool:
db_type_work = db.query(TypeWorkDB).filter(TypeWorkDB.flag == type_work_id).first()
db_work_statuses = db.query(WorkStatusDB).filter(WorkStatusDB.flag.in_(work_statuses))
if db_type_work and db_work_statuses.count() > 0:
db_type_work.work_statuses = []
db.commit()
for work_status in list(db_work_statuses):
db_type_work.work_statuses.append(work_status)
db.commit()
db.refresh(db_type_work)
return True
else:
return False
15 changes: 6 additions & 9 deletions app/src/application/typework/database/typeWorkDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ class TypeWorkDB(Base):

flag = Column(Integer, primary_key=True, index=True, autoincrement=True)
name = Column(String)
status_list = Column(String)

public_works = relationship("PublicWorkDB", backref="typework", cascade="all, delete-orphan")

type_photos = relationship("TypePhotoDB", secondary='association_type_ph_pw')
work_statuses = relationship("WorkStatusDB", secondary='association_tw_ws')

@classmethod
def from_model(cls, type_work: TypeWork):
parsed_list = ",".join(map(str, type_work.status_list))
type_work_db = TypeWorkDB(
name=type_work.name,
status_list=parsed_list)
type_work_db = TypeWorkDB(name=type_work.name)

if type_work.flag != 0:
type_work_db.flag = type_work.flag
Expand All @@ -29,16 +28,14 @@ def from_model(cls, type_work: TypeWork):

def parse_to_type_work(self):
parsed_list = []
if len(self.status_list) > 0:
parsed_list = list(map(int, self.status_list.split(',')))
if len(self.work_statuses) > 0:
parsed_list = list(map(lambda work_status: work_status.flag, self.work_statuses))
return TypeWork(
flag=self.flag,
name=self.name,
status_list=parsed_list
)

def update(self, type_work: TypeWork):
parsed_list = ",".join(map(str, type_work.status_list))
self.flag = type_work.flag
self.name = type_work.name
self.status_list = parsed_list
48 changes: 45 additions & 3 deletions app/src/application/typework/routes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import Dict, List

from application.shared.base_router import BaseRouter
from fastapi import APIRouter, Depends, HTTPException, FastAPI
from application.shared.response import Response
from application.typephoto.models.typePhoto import TypePhoto
from fastapi import APIRouter, Depends, HTTPException, FastAPI, Body
from sqlalchemy.orm import Session
from application.typework.models.typeWork import TypeWork
from application.core.database import get_db
Expand Down Expand Up @@ -34,7 +36,7 @@ async def update_type_work(type_work: TypeWork, db: Session = Depends(get_db)) -
if type_work_db:
return type_work_db
else:
raise HTTPException(status_code=403, detail="Not able to find type of work to update")
raise HTTPException(status_code=603, detail="Not able to find type of work to update")

@staticmethod
@type_work_router.post("/delete", responses={403: {"description": "Operation forbidden"}})
Expand All @@ -44,9 +46,49 @@ async def delete_type_work(type_work_id: int, db: Session = Depends(get_db)) ->
type_work_db.status_list = []
return type_work_db
else:
raise HTTPException(status_code=403, detail="Not able to find type of work to delete")
raise HTTPException(status_code=603, detail="Not able to find type of work to delete")

@staticmethod
@type_work_router.get("/version")
async def get_table_version(db: Session = Depends(get_db)) -> Dict[str, int]:
return {"version": repository.get_table_version(db)}

@staticmethod
@type_work_router.post("/typePhoto/add")
async def add_type_photo_to_type_work(type_work_id: int, type_photo_id: int,
db: Session = Depends(get_db)) -> Response:
result = repository.add_type_photo_to_type_work(db, type_work_id, type_photo_id)
if result:
return Response(success=result)
else:
raise HTTPException(status_code=603, detail="Not able to create association")

@staticmethod
@type_work_router.post("/typePhoto/update")
async def update_type_photos_to_type_work(type_work_id: int = Body(...), type_photos: List[int] = Body(...),
db: Session = Depends(get_db)) -> Response:
result = repository.update_type_photos_to_type_work(db, type_work_id, type_photos)
if result:
return Response(success=result)
else:
raise HTTPException(status_code=603, detail="Not able to create association between type photos")

@staticmethod
@type_work_router.get("/typePhoto/all")
async def get_type_photo_from_type_work(type_work_id: int, db: Session = Depends(get_db)) -> List[TypePhoto]:
return repository.get_type_work_type_photos(db, type_work_id)

@staticmethod
@type_work_router.post("/workStatus/update")
async def update_work_statuses_to_type_work(type_work_id: int = Body(...), work_statuses: List[int] = Body(...),
db: Session = Depends(get_db)) -> Response:
result = repository.update_work_statuses_of_type_work(db, type_work_id, work_statuses)
if result:
return Response(success=result)
else:
raise HTTPException(status_code=603, detail="Not able to create association between work status")

@staticmethod
@type_work_router.get("/workStatus/all")
async def get_work_status_from_type_work(type_work_id: int, db: Session = Depends(get_db)) -> List[TypePhoto]:
return repository.get_type_work_work_status(db, type_work_id)
12 changes: 3 additions & 9 deletions app/src/application/workstatus/database/workStatusDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from sqlalchemy import Column, Integer, String

from application.core.database import Base
from sqlalchemy.orm import relationship


class WorkStatusDB(Base):
Expand All @@ -11,15 +12,14 @@ class WorkStatusDB(Base):
flag = Column(Integer, primary_key=True, index=True, autoincrement=True)
name = Column(String)
description = Column(String)
type_work_list = Column(String)

type_works = relationship("TypeWorkDB", secondary='association_tw_ws')

@classmethod
def from_model(cls, work_status: WorkStatus):
parsed_list = ",".join(map(str, work_status.type_work_list))
work_status_db = WorkStatusDB(
name=work_status.name,
description=work_status.description,
type_work_list=parsed_list
)

if work_status.flag != 0:
Expand All @@ -28,19 +28,13 @@ def from_model(cls, work_status: WorkStatus):
return work_status_db

def parse_to_work_status(self):
parsed_list = []
if len(self.type_work_list) > 0:
parsed_list = list(map(int, self.type_work_list.split(',')))
return WorkStatus(
flag=self.flag,
name=self.name,
description=self.description,
type_work_list=parsed_list
)

def update(self, work_status: WorkStatus):
parsed_list = ",".join(map(str, work_status.type_work_list))
self.flag = work_status.flag
self.name = work_status.name
self.description = work_status.description
self.type_work_list = parsed_list
1 change: 0 additions & 1 deletion app/src/application/workstatus/models/workStatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class WorkStatus(BaseModel):
flag: int = None
name: str
description: str
type_work_list: List[int] = []

class Config:
orm_mode = True

0 comments on commit eeb0247

Please sign in to comment.