Skip to content

Commit b9a3276

Browse files
authored
Merge pull request #155 from cuappdev/ashley/release
Fixing create post bug
2 parents 39968c9 + 46b1be1 commit b9a3276

7 files changed

Lines changed: 325 additions & 53 deletions

File tree

src/api/controllers/PostController.ts

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,41 +67,70 @@ export class PostController {
6767
}
6868

6969
@Post('filterByCategories/')
70-
async filterPostsByCategories(@CurrentUser() user: UserModel, @Body() filterPostsRequest: FilterPostsRequest): Promise<GetPostsResponse> {
71-
return { posts: await this.postService.filterPostsByCategories(user, filterPostsRequest) };
70+
async filterPostsByCategories(
71+
@CurrentUser() user: UserModel,
72+
@Body() filterPostsRequest: FilterPostsRequest,
73+
@QueryParam('page', { required: false }) page: number = 1,
74+
@QueryParam('limit', { required: false }) limit: number = 10
75+
): Promise<GetPostsResponse> {
76+
return { posts: await this.postService.filterPostsByCategories(user, filterPostsRequest, page, limit) };
7277
}
7378

7479
@Post('filterByPrice/')
75-
async filterPostsByPrice(@CurrentUser() user: UserModel, @Body() filterPostsByPriceRequest: FilterPostsByPriceRequest): Promise<GetPostsResponse> {
76-
return { posts: await this.postService.filterPostsByPrice(user, filterPostsByPriceRequest) };
80+
async filterPostsByPrice(
81+
@CurrentUser() user: UserModel,
82+
@Body() filterPostsByPriceRequest: FilterPostsByPriceRequest,
83+
@QueryParam('page', { required: false }) page: number = 1,
84+
@QueryParam('limit', { required: false }) limit: number = 10
85+
): Promise<GetPostsResponse> {
86+
return { posts: await this.postService.filterPostsByPrice(user, filterPostsByPriceRequest, page, limit) };
7787
}
7888

7989
@Post('filterPriceHighToLow/')
80-
async filterPriceHighToLow(@CurrentUser() user: UserModel): Promise<GetPostsResponse> {
81-
return { posts: await this.postService.filterPriceHighToLow(user) };
90+
async filterPriceHighToLow(
91+
@CurrentUser() user: UserModel,
92+
@QueryParam('page', { required: false }) page: number = 1,
93+
@QueryParam('limit', { required: false }) limit: number = 10
94+
): Promise<GetPostsResponse> {
95+
return { posts: await this.postService.filterPriceHighToLow(user, page, limit) };
8296
}
8397

8498
@Post('filterPriceLowToHigh/')
85-
async filterPriceLowToHigh(@CurrentUser() user: UserModel): Promise<GetPostsResponse> {
86-
return { posts: await this.postService.filterPriceLowToHigh(user) };
99+
async filterPriceLowToHigh(
100+
@CurrentUser() user: UserModel,
101+
@QueryParam('page', { required: false }) page: number = 1,
102+
@QueryParam('limit', { required: false }) limit: number = 10
103+
): Promise<GetPostsResponse> {
104+
return { posts: await this.postService.filterPriceLowToHigh(user, page, limit) };
87105
}
88106

89107
@Post('filterNewlyListed/')
90-
async filterNewlyListed(@CurrentUser() user: UserModel): Promise<GetPostsResponse> {
91-
return { posts: await this.postService.filterNewlyListed(user) };
108+
async filterNewlyListed(
109+
@CurrentUser() user: UserModel,
110+
@QueryParam('page', { required: false }) page: number = 1,
111+
@QueryParam('limit', { required: false }) limit: number = 10
112+
): Promise<GetPostsResponse> {
113+
return { posts: await this.postService.filterNewlyListed(user, page, limit) };
92114
}
93115

94116
@Post('filterByCondition/')
95-
async filterByCondition(@CurrentUser() user: UserModel, @Body() filterPostsByConditionRequest: FilterPostsByConditionRequest): Promise<GetPostsResponse> {
96-
return { posts: await this.postService.filterByCondition(user, filterPostsByConditionRequest) };
117+
async filterByCondition(
118+
@CurrentUser() user: UserModel,
119+
@Body() filterPostsByConditionRequest: FilterPostsByConditionRequest,
120+
@QueryParam('page', { required: false }) page: number = 1,
121+
@QueryParam('limit', { required: false }) limit: number = 10
122+
): Promise<GetPostsResponse> {
123+
return { posts: await this.postService.filterByCondition(user, filterPostsByConditionRequest, page, limit) };
97124
}
98125

99126
@Post('filter/')
100127
async filterPosts(
101128
@CurrentUser() user: UserModel,
102-
@Body() filterPostsUnifiedRequest: FilterPostsUnifiedRequest
129+
@Body() filterPostsUnifiedRequest: FilterPostsUnifiedRequest,
130+
@QueryParam('page', { required: false }) page: number = 1,
131+
@QueryParam('limit', { required: false }) limit: number = 10
103132
): Promise<GetPostsResponse> {
104-
return { posts: await this.postService.filterPostsUnified(user, filterPostsUnifiedRequest) };
133+
return { posts: await this.postService.filterPostsUnified(user, filterPostsUnifiedRequest, page, limit) };
105134
}
106135

107136
@Get('archive/')

src/repositories/PostRepository.ts

Lines changed: 102 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -123,56 +123,115 @@ export class PostRepository extends AbstractRepository<PostModel> {
123123
.getMany();
124124
}
125125

126-
public async filterPostsByCategories(categories: string[]): Promise<PostModel[]> {
127-
return await this.repository
126+
public async filterPostsByCategories(categories: string[], skip: number, limit: number): Promise<PostModel[]> {
127+
const postIds = await this.repository
128128
.createQueryBuilder("post")
129-
.leftJoinAndSelect("post.user", "user")
129+
.select("post.id")
130130
.innerJoin("post.categories", "category")
131131
.where("category.name IN (:...categories)", { categories })
132132
.andWhere("post.archive = false")
133+
.skip(skip)
134+
.take(limit)
133135
.getMany();
134-
}
135136

136-
public async filterPostsByPrice(lowerBound: number, upperBound: number): Promise<PostModel[]> {
137+
const ids = postIds.map(post => post.id);
138+
if (ids.length === 0) return [];
139+
137140
return await this.repository
138141
.createQueryBuilder("post")
139142
.leftJoinAndSelect("post.user", "user")
140143
.leftJoinAndSelect("post.categories", "categories")
144+
.where("post.id IN (:...ids)", { ids })
145+
.getMany();
146+
}
147+
148+
public async filterPostsByPrice(lowerBound: number, upperBound: number, skip: number, limit: number): Promise<PostModel[]> {
149+
const postIds = await this.repository
150+
.createQueryBuilder("post")
151+
.select("post.id")
141152
.where("CASE WHEN post.altered_price = -1 THEN post.original_price ELSE post.altered_price END >= :lowerBound", { lowerBound: lowerBound })
142153
.andWhere("CASE WHEN post.altered_price = -1 THEN post.original_price ELSE post.altered_price END <= :upperBound", { upperBound: upperBound })
143154
.andWhere("post.archive = false")
155+
.skip(skip)
156+
.take(limit)
157+
.getMany();
158+
159+
const ids = postIds.map(post => post.id);
160+
if (ids.length === 0) return [];
161+
162+
return await this.repository
163+
.createQueryBuilder("post")
164+
.leftJoinAndSelect("post.user", "user")
165+
.leftJoinAndSelect("post.categories", "categories")
166+
.where("post.id IN (:...ids)", { ids })
144167
.orderBy("categories.name", "ASC")
145168
.getMany();
146169
}
147170

148-
public async filterPriceHighToLow(): Promise<PostModel[]> {
171+
public async filterPriceHighToLow(skip: number, limit: number): Promise<PostModel[]> {
172+
const postIds = await this.repository
173+
.createQueryBuilder("post")
174+
.select("post.id")
175+
.where("post.archive = false")
176+
.orderBy("CASE WHEN post.altered_price = -1 THEN post.original_price ELSE post.altered_price END", "DESC")
177+
.skip(skip)
178+
.take(limit)
179+
.getMany();
180+
181+
const ids = postIds.map(post => post.id);
182+
if (ids.length === 0) return [];
183+
149184
return await this.repository
150185
.createQueryBuilder("post")
151186
.leftJoinAndSelect("post.user", "user")
152187
.leftJoinAndSelect("post.categories", "categories")
153-
.where("post.archive = false")
188+
.where("post.id IN (:...ids)", { ids })
154189
.orderBy("CASE WHEN post.altered_price = -1 THEN post.original_price ELSE post.altered_price END", "DESC")
155190
.addOrderBy("categories.name", "ASC")
156191
.getMany();
157192
}
158193

159-
public async filterPriceLowToHigh(): Promise<PostModel[]> {
194+
public async filterPriceLowToHigh(skip: number, limit: number): Promise<PostModel[]> {
195+
const postIds = await this.repository
196+
.createQueryBuilder("post")
197+
.select("post.id")
198+
.where("post.archive = false")
199+
.orderBy("CASE WHEN post.altered_price = -1 THEN post.original_price ELSE post.altered_price END", "ASC")
200+
.skip(skip)
201+
.take(limit)
202+
.getMany();
203+
204+
const ids = postIds.map(post => post.id);
205+
if (ids.length === 0) return [];
206+
160207
return await this.repository
161208
.createQueryBuilder("post")
162209
.leftJoinAndSelect("post.user", "user")
163210
.leftJoinAndSelect("post.categories", "categories")
164-
.where("post.archive = false")
211+
.where("post.id IN (:...ids)", { ids })
165212
.orderBy("CASE WHEN post.altered_price = -1 THEN post.original_price ELSE post.altered_price END", "ASC")
166213
.addOrderBy("categories.name", "ASC")
167214
.getMany();
168215
}
169216

170-
public async filterNewlyListed(): Promise<PostModel[]> {
217+
public async filterNewlyListed(skip: number, limit: number): Promise<PostModel[]> {
218+
const postIds = await this.repository
219+
.createQueryBuilder("post")
220+
.select("post.id")
221+
.where("post.archive = false")
222+
.orderBy("post.created", "DESC")
223+
.skip(skip)
224+
.take(limit)
225+
.getMany();
226+
227+
const ids = postIds.map(post => post.id);
228+
if (ids.length === 0) return [];
229+
171230
return await this.repository
172231
.createQueryBuilder("post")
173232
.leftJoinAndSelect("post.user", "user")
174233
.leftJoinAndSelect("post.categories", "categories")
175-
.where("post.archive = false")
234+
.where("post.id IN (:...ids)", { ids })
176235
.orderBy("post.created", "DESC")
177236
.getMany();
178237
}
@@ -188,22 +247,32 @@ export class PostRepository extends AbstractRepository<PostModel> {
188247
.getMany();
189248
}
190249

191-
public async filterByCondition(conditions: string[]): Promise<PostModel[]> {
250+
public async filterByCondition(conditions: string[], skip: number, limit: number): Promise<PostModel[]> {
251+
const postIds = await this.repository
252+
.createQueryBuilder("post")
253+
.select("post.id")
254+
.where("post.condition IN (:...conditions)", { conditions })
255+
.andWhere("post.archive = false")
256+
.skip(skip)
257+
.take(limit)
258+
.getMany();
259+
260+
const ids = postIds.map(post => post.id);
261+
if (ids.length === 0) return [];
262+
192263
return await this.repository
193264
.createQueryBuilder("post")
194265
.leftJoinAndSelect("post.user", "user")
195266
.leftJoinAndSelect("post.categories", "categories")
196-
.where("post.condition IN (:...conditions)", { conditions })
197-
.andWhere("post.archive = false")
267+
.where("post.id IN (:...ids)", { ids })
198268
.orderBy("categories.name", "ASC")
199269
.getMany();
200270
}
201271

202-
public async filterPostsUnified(filterPostsUnifiedRequest: FilterPostsUnifiedRequest): Promise<PostModel[]> {
272+
public async filterPostsUnified(filterPostsUnifiedRequest: FilterPostsUnifiedRequest, skip: number, limit: number): Promise<PostModel[]> {
203273
const qb = this.repository
204274
.createQueryBuilder("post")
205-
.leftJoinAndSelect("post.user", "user")
206-
.leftJoinAndSelect("post.categories", "categories")
275+
.select("post.id")
207276
.where("post.archive = false");
208277

209278
// Categories
@@ -257,7 +326,18 @@ export class PostRepository extends AbstractRepository<PostModel> {
257326
}
258327
}
259328

260-
return await qb.getMany();
329+
qb.skip(skip).take(limit);
330+
331+
const postIds = await qb.getMany();
332+
const ids = postIds.map(post => post.id);
333+
if (ids.length === 0) return [];
334+
335+
return await this.repository
336+
.createQueryBuilder("post")
337+
.leftJoinAndSelect("post.user", "user")
338+
.leftJoinAndSelect("post.categories", "categories")
339+
.where("post.id IN (:...ids)", { ids })
340+
.getMany();
261341
}
262342

263343

@@ -289,7 +369,8 @@ export class PostRepository extends AbstractRepository<PostModel> {
289369

290370
public async archiveAllPostsByUserId(userId: string): Promise<void> {
291371
await this.repository
292-
.createQueryBuilder()
372+
.createQueryBuilder("post")
373+
.leftJoin("post.user", "user")
293374
.update(PostModel)
294375
.set({ archive: true })
295376
.where("user.firebaseUid = :userId", { userId })
@@ -325,7 +406,7 @@ export class PostRepository extends AbstractRepository<PostModel> {
325406
.createQueryBuilder("post")
326407
.leftJoinAndSelect("post.user", "user")
327408
.where("post.id != :excludePostId", { excludePostId })
328-
.andWhere("post.user != :excludeUserId", { excludeUserId })
409+
.andWhere("user.firebaseUid != :excludeUserId", { excludeUserId })
329410
.orderBy(`embedding::vector <-> CAST('${lit}' AS vector(512))`)
330411
.setParameters({ embedding: queryEmbedding })
331412
.limit(10)
@@ -341,9 +422,8 @@ export class PostRepository extends AbstractRepository<PostModel> {
341422
.createQueryBuilder("post")
342423
.leftJoinAndSelect("post.user", "user")
343424
.where("post.embedding IS NOT NULL")
344-
.andWhere("post.user != :excludeUserId", { excludeUserId })
425+
.andWhere("user.firebaseUid != :excludeUserId", { excludeUserId })
345426
.orderBy(`post.embedding::vector <-> CAST('${lit}' AS vector(512))`)
346-
.setParameter("embedding", embedding)
347427
.limit(limit)
348428
.getMany();
349429
}

src/repositories/RequestRepository.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ export class RequestRepository extends AbstractRepository<RequestModel> {
5757

5858
public async archiveAllRequestsByUserId(userId: Uuid): Promise<void> {
5959
await this.repository
60-
.createQueryBuilder()
60+
.createQueryBuilder("request")
61+
.leftJoin("request.user", "user")
6162
.update(RequestModel)
6263
.set({ archive: true })
6364
.where("user.firebaseUid = :userId", { userId })
@@ -109,7 +110,7 @@ export class RequestRepository extends AbstractRepository<RequestModel> {
109110
.where("request.embedding IS NOT NULL")
110111
.andWhere("user.firebaseUid != :excludeUserId", { excludeUserId })
111112
.orderBy(`request.embedding::vector <-> CAST('${lit}' AS vector(512))`)
112-
.setParameter("excludeUserId", excludeUserId)
113+
// .setParameter("excludeUserId", excludeUserId)
113114
.take(limit)
114115
.getMany();
115116
}

0 commit comments

Comments
 (0)