WalidAlHassan commited on
Commit
fbd1869
·
1 Parent(s): fdafec4
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ *.psd filter=lfs diff=lfs merge=lfs -text
Setup.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ 1. python -m venv venv
2
+ 2. venv\Scripts\activate.bat
3
+ 3. pip install fastapi[all]
__pycache__/main.cpython-312.pyc ADDED
Binary file (3.97 kB). View file
 
app/__init__.py ADDED
File without changes
app/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (177 Bytes). View file
 
app/__pycache__/main.cpython-312.pyc ADDED
Binary file (3.96 kB). View file
 
app/__pycache__/mainTest.cpython-312.pyc ADDED
Binary file (4.07 kB). View file
 
app/main.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi.params import Body
2
+ from fastapi import FastAPI, Response, status, HTTPException
3
+ from pydantic import BaseModel
4
+ from typing import Optional
5
+ from random import randrange
6
+
7
+ class po(BaseModel):
8
+ title:str
9
+ content:str
10
+ rating:Optional[int] = None
11
+
12
+
13
+ db = [
14
+ {"title":"title 1","content":"content 1","id":1},
15
+ {"title":"title 2","content":"content 2","id":2},
16
+ {"title":"title 3","content":"content 3","id":3}
17
+ ]
18
+
19
+ def find_id(id):
20
+ for i in db:
21
+ if i["id"] == id:
22
+ return i
23
+
24
+ def find_index_post(id):
25
+ for i, p in enumerate(db):
26
+ if p["id"] == id:
27
+ return i
28
+
29
+
30
+ app = FastAPI()
31
+
32
+
33
+ @app.get("/post") #Show all Post
34
+ async def all_post():
35
+ return {"message": db}
36
+
37
+ @app.post("/post", status_code=status.HTTP_201_CREATED) #Creat Post
38
+ async def creat_post(Post:po):
39
+ post_dict = Post.dict()
40
+ post_dict['id'] = randrange(0,999999)
41
+ db.append(post_dict)
42
+ return {"New Post": post_dict}
43
+
44
+ @app.get("/post/latests") #Recent Post
45
+ async def latests_post():
46
+ post = db[len(db)-1]
47
+ return {"Message": post}
48
+
49
+
50
+ @app.get("/post/{id}") #Post By ID
51
+ async def id_post(id: int, res: Response):
52
+ post = find_id(id)
53
+ if not post:
54
+ raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Post with id {id} was not found.")
55
+ # res.status_code = status.HTTP_404_NOT_FOUND
56
+ # return {"Massage": f"Post with id {id} was not found."}
57
+ return {"Message": post}
58
+
59
+ @app.delete("/post/{id}",status_code=status.HTTP_204_NO_CONTENT) #Delete Post by Id
60
+ async def delete_post(id:int):
61
+ index = find_index_post(id)
62
+ if index is None:
63
+ raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Data not found")
64
+ db.pop(index)
65
+ return Response(status_code=status.HTTP_204_NO_CONTENT)
66
+
67
+ @app.put("/post/{id}") #Update Post
68
+ async def update_post(id: int, post:po):
69
+ index = find_index_post(id)
70
+ if index is None:
71
+ raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Data not found")
72
+
73
+ post_dict = post.dict()
74
+ post_dict['id'] = id
75
+ db[index] = post_dict
76
+ return {"Data":db[index]}
77
+
78
+ # venv\Scripts\activate.bat
79
+ # python -m uvicorn app.mainTest:app --reload
app/mainTest.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Optional
2
+ from fastapi import FastAPI, status, Response, HTTPException
3
+ from pydantic import BaseModel
4
+ from random import randrange
5
+
6
+ class po(BaseModel):
7
+ title:str
8
+ content:str
9
+ rating:Optional[int]=None
10
+
11
+ db = [
12
+ {"title":"Title 1","content":"Content 1","id":1},
13
+ {"title":"Title 2","content":"Content 2","id":2},
14
+ {"title":"Title 3","content":"Content 3","id":3},
15
+ {"title":"Title 4","content":"Content 4","id":4},
16
+ {"title":"Title 5","content":"Content 5","id":5}
17
+ ]
18
+
19
+ def find_id(id):
20
+ for i in db:
21
+ if i["id"] == id:
22
+ return i
23
+
24
+ def find_index_post(id):
25
+ for i, p in enumerate(db):
26
+ if p["id"] == id:
27
+ print(p)
28
+ print(i)
29
+ return i
30
+
31
+ app = FastAPI()
32
+
33
+ @app.get("/post")
34
+ async def all_podt():
35
+ return {"Message":db}
36
+
37
+ @app.post("/post",status_code=status.HTTP_201_CREATED)
38
+ async def creat_post(post:po):
39
+ post_dict = post.dict()
40
+ post_dict['id']= randrange(1,500000)
41
+ db.append(post_dict)
42
+ return{"New Post":post_dict}
43
+
44
+ @app.get("/post/latests") #Recent Post
45
+ async def latests_post():
46
+ post = db[len(db)-1]
47
+ return {"Message": post}
48
+
49
+ @app.get("/post/{id}")
50
+ async def post_by_id(id:int, res:Response):
51
+ post = find_id(id)
52
+ if not post:
53
+ raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Post with id {id} not found.")
54
+ else:
55
+ return{"Post": post}
56
+
57
+ @app.delete("/post/{id}",status_code=status.HTTP_204_NO_CONTENT)
58
+ async def delete_post(id:int):
59
+ index = find_index_post(id)
60
+ if index is None:
61
+ raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,detail=f"Data not found.")
62
+ db.append(index)
63
+ return Response(status_code=status.HTTP_204_NO_CONTENT)
64
+
65
+ @app.put("/post/{id}")
66
+ async def update_post(id:int,post:po):
67
+ index = find_index_post(id)
68
+ if index in None:
69
+ raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Data not found")
70
+ post_dict = post.dict()
71
+ post_dict['id'] = id
72
+ db[index] = post_dict
73
+ return {"Data":db[index]}
74
+
75
+ # venv\Scripts\activate.bat
76
+ # python -m uvicorn app.mainTest:app --reload