@@ -20,6 +20,7 @@ def _serialize_post(self, post: Post, author: Author) -> PostRead:
2020 user_id = post .user_id ,
2121 title = post .title ,
2222 content = post .content ,
23+ type = post .type ,
2324 region_id = post .region_id ,
2425 post_imageURLs = post .post_imageURLs ,
2526 created_at = post .created_at ,
@@ -46,13 +47,19 @@ async def create_post(
4647 ** post_data .dict (),
4748 user_id = user .id
4849 )
50+ if post .type == "disaster" :
51+ user .point += 10
52+ elif post .type == "normal" :
53+ user .point += 5
54+ self .session .add (user )
4955 self .session .add (post )
5056 self .session .commit ()
5157 self .session .refresh (post )
5258 author = Author (
5359 id = user .id ,
5460 username = user .username ,
55- profile_imageURL = user .profile_imageURL
61+ profile_imageURL = user .profile_imageURL ,
62+ point = user .point
5663 )
5764 return self ._serialize_post (post , author )
5865
@@ -105,7 +112,8 @@ async def update_post(
105112 author = Author (
106113 id = user .id ,
107114 username = user .username ,
108- profile_imageURL = user .profile_imageURL
115+ profile_imageURL = user .profile_imageURL ,
116+ point = user .point
109117 )
110118 return self ._serialize_post (post , author )
111119
@@ -123,10 +131,12 @@ def delete_post(self, post_id: int, user: User):
123131 self .session .commit ()
124132 return {"ok" : True }
125133
126- def list_posts (self , term : str = None , region_ids : list = None , sort : str = None ):
134+ def list_posts (self , term : str = None , type : str = None , region_ids : list = None , sort : str = None ):
127135 query = select (Post ).options (selectinload (Post .user ))
128136 if term :
129137 query = query .where (Post .title .contains (term ) | Post .content .contains (term ))
138+ if type :
139+ query = query .where (Post .type == type )
130140 if region_ids :
131141 query = query .where (Post .region_id .in_ (region_ids ))
132142 if sort == "latest" :
@@ -140,7 +150,8 @@ def list_posts(self, term: str = None, region_ids: list = None, sort: str = None
140150 Author (
141151 id = post .user .id ,
142152 username = post .user .username ,
143- profile_imageURL = post .user .profile_imageURL
153+ profile_imageURL = post .user .profile_imageURL ,
154+ point = post .user .point
144155 )
145156 )
146157 for post in posts
@@ -151,7 +162,8 @@ def list_user_posts(self, user: User):
151162 author = Author (
152163 id = user .id ,
153164 username = user .username ,
154- profile_imageURL = user .profile_imageURL
165+ profile_imageURL = user .profile_imageURL ,
166+ point = user .point
155167 )
156168 return [self ._serialize_post (post , author ) for post in posts ]
157169
@@ -168,6 +180,7 @@ def increment_view_count(self, post_id: int):
168180 author = Author (
169181 id = post .user .id ,
170182 username = post .user .username ,
171- profile_imageURL = post .user .profile_imageURL
183+ profile_imageURL = post .user .profile_imageURL ,
184+ point = post .user .point
172185 )
173186 return self ._serialize_post (post , author )
0 commit comments