diff --git a/config/settings.py b/config/settings.py index e332a2b..df8f25d 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..40299eb 100644 --- a/config/urls.py +++ b/config/urls.py @@ -1,21 +1,20 @@ -"""config URL Configuration -The `urlpatterns` list routes URLs to views. For more information please see: - https://docs.djangoproject.com/en/4.1/topics/http/urls/ -Examples: -Function views - 1. Add an import: from my_app import views - 2. Add a URL to urlpatterns: path('', views.home, name='home') -Class-based views - 1. Add an import: from other_app.views import Home - 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') -Including another URLconf - 1. Import the include() function: from django.urls import include, path - 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) -""" from django.contrib import admin from django.urls import path +from ninja import NinjaAPI +from files_management.controller import Posts_Conrtoller +from files_management.utils import list_posts +# from files_management.utils import utils + +#from account.views import products_controller,users + +api=NinjaAPI() + +api.add_router('/Posts',Posts_Conrtoller) + + urlpatterns = [ path('admin/', admin.site.urls), + path('api/',api.urls), ] diff --git a/files_management/controller.py b/files_management/controller.py index 45a329d..e3cbf3d 100644 --- a/files_management/controller.py +++ b/files_management/controller.py @@ -1,2 +1,55 @@ +from gc import get_objects +from turtle import title from django.shortcuts import render # Create your views here. +from ninja import Router,Schema +from files_management.utils import list_posts,save_post,get_post,del_post + +class BodyIn(Schema): + title :str + content:str + +Posts_Conrtoller=Router() + + +# to list all posts +@Posts_Conrtoller.get('get') +def list_all_posts(request): + return list_posts() + + +# to retrieve a certain post + +@Posts_Conrtoller.get('/{title}') +def retrieve_post(request ,title): + return get_post(title) + + + + +# to create a new post +@Posts_Conrtoller.post('') +def create_post(request,title,content): + return save_post(title,content) + + + +# to update a certain post +# @Posts_Conrtoller.put('') +# def update_post(request,payload:BodyIn()): +# pass + # payload.title=title + # payload.content=content + + # filename = f"posts/{title}.md" + # if default_storage.exists(filename): + # default_storage.add(filename,ContentFile(content)) + # default_storage.save(filename, ContentFile(content)) + # return{'Message':f'Post {title} Created Successfully'} + + + +# to delete a certain post +@Posts_Conrtoller.delete('') +def delet_post(request,title): + return del_post(title) diff --git a/files_management/utils.py b/files_management/utils.py index fbcb059..3155489 100644 --- a/files_management/utils.py +++ b/files_management/utils.py @@ -23,6 +23,7 @@ def save_post(title, content): if default_storage.exists(filename): default_storage.delete(filename) default_storage.save(filename, ContentFile(content)) + return{'Message':f'Post {title} Created Successfully'} def get_post(title): @@ -37,5 +38,17 @@ def get_post(title): return None + + + def del_post(title): - pass + + filename = f"posts/{title}.md" + if default_storage.exists(filename): + default_storage.delete(filename) + else: + return {'Message':'The post cannot be deleted because it does not exist'} + return {'Message':f'Post {title} deleted Successfully'} + + + \ No newline at end of file diff --git a/posts/post_3.md b/posts/post_3.md new file mode 100644 index 0000000..e8814c7 --- /dev/null +++ b/posts/post_3.md @@ -0,0 +1 @@ +This is the content of post 3 \ No newline at end of file