patchnotes_controller.py done

This commit is contained in:
Justin Weins 2025-03-31 16:06:00 +02:00
parent ace005248d
commit a13081cd71
7 changed files with 81 additions and 87 deletions

View File

@ -26,3 +26,4 @@ gen/openapi_server/encoder.py
gen/openapi_server/controllers gen/openapi_server/controllers
gen/openapi_server/test gen/openapi_server/test
gen/openapi_server gen/openapi_server
openapi_server/models/base_model.py

View File

@ -20,8 +20,9 @@ paths:
content: content:
application/json: application/json:
schema: schema:
items:
$ref: '#/components/schemas/patch_notes' $ref: '#/components/schemas/patch_notes'
"400":
description: bad payload
/patchnotes/{patchID}: /patchnotes/{patchID}:
delete: delete:
tags: tags:
@ -40,8 +41,6 @@ paths:
description: patch successfully deleted description: patch successfully deleted
"404": "404":
description: patch not found description: patch not found
"500":
description: Internal Server Error, Patch not deleted
/patchnotes/{date}: /patchnotes/{date}:
get: get:
description: returns list of all unseen Patch notes from the user description: returns list of all unseen Patch notes from the user
@ -66,8 +65,6 @@ paths:
description: returns list of all unseen Patch notes from the user description: returns list of all unseen Patch notes from the user
"404": "404":
description: no Patch notes found description: no Patch notes found
"500":
description: "Internal Server error, Failed to get Patch Notes"
tags: tags:
- Patchnotes - Patchnotes
x-openapi-router-controller: openapi_server.controllers.patchnotes_controller x-openapi-router-controller: openapi_server.controllers.patchnotes_controller
@ -90,13 +87,13 @@ components:
changes: changes:
type: string type: string
title: changes title: changes
date: patch_date:
title: date title: patch_date
type: string type: string
required: required:
- "title" - "title"
- "changes" - "changes"
- "date" - "patch_date"
user: user:
example: example:
lastSeenDate: 2025-01-02 lastSeenDate: 2025-01-02

View File

@ -24,3 +24,4 @@
openapi_server/controllers/patchnotes_controller.py openapi_server/controllers/patchnotes_controller.py
openapi_server/__main__.py openapi_server/__main__.py
openapi_server/encoder.py openapi_server/encoder.py
openapi_server/models/base_model.py

View File

@ -8,7 +8,6 @@ openapi_server/__init__.py
openapi_server/controllers/__init__.py openapi_server/controllers/__init__.py
openapi_server/controllers/security_controller.py openapi_server/controllers/security_controller.py
openapi_server/models/__init__.py openapi_server/models/__init__.py
openapi_server/models/base_model.py
openapi_server/models/patch_notes.py openapi_server/models/patch_notes.py
openapi_server/models/user.py openapi_server/models/user.py
openapi_server/openapi/openapi.yaml openapi_server/openapi/openapi.yaml

View File

