diff --git a/config/urls.py b/config/urls.py index d52e5777..c83842d3 100644 --- a/config/urls.py +++ b/config/urls.py @@ -15,7 +15,13 @@ """ from django.contrib import admin from django.urls import path +from ninja import NinjaAPI +from headless.controllers import controller + +api = NinjaAPI() +api.add_router('', controller) urlpatterns = [ path('admin/', admin.site.urls), -] + path('api/', api.urls) +] \ No newline at end of file diff --git a/headless/controllers.py b/headless/controllers.py index e69de29b..db3640b4 100644 --- a/headless/controllers.py +++ b/headless/controllers.py @@ -0,0 +1,24 @@ +from ninja import Router +from headless.utils import * + +controller = Router() + +@controller.get('posts') +def get_posts(request): + return list_posts() + +@controller.get('post') +def get_single_post(request, title): + return get_post(title) + +@controller.post('create') +def create_post(request, title, content): + return save_post(title, content) + +@controller.put('update') +def update_post(request, title, content): + return save_post(title, content) + +@controller.delete('deleted') +def delet_post(request, title): + return del_post(title) \ No newline at end of file diff --git a/headless/utils.py b/headless/utils.py index 20a48f9c..3947c97f 100644 --- a/headless/utils.py +++ b/headless/utils.py @@ -38,4 +38,11 @@ def get_post(title): def del_post(title): - pass \ No newline at end of file + """ + Delet a blog post, given its title + If an existing post with the same title already exists, + it is deleted. + """ + filename = f"posts/{title}.md" + if default_storage.exists(filename): + default_storage.delete(filename) \ No newline at end of file diff --git a/posts/Muntadhar.md b/posts/Muntadhar.md new file mode 100644 index 00000000..8f825da8 --- /dev/null +++ b/posts/Muntadhar.md @@ -0,0 +1 @@ +it's done \ No newline at end of file