Commit 54912334 authored by Pedro Carlucci's avatar Pedro Carlucci Committed by Arthur Sudbrack Ibarra
Browse files

feat: stopping with pdfs duplications

parent 78a692b2
Showing with 20 additions and 22 deletions
+20 -22
......@@ -20,20 +20,9 @@ class PDFRepository:
return pdfs
def get_by_nome(self, nome: str) -> PDF:
# This method returns the latest pdf with the given nome.
# It uses the 'criado' field to determine which pdf is the latest.
all_pdfs = self.get_all()
latest_pdf = None
for pdf in all_pdfs:
if pdf.nome == nome:
if latest_pdf is None:
latest_pdf = pdf
else:
if is_date_after(pdf.criado, latest_pdf.criado):
latest_pdf = pdf
if latest_pdf is None:
raise Exception(f"PDF com nome {nome} não encontrado.")
return latest_pdf
# This method returns pdf with the given nome.
pdf_dict = self._collection.find_one({"nome": nome})
return PDF.parse_obj(pdf_dict)
def create(self, pdf_data: PDF) -> InsertOneResult:
# On create we need to convert car_data to dict and then insert it into the database.
......
......@@ -24,17 +24,26 @@ class PDFService:
def get_by_nome(self, nome: str) -> PDF:
try:
return self._repository.get_by_nome(nome)
except Exception as error:
except Exception:
raise HTTPException(
status_code=404, detail=error.args[0])
status_code=404, detail="PDF não encontrado.")
def create(self, pdf_data: PDF) -> PDF:
# Set date attributes.
created_date = current_date()
pdf_data.criado = created_date
pdf_data.ultimo_visto = created_date
result = self._repository.create(pdf_data)
return self._repository.find_by_id(result.inserted_id)
try:
# get_by_nome raises a 404 error if the car is not found.
self.get_by_nome(pdf_data.nome)
# If we got here, that means the pdf already exists in the database.
raise ValueError(f"PDF já existente por nome: {pdf_data.nome}.")
except HTTPException as e:
if e.status_code == 404:
created_date = current_date()
pdf_data.criado = created_date
pdf_data.ultimo_visto = created_date
result = self._repository.create(pdf_data)
return self._repository.find_by_id(result.inserted_id)
except ValueError as e:
raise HTTPException(
status_code=400, detail=e.args[0])
def update(self, nome: str, pdf_data: PDF) -> PDF:
pdf_data.ultimo_visto = current_date()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment