Skip to content

Commit

Permalink
Fix routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Walter committed Dec 10, 2020
1 parent 2e7ed78 commit 4a2fbae
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 11 deletions.
13 changes: 12 additions & 1 deletion app/src/application/collect/database/collectDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,25 @@ def from_model(cls, collect: Collect):
comments=collect.comments,
public_work_id=collect.public_work_id,
user_email=collect.user_email,
public_work_status = collect.public_work_status
public_work_status=collect.public_work_status
)

if collect.id and is_valid_uuid(collect.id):
collect_db.id = collect.id

return collect_db

def parse_to_collect(self):
return Collect(
id=self.id,
public_work_id=self.public_work_id,
date=self.date,
user_email=self.user_email,
comments=self.comments,
public_work_status=self.public_work_status,
photos=self.photos
)

def update(self, collect: Collect):
self.id = collect.id
self.comments = collect.comments
Expand Down
6 changes: 4 additions & 2 deletions app/src/application/collect/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ async def get_all_collect(db: Session = Depends(get_db)) -> List[Collect]:

@staticmethod
@collect_router.get("/paginated")
async def get_collect_paginated(page: int = Query(1), per_page: int = Query(20),
async def get_collect_paginated(page: int = 1, per_page: int = 20,
db: Session = Depends(get_db)) -> Pagination:
return repository.get_all_collect_paginated(db, page, per_page)
paginatedCollects = repository.get_all_collect_paginated(db, page, per_page)
paginatedCollects.data = list(map(lambda collect: collect.parse_to_collect(), paginatedCollects.data))
return paginatedCollects

@staticmethod
@collect_router.post("/add")
Expand Down
4 changes: 4 additions & 0 deletions app/src/application/file/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
from pydantic import BaseModel


def check_folder_exists_or_create(folder: str):
Path(folder).mkdir(parents=True, exist_ok=True)

def create_json_file(data: BaseModel):
with open('data.txt', 'w') as outfile:
json.dump(data, outfile)


def create_json_file_from_list(filename: str, data: List[BaseModel]) -> Path:
report_folder = Path(config.settings.report_folder)
check_folder_exists_or_create(report_folder)
destination = report_folder.joinpath(filename + ".json")
new_data = list(map(lambda item: item.json(), data))
with destination.open('w') as outfile:
Expand Down
6 changes: 6 additions & 0 deletions app/src/application/image/database/repository.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import shutil

from pathlib import Path
from application.core import config
from application.file.file_utils import check_folder_exists_or_create

from fastapi import UploadFile


def check_folder_exists():
check_folder_exists_or_create(config.settings.image_folder)


def save_upload_file(upload_file: UploadFile, destination: Path) -> bool:
destination = destination.joinpath(upload_file.filename)
file_saved = True
Expand Down
3 changes: 2 additions & 1 deletion app/src/application/image/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ def route(self) -> APIRouter:
@staticmethod
@images_router.post("/upload", responses={403: {"description": "Operation forbidden"}})
def upload_image(request: Request, file: UploadFile = File(...)):
repository.check_folder_exists()
image_folder = config.settings.image_folder
saved = repository.save_upload_file(file, Path(image_folder))
if saved:
filepath = "{0}{1}{2}".format(request.base_url.__str__(), "images/", file.filename)
return {"filepath": filepath}
else:
raise HTTPException(status_code=403, detail="Not able to find public work to delete")
raise HTTPException(status_code=403, detail="Not able to add image")

@staticmethod
@images_router.get("/{image_name}")
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 @@ -27,7 +27,7 @@
mpApi = FastAPI(
title='F05 Backend API',
description='API backend for the project F05',
version="1.8.1",
version="1.8.2",
openapi_prefix=config.settings.api_prefix,
docs_url=None,
redoc_url=None,
Expand Down
2 changes: 1 addition & 1 deletion app/src/application/publicwork/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async def get_all_public_work(db: Session = Depends(get_db)) -> List[PublicWork]

@staticmethod
@public_work_router.get("/paginated")
async def get_public_work_paginated(page: int = Query(1), per_page: int = Query(10),
async def get_public_work_paginated(page: int = 1, per_page: int = 10,
db: Session = Depends(get_db)) -> Pagination:
public_work_list = public_work_repository.get_public_work_paginated(db, page, per_page)
if public_work_list:
Expand Down
7 changes: 6 additions & 1 deletion app/src/application/receiving_queue/data/database.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from pymongo import MongoClient

from application.core import config

class QueueDB:
_instance = None

def __init__(self):
self.client = MongoClient('mongodb', 27017)
if config.settings.environment == "development":
mongoClient = MongoClient(port=27017)
else:
mongoClient = MongoClient('mongodb', 27017)
self.client = mongoClient
self.queue_db = self.client.trena

@classmethod
Expand Down
18 changes: 17 additions & 1 deletion app/src/application/receiving_queue/data/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ def accept_public_work(sqldb: Session, public_work_id: str) -> bool:
return False


def delete_public_work(public_work_id: str) -> bool:
db[public_work_id].drop()


def accept_collect(sqldb: Session, public_work_id: str, collect_id: str) -> bool:
collect = db[public_work_id].find_one({"type": "collect", DATA_ID_KEY: collect_id})
if collect:
Expand All @@ -150,6 +154,16 @@ def accept_collect(sqldb: Session, public_work_id: str, collect_id: str) -> bool
return False


def delete_collect(public_work_id: str, collect_id: str) -> bool:
collect = db[public_work_id].find_one({"type": "collect", DATA_ID_KEY: collect_id})
if collect:
db[public_work_id].delete_many({"type": "collect", DATA_ID_KEY: collect_id})
db[public_work_id].delete_many({"type": "photo", 'data.collect_id': collect_id})
check_collection_empty(public_work_id)
return True
return False


def accept_photo(sqldb: Session, public_work_id: str, photo_id: str) -> bool:
photo = db[public_work_id].find_one({"type": "photo", DATA_ID_KEY: photo_id})
if photo:
Expand All @@ -165,10 +179,12 @@ def accept_photo(sqldb: Session, public_work_id: str, photo_id: str) -> bool:
def deletePhoto(public_work_id: str, photo_id: str) -> bool:
photo = db[public_work_id].find_one({"type": "photo", DATA_ID_KEY: photo_id})
if photo:
photo.remove()
db[public_work_id].delete_many({"type": "photo", DATA_ID_KEY: photo_id})
check_collection_empty(public_work_id)
return True
return False


def check_collection_empty(public_work_id: str):
if db[public_work_id].count() == 0:
db[public_work_id].drop()
Expand Down
21 changes: 18 additions & 3 deletions app/src/application/receiving_queue/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ async def accept_public_work(id: str, db: Session = Depends(get_db)) -> Response
else:
return Response(success=False, error=Error(status_code=401, message="Erro ao aceitar obra pública"))

@staticmethod
@queueRouter.post("/publicwork/{id}/delete")
async def delete_public_work(id: str) -> Response:
if repository.delete_public_work(id):
return Response(success=True)
else:
return Response(success=False, error=Error(status_code=401, message="Erro ao deletar obra pública"))

@staticmethod
@queueRouter.post("/publicwork/{public_work_id}/collect/{collect_id}/accept")
async def accept_collect(public_work_id: str, collect_id: str, db: Session = Depends(get_db)) -> Response:
Expand All @@ -79,6 +87,14 @@ async def accept_collect(public_work_id: str, collect_id: str, db: Session = Dep
else:
return Response(success=False, error=Error(status_code=401, message="Erro ao aceitar coleta"))

@staticmethod
@queueRouter.post("/publicwork/{public_work_id}/collect/{collect_id}/delete")
async def delete_collect(public_work_id: str, collect_id: str) -> Response:
if repository.delete_collect(public_work_id, collect_id):
return Response(success=True)
else:
return Response(success=False, error=Error(status_code=401, message="Erro ao deletar coleta"))

@staticmethod
@queueRouter.get("/publicwork/{public_work_id}/collect/{collect_id}/photos")
async def get_photos_of_collect(public_work_id: str, collect_id: str) -> List[Photo]:
Expand All @@ -102,11 +118,10 @@ async def accept_photo(public_work_id: str, photo_id: str, db: Session = Depends
else:
return Response(success=False, error=Error(status_code=401, message="Erro ao aceitar foto"))


@staticmethod
@queueRouter.post("/publicwork/{public_work_id}/photo/{photo_id}/delete")
async def accept_photo(public_work_id: str, photo_id: str, db: Session = Depends(get_db)) -> Response:
async def delete_photo(public_work_id: str, photo_id: str, db: Session = Depends(get_db)) -> Response:
if repository.deletePhoto(db, public_work_id, photo_id):
return Response(success=True)
else:
return Response(success=False, error=Error(status_code=401, message="Erro ao aceitar foto"))
return Response(success=False, error=Error(status_code=401, message="Erro ao deletar foto"))

0 comments on commit 4a2fbae

Please sign in to comment.