diff --git a/config/urls.py b/config/urls.py index d52e5777..b76554f7 100644 --- a/config/urls.py +++ b/config/urls.py @@ -15,7 +15,14 @@ """ from django.contrib import admin from django.urls import path +from ninja import NinjaAPI +from headless.controllers import cont + +api = NinjaAPI() +api.add_router('/post', cont) + urlpatterns = [ path('admin/', admin.site.urls), + path('api/', api.urls) ] diff --git a/headless/controllers.py b/headless/controllers.py index e69de29b..63071cb2 100644 --- a/headless/controllers.py +++ b/headless/controllers.py @@ -0,0 +1,29 @@ +from ninja import Router +from headless.utils import * + +cont = Router() + + +@cont.get("") +def list_all_posts(request): + return list_posts() + + +@cont.get("/get_one_post") +def get_one(request, title: str): + return get_post(title) + + +@cont.post("/create_post") +def create(request, title: str, content: str): + return save_post(title, content) + + +@cont.put("/update_post") +def update(request, title: str, content: str): + return save_post(title, content) + + +@cont.delete("/delete_post") +def delete(request, title: str): + return del_post(title) diff --git a/headless/utils.py b/headless/utils.py index 20a48f9c..a75fbf7c 100644 --- a/headless/utils.py +++ b/headless/utils.py @@ -10,7 +10,7 @@ def list_posts(): """ _, filenames = default_storage.listdir("posts") return list(sorted(re.sub(r"\.md$", "", filename) - for filename in filenames if filename.endswith(".md"))) + for filename in filenames if filename.endswith(".md"))) def save_post(title, content): @@ -38,4 +38,10 @@ def get_post(title): def del_post(title): - pass \ No newline at end of file + """ + Retrieves a post by its title. If no such + post exists, the function returns None. + """ + filename = f"posts/{title}.md" + if default_storage.exists(filename): + default_storage.delete(filename) diff --git a/posts/php_sucks.md b/posts/php_sucks.md index 56fa8623..48cbd979 100644 --- a/posts/php_sucks.md +++ b/posts/php_sucks.md @@ -1 +1 @@ -PHP sucks because Layth said so! \ No newline at end of file +cuz layth said so! \ No newline at end of file diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index ac5d13a7..00000000 --- a/requirements.txt +++ /dev/null @@ -1,7 +0,0 @@ -asgiref==3.4.1 -Django==3.2.8 -django-ninja==0.16.1 -pydantic==1.8.2 -pytz==2021.3 -sqlparse==0.4.2 -typing-extensions==3.10.0.2