create and put patchnote bugs fixed
This commit is contained in:
parent
ab26234c20
commit
bdf0466d79
|
|
@ -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)
|
pn = PatchNotes.from_dict(body)
|
||||||
|
|
||||||
|
# --- Datum wahlweise als str oder date annehmen ---
|
||||||
|
if isinstance(pn.patch_date, date):
|
||||||
|
date_str = pn.patch_date.isoformat()
|
||||||
|
else:
|
||||||
try:
|
try:
|
||||||
date_obj = datetime.strptime(pn.patch_date, '%Y-%m-%d').date()
|
date_str = datetime.strptime(pn.patch_date, "%Y-%m-%d").date().isoformat()
|
||||||
date_str = date_obj.strftime('%Y-%m-%d')
|
except ValueError:
|
||||||
except ValueError as e:
|
|
||||||
raise ProblemException(
|
raise ProblemException(
|
||||||
title="Bad Request",
|
title="Bad Request",
|
||||||
detail="The date is invalid. Needs to be in this Format YYYY-MM-DD",
|
detail="patch_date must be in format YYYY-MM-DD",
|
||||||
status=400,
|
status=400,
|
||||||
)
|
)
|
||||||
|
|
||||||
patch_note = {
|
patch_doc = {
|
||||||
'patchID': str(uuid.uuid4()),
|
"patchID": str(uuid.uuid4()),
|
||||||
'title': pn.title,
|
"title": pn.title,
|
||||||
'changes': pn.changes,
|
"changes": pn.changes,
|
||||||
'version': pn.version,
|
"version": pn.version,
|
||||||
'patch_date': date_str
|
"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)
|
||||||
|
|
||||||
|
if isinstance(pn.patch_date, date):
|
||||||
|
date_str = pn.patch_date.isoformat()
|
||||||
|
else:
|
||||||
try:
|
try:
|
||||||
date_obj = datetime.strptime(pn.patch_date, '%Y-%m-%d').date()
|
date_str = datetime.strptime(pn.patch_date, "%Y-%m-%d").date().isoformat()
|
||||||
date_str = date_obj.strftime('%Y-%m-%d')
|
except ValueError:
|
||||||
except ValueError as e:
|
|
||||||
raise ProblemException(
|
raise ProblemException(
|
||||||
title="Bad Request",
|
title="Bad Request",
|
||||||
detail="The date is invalid. Needs to be in this Format YYYY-MM-DD",
|
detail="patch_date must be in format YYYY-MM-DD",
|
||||||
status=400,
|
status=400,
|
||||||
)
|
)
|
||||||
query_filter = {'patchID': patch_id}
|
|
||||||
updated_post = {'$set':
|
result = collection.update_one(
|
||||||
{
|
{"patchID": patch_id},
|
||||||
'title': pn.title,
|
{"$set": {
|
||||||
'changes': pn.changes,
|
"title": pn.title,
|
||||||
'version': pn.version,
|
"changes": pn.changes,
|
||||||
'date': date_str
|
"version": pn.version,
|
||||||
}
|
"patch_date": date_str, # richtiger Feldname!
|
||||||
}
|
}}
|
||||||
result = collection.update_one(query_filter, updated_post)
|
)
|
||||||
if result.modified_count == 0:
|
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue