ABAO77 commited on
Commit
f0318be
·
verified ·
1 Parent(s): 97c4d05

Upload 157 files

Browse files
src/apis/controllers/__pycache__/post_controller.cpython-311.pyc CHANGED
Binary files a/src/apis/controllers/__pycache__/post_controller.cpython-311.pyc and b/src/apis/controllers/__pycache__/post_controller.cpython-311.pyc differ
 
src/apis/controllers/post_controller.py CHANGED
@@ -121,46 +121,76 @@ async def list_all_posts_controller(user_id: str):
121
  return {"status": "error", "message": str(e)}
122
 
123
 
124
- async def list_posts_destination_controller(destination: str):
125
  try:
126
- posts = await PostCRUD.read({"destination_id": destination})
127
- user_ids = [post.get("user_id") for post in posts]
128
- user_infos = await gather(
129
- *[UserCRUD.find_by_id(user_id) for user_id in user_ids]
130
- )
 
 
 
131
  user_info_map = {
132
- user_info.get("_id"): user_info for user_info in user_infos if user_info
 
 
 
 
133
  }
134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  serialized_posts = []
136
  for post in posts:
137
- user_id = post.get("user_id")
138
- user_info = user_info_map.get(user_id)
139
  serialized_post = {
140
- "id": serialize_datetime(post.get("_id")),
141
  "content": post.get("content"),
142
- "destination_id": post.get("destination_id"),
143
- "comment_ids": post.get("comment_ids", []),
144
- "like": post.get("like", []),
 
 
145
  "picture": post.get("picture", []),
146
  "created_at": serialize_datetime(post.get("created_at")),
147
  "updated_at": serialize_datetime(post.get("updated_at")),
148
- "user_info": (
149
- {
150
- "name": user_info.get("name"),
151
- "picture": user_info.get("picture"),
152
- }
153
- if user_info
154
- else None
155
- ),
156
  }
157
  serialized_posts.append(serialized_post)
 
158
  return {"status": "success", "message": serialized_posts}
 
159
  except Exception as e:
160
- logger.error(f"Error listing posts: {str(e)}")
161
  return {"status": "error", "message": str(e)}
162
 
163
 
 
164
  async def update_a_post_controller(
165
  user_id: str, post_id: str, content: str, destination_id: str
166
  ) -> Dict:
 
121
  return {"status": "error", "message": str(e)}
122
 
123
 
124
+ async def list_posts_by_destination_controller(destination_id: str, user_id: str):
125
  try:
126
+ # Filtered posts only for the given destination_id
127
+ posts = await PostCRUD.find_many({"destination_id": destination_id})
128
+ if not posts:
129
+ return {"status": "success", "message": []}
130
+
131
+ # Get unique user_ids
132
+ user_ids = list({post.get("user_id") for post in posts})
133
+ user_infos = await gather(*[UserCRUD.find_by_id(uid) for uid in user_ids])
134
  user_info_map = {
135
+ info.get("_id"): {
136
+ "name": info.get("name"),
137
+ "picture": info.get("picture"),
138
+ }
139
+ for info in user_infos if info
140
  }
141
 
142
+ # Get destination name
143
+ destination_info = await DestinationCRUD.find_by_id(destination_id)
144
+ destination_name = destination_info.get("name") if destination_info else None
145
+
146
+ # Reactions by current user for these posts
147
+ formatted_user_reactions_map = {}
148
+ if user_id:
149
+ all_post_ids = [serialize_datetime(post.get("_id")) for post in posts]
150
+ reactions = await gather(
151
+ *[
152
+ ReactionCRUD.read_one({"user_id": user_id, "post_id": post_id})
153
+ for post_id in all_post_ids
154
+ ]
155
+ )
156
+ for reaction in reactions:
157
+ if reaction:
158
+ post_id = reaction.get("post_id")
159
+ formatted_user_reactions_map[post_id] = {
160
+ "id": serialize_datetime(reaction.get("_id")),
161
+ "post_id": post_id,
162
+ "user_id": reaction.get("user_id"),
163
+ "reaction_type": reaction.get("type"),
164
+ }
165
+
166
+ # Final serialization
167
  serialized_posts = []
