diff --git a/.idea/digitalize_task2_backend.iml b/.idea/digitalize_task2_backend.iml
index e3bd986..d635bf9 100644
--- a/.idea/digitalize_task2_backend.iml
+++ b/.idea/digitalize_task2_backend.iml
@@ -16,7 +16,7 @@
-
+
diff --git a/.idea/misc.xml b/.idea/misc.xml
index 3b7bbcc..04e3f71 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -1,4 +1,7 @@
-
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..94a25f7
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
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..bbf9967 100644
--- a/config/urls.py
+++ b/config/urls.py
@@ -1,21 +1,13 @@
-"""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.FileManController import files_controller
+
+api = NinjaAPI()
+
+api.add_router('/', files_controller)
urlpatterns = [
path('admin/', admin.site.urls),
+ path('api/', api.urls)
]
diff --git a/files_management/FileManController.py b/files_management/FileManController.py
new file mode 100644
index 0000000..be2966c
--- /dev/null
+++ b/files_management/FileManController.py
@@ -0,0 +1,76 @@
+from files_management.utils import list_posts, get_post, save_post, del_post, exist_post
+# from django.shortcuts import render
+from ninja import Router, Schema
+
+
+# Create your views here.
+
+class FileBody(Schema):
+ """this Schema for posting new post"""
+ file_title: str
+ file_content: str = None
+
+
+class FileBodyUpdate(Schema):
+ """this Schema for update post"""
+ file_content: str = None
+
+
+files_controller = Router()
+
+
+# list all Posts
+@files_controller.get('/posts')
+def get_files(request):
+ return list_posts()
+
+
+@files_controller.get('/posts/{paraTitle}')
+def get_files_by_title(request, paraTitle: str):
+ try:
+ if get_post(title=paraTitle):
+ return get_post(title=paraTitle)
+ elif get_post(title=paraTitle) is None:
+ return {'Message': 'There is no post with that name'}
+ except:
+ return {'Message': 'There is an error occurs'}
+
+# this method for posting new file
+@files_controller.post('/posts')
+def post_file(request, payload: FileBody):
+ try:
+ if payload.file_title is not None:
+ save_post(title=payload.file_title, content=payload.file_content)
+ return {'Message': 'File Saved Successfully'}
+ else:
+ return {'Message': 'Please provide file name'}
+ except:
+ return {'Message': 'There is an error occurs'}
+
+
+# this method for updating existing file
+@files_controller.put('/posts/{paraTitle}')
+def update_file(request, payload: FileBodyUpdate, paraTitle):
+ try:
+ if exist_post(paraTitle):
+ if paraTitle is not None and payload.file_content is not None:
+ save_post(title=paraTitle, content=payload.file_content)
+ return {'Message': 'File Updated Successfully'}
+ else:
+ return {'Message': 'Please provide file name and file content to update'}
+ else:
+ return {'Message': 'There is no file with that name'}
+ except:
+ return {'Message': 'There is an error occurs'}
+
+
+@files_controller.delete('/posts/{paraTitle}')
+def delete_file(request, paraTitle):
+ try:
+ if paraTitle is not None:
+ if del_post(paraTitle):
+ return {'Message': 'File deleted Successfully'}
+ else:
+ return {'Message': 'There is no file with that name, Please provide a correct file name to delete it'}
+ except:
+ return {'Message': 'There is an error occurs'}
\ No newline at end of file
diff --git a/files_management/controller.py b/files_management/controller.py
deleted file mode 100644
index 45a329d..0000000
--- a/files_management/controller.py
+++ /dev/null
@@ -1,2 +0,0 @@
-from django.shortcuts import render
-# Create your views here.
diff --git a/files_management/utils.py b/files_management/utils.py
index fbcb059..a25abb7 100644
--- a/files_management/utils.py
+++ b/files_management/utils.py
@@ -38,4 +38,21 @@ def get_post(title):
def del_post(title):
- pass
+ """
+ delete file if exists
+ """
+ filename = f"posts/{title}.md"
+ if default_storage.exists(filename):
+ default_storage.delete(filename)
+ else:
+ return False
+ return {'Message': 'File deleted Successfully'}
+
+
+def exist_post(title):
+ """Check if file exists"""
+ filename = f"posts/{title}.md"
+ if default_storage.exists(filename):
+ return True
+ else:
+ return False
diff --git a/posts/post_1.md b/posts/post_1.md
index a2838c6..460b537 100644
--- a/posts/post_1.md
+++ b/posts/post_1.md
@@ -1 +1 @@
-This is a content of post 1
\ No newline at end of file
+This is the content of post 1
\ No newline at end of file
diff --git a/requirements.txt b/requirements.txt
index a9724da..e69de29 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,6 +0,0 @@
-asgiref==3.5.2
-Django==4.1.2
-django-ninja==0.19.1
-pydantic==1.10.2
-sqlparse==0.4.3
-typing_extensions==4.4.0