diff --git a/config/settings.py b/config/settings.py index e332a2b..91faf4d 100644 --- a/config/settings.py +++ b/config/settings.py @@ -37,6 +37,7 @@ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'files_management' ] MIDDLEWARE = [ diff --git a/config/urls.py b/config/urls.py index b6601c1..0ee8309 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 +# call the router +from files_management.controller import posts_cont + +api=NinjaAPI() +api.add_router('/posts',posts_cont) urlpatterns = [ path('admin/', admin.site.urls), + path('api/',api.urls) ] diff --git a/files_management/controller.py b/files_management/controller.py index 45a329d..b7fb7eb 100644 --- a/files_management/controller.py +++ b/files_management/controller.py @@ -1,2 +1,47 @@ +from tkinter import N from django.shortcuts import render +from ninja import Router,Schema # call Router and schema class from ninja package +from files_management.utils import list_posts,get_post,save_post,del_post # call utils functions + +class BodyIn(Schema): + post_name: str = None + post_content: str + + # Create your views here. +posts_cont=Router() + +# Get All +@posts_cont.get('') +def posts_get_all(request): + return {'All Posts':list_posts()} + +# Get sertain file +@posts_cont.get('/{title}') +def posts_get_one(request,title): + F_content=get_post(title=title) + if F_content==None: return {'massege': 'Sorry, No post with this name!'} + else: return {'File': F_content} + +# Post new file +@posts_cont.post('') +def posts_post_new(request, payload: BodyIn): + save_post(title=payload.post_name,content=payload.post_content) + return {'massege': 'Post Saved successfully'} + +# Update file +@posts_cont.put('/{title}') +def posts_update(request, title,payload: BodyIn): + if title in list_posts(): + save_post(title=title,content=payload.post_content) + return {'massege': 'Post updated successfully'} + else: return {'massege': 'Sorry, No post with this name!'} + +# Delete post +@posts_cont.delete('/{title}') +def post_delet(request,title): + if title in list_posts(): + del_post(title) + return {'massege': 'Post deleteted successfully'} + else: return {'massege': 'Sorry, No post with this name!'} + \ No newline at end of file diff --git a/files_management/utils.py b/files_management/utils.py index fbcb059..f6fe43b 100644 --- a/files_management/utils.py +++ b/files_management/utils.py @@ -36,6 +36,7 @@ def get_post(title): except FileNotFoundError: return None - +# Custome delete function def del_post(title): - pass + filename = f"posts/{title}.md" + default_storage.delete(filename) \ No newline at end of file diff --git a/posts/post_2.md b/posts/post_2.md index aeba1ac..6c88f23 100644 --- a/posts/post_2.md +++ b/posts/post_2.md @@ -1 +1 @@ -This is the content of post 2 \ No newline at end of file +Post 2 new content \ No newline at end of file