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

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
@@ -1,9 +1,11 @@
1
  from typing import Dict
2
  from src.utils.mongo import PostCRUD
3
  from src.utils.logger import logger
4
- from src.utils.mongo import UserCRUD, ReactionCRUD
5
  from asyncio import gather
6
  from src.utils.helper import call_external_api, serialize_datetime
 
 
7
 
8
 
9
  async def create_a_post_controller(
@@ -57,64 +59,63 @@ async def get_a_post_controller(post_id: str) -> Dict:
57
  async def list_all_posts_controller(user_id: str):
58
  try:
59
  posts = await PostCRUD.find_all()
60
- user_ids = [post.get("user_id") for post in posts]
61
- user_infos = await gather(
62
- *[UserCRUD.find_by_id(user_id) for user_id in user_ids]
63
- )
64
-
65
  user_info_map = {
66
- user_info.get("_id"): user_info for user_info in user_infos if user_info
 
 
 
 
 
67
  }
68
- all_post_ids = {serialize_datetime(post.get("_id")) for post in posts}
69
-
70
- formatted_user_reactions = []
 
 
 
 
 
71
  if user_id:
 
72
  reactions = await gather(
73
  *[
74
  ReactionCRUD.read_one({"user_id": user_id, "post_id": post_id})
75
  for post_id in all_post_ids
76
  ]
77
  )
78
- formatted_user_reactions = [
79
- {
80
- "id": serialize_datetime(reaction.get("_id", None)),
81
- "post_id": reaction.get("post_id", None),
82
- "user_id": reaction.get("user_id", None),
83
- "reaction_type": reaction.get("type", None),
84
- }
85
- for reaction in reactions
86
- if reaction is not None # Ensure only non-None reactions are processed
87
- ]
88
-
89
  serialized_posts = []
90
- for index, post in enumerate(posts):
91
- user_id = post.get("user_id")
92
- user_info = user_info_map.get(user_id)
 
93
  serialized_post = {
94
- "id": serialize_datetime(post.get("_id")),
95
  "content": post.get("content"),
96
- "destination_id": post.get("destination_id"),
 
97
  "comment_count": post.get("comment_count", []),
98
  "reaction_count": post.get("reaction_count", []),
99
- "current_user_reaction": (
100
- formatted_user_reactions[index]
101
- if formatted_user_reactions
102
- else None
103
- ),
104
  "picture": post.get("picture", []),
105
  "created_at": serialize_datetime(post.get("created_at")),
106
  "updated_at": serialize_datetime(post.get("updated_at")),
107
- "user_info": (
108
- {
109
- "name": user_info.get("name"),
110
- "picture": user_info.get("picture"),
111
- }
112
- if user_info
113
- else None
114
- ),
115
  }
116
  serialized_posts.append(serialized_post)
 
117
  return {"status": "success", "message": serialized_posts}
 
118
  except Exception as e:
119
  logger.error(f"Error listing posts: {str(e)}")
120
  return {"status": "error", "message": str(e)}
 
1
  from typing import Dict
2
  from src.utils.mongo import PostCRUD
3
  from src.utils.logger import logger
4
+ from src.utils.mongo import UserCRUD, ReactionCRUD, DestinationCRUD
5
  from asyncio import gather
6
  from src.utils.helper import call_external_api, serialize_datetime
7
+ from datetime import datetime
8
+ from bson import ObjectId
9
 
10
 
11
  async def create_a_post_controller(
 
59
  async def list_all_posts_controller(user_id: str):
60
  try:
61
  posts = await PostCRUD.find_all()
62
+ user_ids = list({post.get("user_id") for post in posts})
63
+ user_infos = await gather(*[UserCRUD.find_by_id(uid) for uid in user_ids])
 
 
 
64
  user_info_map = {
65
+ info.get("_id"): {
66
+ "name": info.get("name"),
67
+ "picture": info.get("picture"),
68
+ }
69
+ for info in user_infos
70
+ if info
71
  }
72
+ destination_ids = list({post.get("destination_id") for post in posts})
73
+ destination_infos = await gather(
74
+ *[DestinationCRUD.find_by_id(did) for did in destination_ids]
75
+ )
76
+ destination_info_map = {
77
+ info.get("_id"): info.get("name") for info in destination_infos if info
78
+ }
79
+ formatted_user_reactions_map = {}
80
  if user_id:
81
+ all_post_ids = [serialize_datetime(post.get("_id")) for post in posts]
82
  reactions = await gather(
83
  *[
84
  ReactionCRUD.read_one({"user_id": user_id, "post_id": post_id})
85
  for post_id in all_post_ids
86
  ]
87
  )
88
+ for reaction in reactions:
89
+ if reaction:
90
+ post_id = reaction.get("post_id")
91
+ formatted_user_reactions_map[post_id] = {
92
+ "id": serialize_datetime(reaction.get("_id")),
93
+ "post_id": post_id,
94
+ "user_id": reaction.get("user_id"),
95
+ "reaction_type": reaction.get("type"),
96
+ }
 
 
97
  serialized_posts = []
98
+ for post in posts:
99
+ post_id = serialize_datetime(post.get("_id"))
100
+ uid = post.get("user_id")
101
+ dest_id = post.get("destination_id")
102
  serialized_post = {
103
+ "id": post_id,
104
  "content": post.get("content"),
105
+ "destination_id": dest_id,
106
+ "destination_name": destination_info_map.get(dest_id),
107
  "comment_count": post.get("comment_count", []),
108
  "reaction_count": post.get("reaction_count", []),
109
+ "current_user_reaction": formatted_user_reactions_map.get(post_id),
 
 
 
 
110
  "picture": post.get("picture", []),
111
  "created_at": serialize_datetime(post.get("created_at")),
112
  "updated_at": serialize_datetime(post.get("updated_at")),
113
+ "user_info": user_info_map.get(uid),
 
 
 
 
 
 
 
114
  }
115
  serialized_posts.append(serialized_post)
116
+
117
  return {"status": "success", "message": serialized_posts}
118
+
119
  except Exception as e:
120
  logger.error(f"Error listing posts: {str(e)}")
121
  return {"status": "error", "message": str(e)}