ngay 2-8-2022

This commit is contained in:
2022-08-02 14:25:51 +07:00
parent 9e3a18756c
commit f541dd9956
15 changed files with 963 additions and 325 deletions

View File

@@ -11,7 +11,11 @@ from src.settings import db, ACCESS_TOKEN_EXPIRE_MINUTES
from ..models.history_find import *
from typing import List
from datetime import datetime, timedelta
from ..models.post import (
PostModel,
UpdatePostModel,
ShowPostModel
)
from ..models.models import (
UserModel,
ShowUserModel,
@@ -27,31 +31,49 @@ import re
history = APIRouter()
##############################POST###############################################
@history.post("/create_history", response_description="Add new user", response_model=HistoryFindModel)
async def create_history(history: HistoryFindModel, current_user: ShowUserModel = Depends(get_current_user)):
datetime_now = datetime.now()
post.created_at = datetime_now.strftime("%m/%d/%y %H:%M:%S")
post = jsonable_encoder(post)
new_post = await db["history_find"].insert_one(post)
created = await db["history_find"].find_one({"_id": new_post.inserted_id})
return JSONResponse(status_code=status.HTTP_201_CREATED, content=created)
@history.post("/create_history", response_description="history", response_model=HistoryFindModel)
async def create_history(history: HistoryFindModel):
datetime_now = datetime.now()
post.created_at = datetime_now.strftime("%m/%d/%y %H:%M:%S")
post = jsonable_encoder(post)
new_post = await db["history_find"].insert_one(post)
created = await db["history_find"].find_one({"_id": new_post.inserted_id})
return JSONResponse(status_code=status.HTTP_201_CREATED, content=created)
@history.get(
"/list_history", response_description="List all posts", response_model=List[HistoryFindModel]
)
async def list_post(current_user: ShowUserModel = Depends(get_current_user)):
async def list_post():
history_find = await db["history_find"].find().to_list(1000)
return history_find
# @post.get(
# "/get_post_by_name", response_description="Get a single posot", response_model=PostModel
# )
# async def show_post(id: str):
# print(id)
# post = await db["posts"].find_one({"_id": id})
# print(post)
# if post is not None:
# return post
# else:
# return None
@ history.get("/list_history_by_user", response_description="Get list Posts viewed", response_model=List[HistoryFindModel])
async def get_list_post_view_by_username(username: str, current_user: ShowUserModel = Depends(get_current_user)):
history_find = await db["history_find"].find({"username": current_user["username"]}).to_list(10)
return history_find
# @ history.get("/list_post_by_user", response_description="Get list Posts viewed", response_model=List[ShowPostModel])
# async def get_list_post_view_by_username(username: str, current_user: ShowUserModel = Depends(get_current_user)):
# history_find = await db["history_find"].find({"username": username}).to_list(10)
# post_view = []
# for dt in history_find:
# print(dt["post_id"])
# post = await db["posts"].find_one({"_id": dt["post_id"]})
# post_view.append(post)
# return post_view
@history.get("/list_post_by_user", response_description="Get list Posts viewed", response_model=List[HistoryFindByUserModel])
async def get_list_post_view_by_username(current_user: ShowUserModel = Depends(get_current_user)):
history_find = await db["history_find"].find({"username": current_user["username"]}).to_list(10)
post_view = []
for dt in history_find:
print(dt["post_id"])
post = await db["posts"].find_one({"_id": dt["post_id"]})
dt["post"] = post
return history_find