import connexion from typing import Dict from typing import Tuple from typing import Union from openapi_server.models.patch_notes import PatchNotes # noqa: E501 from openapi_server.models.patch_notes_input import PatchNotesInput # noqa: E501 from openapi_server import util import logging import uuid from datetime import datetime from bson import json_util from connexion import ProblemException from flask import Response from __main__ import collection from openapi_server.__main__ import collection from openapi_server.models.patch_notes import PatchNotes def create_patchnote(body): # noqa: E501 """Creates new patch notes # noqa: E501 :param body: :param patch_notes_input: :type patch_notes_input: dict | bytes :rtype: Union[PatchNotes, Tuple[PatchNotes, int], Tuple[PatchNotes, int, Dict[str, str]] """ patch_notes_input = body if body is None: return Response('error: 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 = { 'patchID': str(uuid.uuid4()), 'title': pn.title, 'changes': pn.changes, 'version': pn.version, 'patch_date': date_str } collection.insert_one(patch_note) patch_note_obj = PatchNotes.from_dict(patch_note) patch_note = patch_note_obj.to_dict() return Response(json_util.dumps(patch_note), mimetype='application/json', status=200) def delete_patchnote(patch_id): # noqa: E501 """Deletes one patch note # noqa: E501 :param patch_id: ID of the patch note :type patch_id: str :type patch_id: str :rtype: Union[None, Tuple[None, int], Tuple[None, int, Dict[str, str]] """ deleted_doc = collection.delete_one({'patchID': patch_id}) if deleted_doc.deleted_count == 0: raise ProblemException( title="Not found", detail="No document has been deleted", status=404, ) return Response( "Document was deleted", mimetype='application/json', status=200, ) def get_patchnote(patch_id): # noqa: E501 """Returns one patch note # noqa: E501 :param patch_id: ID of the patch note :type patch_id: str :type patch_id: str :rtype: Union[PatchNotes, Tuple[PatchNotes, int], Tuple[PatchNotes, int, Dict[str, str]] """ pn = collection.find_one({'patchID': patch_id}) if pn is None: raise ProblemException( title="Not found", detail="No document has been found", status=404, ) return Response( json_util.dumps(pn), mimetype='application/json', status=200, ) def list_patchnotes(since=None, limit=None, offset=None): # noqa: E501 """Returns list of patch notes newer than the caller’s last login # noqa: E501 :param since: Only patchnotes newer than this date (YYYY-MM-DD) :type since: str :param limit: Maximum number of items to return :type limit: int :param offset: Pagination offset :type offset: int :rtype: Union[List[PatchNotes], Tuple[List[PatchNotes], int], Tuple[List[PatchNotes], int, Dict[str, str]] """ try: datetime.strptime(since, '%Y-%m-%d') # 2025-01-0 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, ) query = { 'patch_date': {'$gte': since} } results = [] for entry in collection.find(query): patch_note = PatchNotes.from_dict(entry) results.append(patch_note.to_dict()) return Response( json_util.dumps(results), mimetype='application/json', status=200, ) def update_patchnote(patch_id, body): # noqa: E501 """Updates one patch note (full replace) # noqa: E501 :param body: :param patch_id: ID of the patch note :type patch_id: str :type patch_id: str :param patch_notes_input: :type patch_notes_input: dict | bytes :rtype: Union[PatchNotes, Tuple[PatchNotes, int], Tuple[PatchNotes, int, Dict[str, str]] """ 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, ) query_filter = {'patchID': patch_id} updated_post = {'$set': { 'title': pn.title, 'changes': pn.changes, 'version': pn.version, 'date': date_str } } result = collection.update_one(query_filter, updated_post) if result.modified_count == 0: raise ProblemException( title="Not found", detail="No document has been deleted", status=404, ) return Response("Document was updated", status=200, mimetype='application/json')