Added basic create, read, delete functionality
This commit is contained in:
parent
61d50cd13c
commit
58c843800c
5 changed files with 58 additions and 21 deletions
|
@ -13,6 +13,10 @@ class Database:
|
||||||
__instance = None
|
__instance = None
|
||||||
db = DDB
|
db = DDB
|
||||||
db.config.storage_directory = DatabaseSettings.DATABASE_DIRECTORY
|
db.config.storage_directory = DatabaseSettings.DATABASE_DIRECTORY
|
||||||
|
if not db.at("requests").exists():
|
||||||
|
db.at("requests").create({})
|
||||||
|
if not db.at("profiles").exists():
|
||||||
|
db.at("profiles").create({})
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_instance():
|
def get_instance():
|
||||||
|
|
|
@ -1,18 +1,23 @@
|
||||||
from flask.views import MethodView
|
from flask.views import MethodView
|
||||||
import marshmallow as ma
|
import marshmallow as ma
|
||||||
|
|
||||||
from app import Application
|
from database import Database
|
||||||
from . import routes_requests as blp
|
from . import routes_requests as blp
|
||||||
|
|
||||||
instance = Application.get_instance()
|
db = Database.get_instance().db
|
||||||
|
|
||||||
|
class RequestsAll(ma.Schema):
|
||||||
|
uuid = ma.fields.String(description="UUID v4", example="3f6dd2e5-8cb1-4ce0-82e4-b68edf4ce37a")
|
||||||
|
status = ma.fields.String()
|
||||||
|
|
||||||
class RequestsGetAll(ma.Schema):
|
|
||||||
pass
|
|
||||||
|
|
||||||
@blp.route("/all")
|
@blp.route("/all")
|
||||||
class AllRequests(MethodView):
|
class AllRequests(MethodView):
|
||||||
@blp.doc(summary="Returns all requests")
|
@blp.doc(summary="Returns all requests")
|
||||||
@blp.response(200, RequestsGetAll, description="Successful operation")
|
@blp.response(200, RequestsAll(many=True), description="Successful operation")
|
||||||
def get(self):
|
def get(self):
|
||||||
response = {}
|
db_data = db.at("requests").read()
|
||||||
|
response = []
|
||||||
|
for key, value in db_data.items():
|
||||||
|
response.append({"uuid": key, **value})
|
||||||
return response
|
return response
|
||||||
|
|
|
@ -1,18 +1,27 @@
|
||||||
|
from uuid import uuid4
|
||||||
from flask.views import MethodView
|
from flask.views import MethodView
|
||||||
import marshmallow as ma
|
import marshmallow as ma
|
||||||
|
|
||||||
from app import Application
|
from database import Database
|
||||||
from . import routes_requests as blp
|
from . import routes_requests as blp
|
||||||
|
|
||||||
instance = Application.get_instance()
|
db = Database.get_instance().db
|
||||||
|
|
||||||
class RequestsCreate(ma.Schema):
|
class RequestsCreate(ma.Schema):
|
||||||
pass
|
status = ma.fields.String(example="Request 3f6dd2e5-8cb1-4ce0-82e4-b68edf4ce37a was created")
|
||||||
|
uuid = ma.fields.String(example="3f6dd2e5-8cb1-4ce0-82e4-b68edf4ce37a")
|
||||||
|
|
||||||
@blp.route("/create")
|
@blp.route("/create")
|
||||||
class CreateRequest(MethodView):
|
class CreateRequest(MethodView):
|
||||||
@blp.doc(summary="Create a new request")
|
@blp.doc(summary="Create a new request")
|
||||||
@blp.response(200, RequestsCreate, description="Successful operation")
|
@blp.response(200, RequestsCreate, description="Successful operation")
|
||||||
def post(self):
|
def post(self):
|
||||||
response = {}
|
uuid = str(uuid4())
|
||||||
|
with db.at("requests").session() as (session, requests):
|
||||||
|
requests[uuid] = { "status": "created" }
|
||||||
|
session.write()
|
||||||
|
response = {
|
||||||
|
"status": f"Request {uuid} was created",
|
||||||
|
"uuid": uuid
|
||||||
|
}
|
||||||
return response
|
return response
|
||||||
|
|
|
@ -1,24 +1,44 @@
|
||||||
|
from flask_smorest import abort
|
||||||
from flask.views import MethodView
|
from flask.views import MethodView
|
||||||
import marshmallow as ma
|
import marshmallow as ma
|
||||||
|
|
||||||
from app import Application
|
from database import Database
|
||||||
from . import routes_requests as blp
|
from . import routes_requests as blp
|
||||||
|
|
||||||
instance = Application.get_instance()
|
db = Database.get_instance().db
|
||||||
|
|
||||||
|
class UuidSchema(ma.Schema):
|
||||||
|
uuid = ma.fields.String(description="UUID of the request", example="3f6dd2e5-8cb1-4ce0-82e4-b68edf4ce37a")
|
||||||
|
|
||||||
class RequestsInteraction(ma.Schema):
|
class RequestsInteraction(ma.Schema):
|
||||||
pass
|
status = ma.fields.String()
|
||||||
|
|
||||||
@blp.route("/<uuid>")
|
class RequestsInteractionDelete(ma.Schema):
|
||||||
|
status = ma.fields.String(example="Request 3f6dd2e5-8cb1-4ce0-82e4-b68edf4ce37a was deleted.")
|
||||||
|
|
||||||
|
@blp.route("/<string:uuid>")
|
||||||
class InteractRequest(MethodView):
|
class InteractRequest(MethodView):
|
||||||
@blp.doc(summary="Returns a specific request by its UID")
|
@blp.doc(summary="Returns a specific request by its UID")
|
||||||
|
@blp.arguments(UuidSchema, location='path', as_kwargs=True)
|
||||||
@blp.response(200, RequestsInteraction, description="Successful operation")
|
@blp.response(200, RequestsInteraction, description="Successful operation")
|
||||||
def get(self):
|
@blp.alt_response(404)
|
||||||
response = {}
|
def get(self, uuid):
|
||||||
|
response = db.at("requests", key=uuid).read()
|
||||||
|
if response is None:
|
||||||
|
abort(404, message=f"Request {uuid} does not exist.")
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@blp.doc(summary="Deletes a specific request by its UID")
|
@blp.doc(summary="Deletes a specific request by its UID")
|
||||||
@blp.response(200, RequestsInteraction, description="Successful operation")
|
@blp.arguments(UuidSchema, location='path', as_kwargs=True)
|
||||||
def delete(self):
|
@blp.response(200, RequestsInteractionDelete, description="Successful operation")
|
||||||
response = {}
|
@blp.alt_response(404)
|
||||||
|
def delete(self, uuid):
|
||||||
|
if not db.at("requests", key=uuid).exists():
|
||||||
|
abort(404, message=f"Request {uuid} does not exist.")
|
||||||
|
with db.at("requests").session() as (session, requests):
|
||||||
|
del requests[uuid]
|
||||||
|
session.write()
|
||||||
|
response = {
|
||||||
|
"status": f"Request {uuid} was deleted."
|
||||||
|
}
|
||||||
return response
|
return response
|
||||||
|
|
|
@ -8,8 +8,7 @@ from routes.requests import routes_requests
|
||||||
from config.server_settings import ServerSettings
|
from config.server_settings import ServerSettings
|
||||||
|
|
||||||
instance = Application.get_instance()
|
instance = Application.get_instance()
|
||||||
app = instance.app
|
app, api = instance.app, instance.api
|
||||||
api = instance.api
|
|
||||||
api.register_blueprint(routes)
|
api.register_blueprint(routes)
|
||||||
api.register_blueprint(routes_requests)
|
api.register_blueprint(routes_requests)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue