80 lines
2.9 KiB
Python
80 lines
2.9 KiB
Python
from fastapi import (
|
|
APIRouter,
|
|
Depends,
|
|
status,
|
|
HTTPException
|
|
)
|
|
from fastapi.responses import JSONResponse
|
|
from fastapi.encoders import jsonable_encoder
|
|
from fastapi.security import OAuth2PasswordRequestForm
|
|
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,
|
|
UpdateUserModel
|
|
)
|
|
from ..dependecies import (
|
|
get_current_user,
|
|
authenticate_user,
|
|
create_access_token,
|
|
get_password_hash
|
|
)
|
|
import re
|
|
|
|
history = APIRouter()
|
|
##############################POST###############################################
|
|
|
|
|
|
@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():
|
|
history_find = await db["history_find"].find().to_list(1000)
|
|
return history_find
|
|
|
|
|
|
@ 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
|