-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from sinasezza/sinasezza
feat(models, routes): models changed and some routes for send messag…
- Loading branch information
Showing
14 changed files
with
225 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
from bson import ObjectId | ||
from pydantic import BaseModel | ||
from datetime import datetime, timezone | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
from chatApp.utils.object_id import PydanticObjectId | ||
|
||
|
||
class Message(BaseModel): | ||
_id: ObjectId | ||
user_id: ObjectId | ||
room_id: str | ||
content: str | ||
# created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc)) | ||
user_id: PydanticObjectId | ||
room_id: str | None = Field(default=None) | ||
content: str = Field(default=None) | ||
media: str = Field(default=None) | ||
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc)) | ||
|
||
|
||
class MessageInDB(Message): | ||
id: PydanticObjectId = Field(alias="_id") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from datetime import datetime, timezone | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
from chatApp.utils.object_id import PydanticObjectId | ||
|
||
|
||
class Room(BaseModel): | ||
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc)) | ||
members: list[PydanticObjectId] | ||
|
||
|
||
class RoomInDB(Room): | ||
id: PydanticObjectId = Field(alias="_id") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,20 @@ | ||
from bson import ObjectId | ||
from pydantic import BaseModel | ||
from datetime import datetime, timezone | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
from chatApp.utils.object_id import PydanticObjectId | ||
|
||
|
||
class User(BaseModel): | ||
_id: ObjectId | ||
username: str | ||
email: str | ||
hashed_password: str | ||
is_active: bool = True | ||
is_admin: bool = False | ||
# created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc)) | ||
# updated_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc)) | ||
# last_login: datetime = None | ||
# last_logout: datetime = None | ||
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc)) | ||
updated_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc)) | ||
last_login: datetime | None = None | ||
|
||
|
||
class UserInDB(User): | ||
id: PydanticObjectId = Field(alias="_id") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,70 @@ | ||
from fastapi import APIRouter | ||
from collections.abc import Mapping | ||
from typing import Any | ||
|
||
from bson import ObjectId | ||
from fastapi import APIRouter, Depends, HTTPException | ||
from motor.motor_asyncio import AsyncIOMotorCollection, AsyncIOMotorCursor | ||
|
||
from chatApp.config import auth | ||
from chatApp.config.database import get_messages_collection | ||
from chatApp.models.message import MessageInDB | ||
from chatApp.models.user import UserInDB | ||
from chatApp.schemas.message import MessageCreateSchema | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get("/all-messages", response_model=list[MessageInDB]) | ||
async def get_all_messages(): | ||
messages_collection: AsyncIOMotorCollection = get_messages_collection() | ||
|
||
cursor: AsyncIOMotorCursor = messages_collection.find() | ||
messages_dicts: list[Mapping[str, Any]] = await cursor.to_list(length=None) | ||
messages: list[MessageInDB] = [ | ||
MessageInDB(**message_dict) for message_dict in messages_dicts | ||
] | ||
|
||
return messages | ||
|
||
|
||
@router.get("/messages", response_model=list[MessageInDB]) | ||
async def get_messages(user: UserInDB = Depends(auth.get_current_user)): | ||
messages_collection: AsyncIOMotorCollection = get_messages_collection() | ||
|
||
cursor: AsyncIOMotorCursor = messages_collection.find( | ||
{"user_id": ObjectId(user.id)} | ||
) | ||
messages_dicts: list[Mapping[str, Any]] = await cursor.to_list(length=None) | ||
messages: list[MessageInDB] = [ | ||
MessageInDB(**message_dict) for message_dict in messages_dicts | ||
] | ||
|
||
return messages | ||
|
||
|
||
@router.get("/message/{message_id}", response_model=MessageInDB) | ||
async def get_message(message_id: str, user: UserInDB = Depends(auth.get_current_user)): | ||
messages_collection: AsyncIOMotorCollection = get_messages_collection() | ||
|
||
message = await messages_collection.find_one( | ||
{"_id": ObjectId(message_id), "user_id": ObjectId(user.id)} | ||
) | ||
|
||
if message is None: | ||
raise HTTPException(status_code=404, detail="Message not found") | ||
|
||
return MessageInDB(**message) | ||
|
||
|
||
@router.post("/message", response_model=MessageInDB) | ||
async def create_message( | ||
message: MessageCreateSchema, user: UserInDB = Depends(auth.get_current_user) | ||
): | ||
messages_collection = get_messages_collection() | ||
|
||
message_dict = message.model_dump() | ||
message_dict["user_id"] = user.id | ||
|
||
result = await messages_collection.insert_one(message_dict) | ||
|
||
return MessageInDB(**message_dict, _id=result.inserted_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.