168
  for post in posts:
169
+ post_id = serialize_datetime(post.get("_id"))
170
+ uid = post.get("user_id")
171
  serialized_post = {
172
+ "id": post_id,
173
  "content": post.get("content"),
174
+ "destination_id": destination_id,
175
+ "destination_name": destination_name,
176
+ "comment_count": post.get("comment_count", []),
177
+ "reaction_count": post.get("reaction_count", []),
178
+ "current_user_reaction": formatted_user_reactions_map.get(post_id),
179
  "picture": post.get("picture", []),
180
  "created_at": serialize_datetime(post.get("created_at")),
181
  "updated_at": serialize_datetime(post.get("updated_at")),
182
+ "user_info": user_info_map.get(uid),
 
 
 
 
 
 
 
183
  }
184
  serialized_posts.append(serialized_post)
185
+
186
  return {"status": "success", "message": serialized_posts}
187
+
188
  except Exception as e:
189
+ logger.error(f"Error listing posts by destination: {str(e)}")
190
  return {"status": "error", "message": str(e)}
191
 
192
 
193
+
194
  async def update_a_post_controller(
195
  user_id: str, post_id: str, content: str, destination_id: str
196
  ) -> Dict:
src/apis/routes/__pycache__/post_router.cpython-311.pyc CHANGED
Binary files a/src/apis/routes/__pycache__/post_router.cpython-311.pyc and b/src/apis/routes/__pycache__/post_router.cpython-311.pyc differ
 
src/apis/routes/post_router.py CHANGED
@@ -8,7 +8,7 @@ from src.apis.middlewares.auth_middleware import get_current_user
8
  from src.apis.controllers.post_controller import (
9
  create_a_post_controller,
10
  list_all_posts_controller,
11
- list_posts_destination_controller,
12
  get_a_post_controller,
13
  update_a_post_controller,
14
  delete_a_post_controller,
@@ -80,24 +80,16 @@ async def list_all_posts(user_id: Optional[str] = None):
80
  return JSONResponse(content=result, status_code=200)
81
 
82
 
83
- class BodyListPost(BaseDocument):
84
- destination_id: str = Field("", description="Destination's id")
85
-
86
- class Config:
87
- json_schema_extra = {
88
- "example": {
89
- "destination_id": "1234567890",
90
- }
91
- }
92
-
93
-
94
- @router.post("/list_post_destination", status_code=status.HTTP_200_OK)
95
- async def list_all_posts_destination(body: BodyListPost):
96
  # result = await get_key_redis("all_posts")
97
  result = None
98
  result = eval(result) if result else None
99
  if not result:
100
- result = await list_posts_destination_controller(body.destination_id)
101
  # background_tasks.add_task(set_key_redis, "all_posts", str(result),20)
102
  if result["status"] == "error":
103
  return JSONResponse(content=result, status_code=404)
 
8
  from src.apis.controllers.post_controller import (
9
  create_a_post_controller,
10
  list_all_posts_controller,
11
+ list_posts_by_destination_controller,
12
  get_a_post_controller,
13
  update_a_post_controller,
14
  delete_a_post_controller,
 
80
  return JSONResponse(content=result, status_code=200)
81
 
82
 
83
+ @router.get("/list_post_destination", status_code=status.HTTP_200_OK)
84
+ async def list_all_posts_destination(
85
+ destination_id: str,
86
+ user_id: Optional[str] = None,
87
+ ):
 
 
 
 
 
 
 
 
88
  # result = await get_key_redis("all_posts")
89
  result = None
90
  result = eval(result) if result else None
91
  if not result:
92
+ result = await list_posts_by_destination_controller(destination_id, user_id)
93
  # background_tasks.add_task(set_key_redis, "all_posts", str(result),20)
94
  if result["status"] == "error":
95
  return JSONResponse(content=result, status_code=404)