|
| 1 | +from datetime import datetime |
| 2 | +from fastapi import Depends, HTTPException, status, APIRouter, Response |
| 3 | +from pymongo.collection import ReturnDocument |
| 4 | +from app import schemas |
| 5 | +from app.database import Post |
| 6 | +from app.oauth2 import require_user |
| 7 | +from app.serializers.postSerializers import postEntity, postListEntity |
| 8 | +from bson.objectid import ObjectId |
| 9 | + |
| 10 | +router = APIRouter() |
| 11 | + |
| 12 | + |
| 13 | +@router.get('/') |
| 14 | +def get_posts(limit: int = 10, page: int = 1, search: str = '', user_id: str = Depends(require_user)): |
| 15 | + skip = (page - 1) * limit |
| 16 | + pipeline = [ |
| 17 | + {'$match': {}}, |
| 18 | + {'$lookup': {'from': 'users', 'localField': 'user', |
| 19 | + 'foreignField': '_id', 'as': 'user'}}, |
| 20 | + {'$unwind': '$user'}, |
| 21 | + { |
| 22 | + '$skip': skip |
| 23 | + }, { |
| 24 | + '$limit': limit |
| 25 | + } |
| 26 | + ] |
| 27 | + posts = postListEntity(Post.aggregate(pipeline)) |
| 28 | + return {'status': 'success', 'results': len(posts), 'posts': posts} |
| 29 | + |
| 30 | + |
| 31 | +@router.post('/', status_code=status.HTTP_201_CREATED) |
| 32 | +def create_post(post: schemas.CreatePostSchema, user_id: str = Depends(require_user)): |
| 33 | + post.user = ObjectId(user_id) |
| 34 | + post.created_at = datetime.utcnow() |
| 35 | + post.updated_at = post.created_at |
| 36 | + result = Post.insert_one(post.dict()) |
| 37 | + pipeline = [ |
| 38 | + {'$match': {'_id': result.inserted_id}}, |
| 39 | + {'$lookup': {'from': 'users', 'localField': 'user', |
| 40 | + 'foreignField': '_id', 'as': 'user'}}, |
| 41 | + {'$unwind': '$user'}, |
| 42 | + ] |
| 43 | + new_post = postListEntity(Post.aggregate(pipeline))[0] |
| 44 | + return new_post |
| 45 | + |
| 46 | + |
| 47 | +@router.put('/{id}') |
| 48 | +def update_post(id: str, payload: schemas.UpdatePostSchema, user_id: str = Depends(require_user)): |
| 49 | + if not ObjectId.is_valid(id): |
| 50 | + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, |
| 51 | + detail=f"Invalid id: {id}") |
| 52 | + updated_post = Post.find_one_and_update( |
| 53 | + {'_id': ObjectId(id)}, {'$set': payload.dict(exclude_none=True)}, return_document=ReturnDocument.AFTER) |
| 54 | + if not updated_post: |
| 55 | + raise HTTPException(status_code=status.HTTP_200_OK, |
| 56 | + detail=f'No post with this id: {id} found') |
| 57 | + return postEntity(updated_post) |
| 58 | + |
| 59 | + |
| 60 | +@router.get('/{id}') |
| 61 | +def get_post(id: str, user_id: str = Depends(require_user)): |
| 62 | + if not ObjectId.is_valid(id): |
| 63 | + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, |
| 64 | + detail=f"Invalid id: {id}") |
| 65 | + pipeline = [ |
| 66 | + {'$match': {'_id': ObjectId(id)}}, |
| 67 | + {'$lookup': {'from': 'users', 'localField': 'user', |
| 68 | + 'foreignField': '_id', 'as': 'user'}}, |
| 69 | + {'$unwind': '$user'}, |
| 70 | + ] |
| 71 | + post = postListEntity(Post.aggregate(pipeline))[0] |
| 72 | + if not post: |
| 73 | + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, |
| 74 | + detail=f"No post with this id: {id} found") |
| 75 | + return post |
| 76 | + |
| 77 | + |
| 78 | +@router.delete('/{id}') |
| 79 | +def delete_post(id: str, user_id: str = Depends(require_user)): |
| 80 | + if not ObjectId.is_valid(id): |
| 81 | + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, |
| 82 | + detail=f"Invalid id: {id}") |
| 83 | + post = Post.find_one_and_delete({'_id': ObjectId(id)}) |
| 84 | + if not post: |
| 85 | + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, |
| 86 | + detail=f'No post with this id: {id} found') |
| 87 | + return Response(status_code=status.HTTP_204_NO_CONTENT) |
0 commit comments