@ -1,50 +1,17 @@
import logging import logging
import uuid import uuid
from datetime import datetime from datetime import datetime
from xxlimited_35 import error
from bson import json_util from bson import json_util
from connexion import ProblemException
from connexion.lifecycle import ConnexionResponse
from flask import Response from flask import Response
from openapi_server import util from openapi_server import util
from openapi_server.__main__ import collection from openapi_server.__main__ import collection
from openapi_server.models.patch_notes import PatchNotes # noqa: E501 from openapi_server.models.patch_notes import PatchNotes # noqa: E501
def patchnotes_date_get1(date): # noqa: E501
"""patchnotes_date_get
returns list of all unseen Patch notes from the user # noqa: E501
:param date:
:type date: str
:rtype: Union[List[PatchNotes], Tuple[List[PatchNotes], int], Tuple[List[PatchNotes], int, Dict[str, str]]
"""
logging.error(type(date))
try:
formatted_date = datetime.strptime(date, '%Y-%m-%d')
logging.error(formatted_date)
except ValueError:
logging.error("Not valid date")
try:
formatted_date = formatted_date.strftime('%Y-%m-%d')
logging.error('formatted date:' + str(formatted_date))
logging.info("valid date")
except ValueError:
logging.error("Not valid date")
date = util.deserialize_date(date)
logging.error('date:' + str(date))
query = {
'date': {'$gte': date.strftime('%Y-%m-%d')}
}
results = list(collection.find(query))
logging.error('results:' + str(results))
return Response(json_util.dumps(f'all unseen patchnotes: {results}'), mimetype='application/json', status=200)
def patchnotes_date_get(date): # noqa: E501 def patchnotes_date_get(date): # noqa: E501
"""patchnotes_date_get """patchnotes_date_get
@ -55,8 +22,18 @@ def patchnotes_date_get(date): # noqa: E501
:rtype: Union[List[PatchNotes], Tuple[List[PatchNotes], int], Tuple[List[PatchNotes], int, Dict[str, str]] :rtype: Union[List[PatchNotes], Tuple[List[PatchNotes], int], Tuple[List[PatchNotes], int, Dict[str, str]]
""" """
logging.error(type(date))
try:
datetime.strptime(date, '%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,
)
logging.error('date:' + str(date))
query = { query = {
'date': {'$gte': date} 'patch_date': {'$gte': date}
} }
results = [] results = []
for entry in collection.find(query): for entry in collection.find(query):
@ -80,10 +57,22 @@ def patchnotes_patch_iddelete(patch_id): # noqa: E501
:rtype: Union[None, Tuple[None, int], Tuple[None, int, Dict[str, str]] :rtype: Union[None, Tuple[None, int], Tuple[None, int, Dict[str, str]]
""" """
return 'do some magic!' deleted_doc = collection.delete_one({'patchID': patch_id})
logging.error(deleted_doc.deleted_count)
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 patchnotes_post(body): # TODO _date to date change def patchnotes_post(body):
"""patchnotes_post """patchnotes_post
creates new Patch notes # noqa: E501 creates new Patch notes # noqa: E501
@ -95,22 +84,33 @@ def patchnotes_post(body): # TODO _date to date change
:rtype: Union[List[PatchNotes], Tuple[List[PatchNotes], int], Tuple[List[PatchNotes], int, Dict[str, str]] :rtype: Union[List[PatchNotes], Tuple[List[PatchNotes], int], Tuple[List[PatchNotes], int, Dict[str, str]]
""" """
if body is None: if body is None:
return Response(json_util.dumps({'error': 'No patch notes provided'}), status=400) return Response('error: No patch notes provided', status=400)
pn = PatchNotes.from_dict(body) pn = PatchNotes.from_dict(body)
logging.error('patch_notes_type:' + str(type(pn))) 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,
)
logging.error('patch_notes_type:' + str(type(date_obj)))
logging.error('patch notes' + str(pn)) logging.error('patch notes' + str(pn))
patch_notes_uuid = uuid.uuid4() patch_note = {
'patchID': str(uuid.uuid4()),
patch_notes = {
'patchID': str(patch_notes_uuid),
'title': pn.title, 'title': pn.title,
'changes': pn.changes, 'changes': pn.changes,
'date': pn._date, 'patch_date': date_str
} }
logging.error('patch_notes_type:' + str(type(patch_notes)))
logging.error('patch notes' + str(patch_notes))
posted_patch_id = collection.insert_one(patch_notes)
return Response(json_util.dumps(f'patch with ID: {patch_notes} was created'), mimetype='application/json', logging.error('patch_notes_type:' + str(type(patch_note)))
logging.error('patch notes' + str(patch_note))
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) status=200)

View File

@ -12,7 +12,7 @@ class PatchNotes(Model):
Do not edit the class manually. Do not edit the class manually.
""" """
def __init__(self, patch_id=None, title=None, changes=None, _date=None): # noqa: E501 def __init__(self, patch_id=None, title=None, changes=None, patch_date=None): # noqa: E501
"""PatchNotes - a model defined in OpenAPI """PatchNotes - a model defined in OpenAPI
:param patch_id: The patch_id of this PatchNotes. # noqa: E501 :param patch_id: The patch_id of this PatchNotes. # noqa: E501
@ -21,27 +21,27 @@ class PatchNotes(Model):
:type title: str :type title: str
:param changes: The changes of this PatchNotes. # noqa: E501 :param changes: The changes of this PatchNotes. # noqa: E501
:type changes: str :type changes: str
:param _date: The _date of this PatchNotes. # noqa: E501 :param patch_date: The patch_date of this PatchNotes. # noqa: E501
:type _date: str :type patch_date: str
""" """
self.openapi_types = { self.openapi_types = {
'patch_id': str, 'patch_id': str,
'title': str, 'title': str,
'changes': str, 'changes': str,
'_date': str 'patch_date': str
} }
self.attribute_map = { self.attribute_map = {
'patch_id': 'patchID', 'patch_id': 'patchID',
'title': 'title', 'title': 'title',
'changes': 'changes', 'changes': 'changes',
'_date': 'date' 'patch_date': 'patch_date'
} }
self._patch_id = patch_id self._patch_id = patch_id
self._title = title self._title = title
self._changes = changes self._changes = changes
self.__date = _date self._patch_date = patch_date
@classmethod @classmethod
def from_dict(cls, dikt) -> 'PatchNotes': def from_dict(cls, dikt) -> 'PatchNotes':
@ -122,24 +122,24 @@ class PatchNotes(Model):
self._changes = changes self._changes = changes
@property @property
def _date(self) -> str: def patch_date(self) -> str:
"""Gets the _date of this PatchNotes. """Gets the patch_date of this PatchNotes.
:return: The _date of this PatchNotes. :return: The patch_date of this PatchNotes.
:rtype: str :rtype: str
""" """
return self.__date return self._patch_date
@_date.setter @patch_date.setter
def _date(self, _date: str): def patch_date(self, patch_date: str):
"""Sets the _date of this PatchNotes. """Sets the patch_date of this PatchNotes.
:param _date: The _date of this PatchNotes. :param patch_date: The patch_date of this PatchNotes.
:type _date: str :type patch_date: str
""" """
if _date is None: if patch_date is None:
raise ValueError("Invalid value for `_date`, must not be `None`") # noqa: E501 raise ValueError("Invalid value for `patch_date`, must not be `None`") # noqa: E501
self.__date = _date self._patch_date = patch_date

View File

@ -20,10 +20,10 @@ paths:
content: content:
application/json: application/json:
schema: schema:
items: $ref: '#/components/schemas/patch_notes'
$ref: '#/components/schemas/patch_notes'
type: array
description: game created description: game created
"400":
description: bad payload
tags: tags:
- Patchnotes - Patchnotes
x-openapi-router-controller: openapi_server.controllers.patchnotes_controller x-openapi-router-controller: openapi_server.controllers.patchnotes_controller
@ -52,8 +52,6 @@ paths:
description: returns list of all unseen Patch notes from the user description: returns list of all unseen Patch notes from the user
"404": "404":
description: no Patch notes found description: no Patch notes found
"500":
description: "Internal Server error, Failed to get Patch Notes"
tags: tags:
- Patchnotes - Patchnotes
x-openapi-router-controller: openapi_server.controllers.patchnotes_controller x-openapi-router-controller: openapi_server.controllers.patchnotes_controller
@ -75,8 +73,6 @@ paths:
description: patch successfully deleted description: patch successfully deleted
"404": "404":
description: patch not found description: patch not found
"500":
description: "Internal Server Error, Patch not deleted"
tags: tags:
- Patchnotes - Patchnotes
x-openapi-router-controller: openapi_server.controllers.patchnotes_controller x-openapi-router-controller: openapi_server.controllers.patchnotes_controller
@ -99,12 +95,12 @@ components:
changes: changes:
title: changes title: changes
type: string type: string
date: patch_date:
title: date title: patch_date
type: string type: string
required: required:
- changes - changes
- date - patch_date
- title - title
title: patch_notes title: patch_notes
user: user: