create and put patchnote bugs fixed

This commit is contained in:
Justin Weins 2025-05-21 16:10:05 +02:00
parent ab26234c20
commit bdf0466d79
1 changed files with 69 additions and 45 deletions

View File

@ -31,32 +31,41 @@ def create_patchnote(body): # noqa: E501
:rtype: Union[PatchNotes, Tuple[PatchNotes, int], Tuple[PatchNotes, int, Dict[str, str]] :rtype: Union[PatchNotes, Tuple[PatchNotes, int], Tuple[PatchNotes, int, Dict[str, str]]
""" """
patch_notes_input = body patch_notes_input = body
if body is None: if not body:
return Response('error: No patch notes provided', status=400) return Response("No patch notes provided", status=400)
pn = PatchNotes.from_dict(body)
try:
date_obj = datetime.strptime(pn.patch_date, '%Y-%m-%d').date()
date_str = date_obj.strftime('%Y-%m-%d')
except ValueError as e:
raise ProblemException(
title="Bad Request",
detail="The date is invalid. Needs to be in this Format YYYY-MM-DD",
status=400,
)
patch_note = { pn = PatchNotes.from_dict(body)
'patchID': str(uuid.uuid4()),
'title': pn.title, # --- Datum wahlweise als str oder date annehmen ---
'changes': pn.changes, if isinstance(pn.patch_date, date):
'version': pn.version, date_str = pn.patch_date.isoformat()
'patch_date': date_str else:
try:
date_str = datetime.strptime(pn.patch_date, "%Y-%m-%d").date().isoformat()
except ValueError:
raise ProblemException(
title="Bad Request",
detail="patch_date must be in format YYYY-MM-DD",
status=400,
)
patch_doc = {
"patchID": str(uuid.uuid4()),
"title": pn.title,
"changes": pn.changes,
"version": pn.version,
"patch_date": date_str,
} }
collection.insert_one(patch_note) collection.insert_one(patch_doc)
patch_note_obj = PatchNotes.from_dict(patch_note) patch_doc.pop("_id", None) # Mongo-internes Feld ausblenden
patch_note = patch_note_obj.to_dict()
return Response(json_util.dumps(patch_note), mimetype='application/json', return Response(
status=200) json_util.dumps(patch_doc),
mimetype="application/json",
status=201, # 201 Created
headers={"Location": f"/api/patchnotes/{patch_doc['patchID']}"},
)
def delete_patchnote(patch_id): # noqa: E501 def delete_patchnote(patch_id): # noqa: E501
@ -155,30 +164,45 @@ def update_patchnote(patch_id, body): # noqa: E501
:rtype: Union[PatchNotes, Tuple[PatchNotes, int], Tuple[PatchNotes, int, Dict[str, str]] :rtype: Union[PatchNotes, Tuple[PatchNotes, int], Tuple[PatchNotes, int, Dict[str, str]]
""" """
if not body:
return Response("No patch notes provided", status=400)
pn = PatchNotes.from_dict(body) pn = PatchNotes.from_dict(body)
try:
date_obj = datetime.strptime(pn.patch_date, '%Y-%m-%d').date() if isinstance(pn.patch_date, date):
date_str = date_obj.strftime('%Y-%m-%d') date_str = pn.patch_date.isoformat()
except ValueError as e: else:
raise ProblemException( try:
title="Bad Request", date_str = datetime.strptime(pn.patch_date, "%Y-%m-%d").date().isoformat()
detail="The date is invalid. Needs to be in this Format YYYY-MM-DD", except ValueError:
status=400, raise ProblemException(
) title="Bad Request",
query_filter = {'patchID': patch_id} detail="patch_date must be in format YYYY-MM-DD",
updated_post = {'$set': status=400,
{ )
'title': pn.title,
'changes': pn.changes, result = collection.update_one(
'version': pn.version, {"patchID": patch_id},
'date': date_str {"$set": {
} "title": pn.title,
} "changes": pn.changes,
result = collection.update_one(query_filter, updated_post) "version": pn.version,
if result.modified_count == 0: "patch_date": date_str, # richtiger Feldname!
}}
)
if result.matched_count == 0:
raise ProblemException( raise ProblemException(
title="Not found", title="Not found",
detail="No document has been deleted", detail="No document has been found",
status=404, status=404,
) )
return Response("Document was updated", status=200, mimetype='application/json')
doc = collection.find_one({"patchID": patch_id})
doc.pop("_id", None)
return Response(
json_util.dumps(doc),
mimetype="application/json",
status=200,
)