Skip to content

Commit ecca099

Browse files
yofriadiYofriadi Yahya
andauthored
modify(ideas, blog): add sort by like on ideas & fix sort on blogs (#76)
* modify(ideas, blog): add sort by like on ideas & fix sort on blogs * modify(leaderboards): add new endpoints leaderboards * modify(leaderboards): trim down response Co-authored-by: Yofriadi Yahya <yofriadi.yahya@outlook.com>
1 parent a287ef5 commit ecca099

File tree

6 files changed

+85
-8
lines changed

6 files changed

+85
-8
lines changed

src/api/controllers/blog.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ export default class BlogController {
4747
let sorts = '';
4848
if (sort) {
4949
sort.split(',').forEach((s: string) => {
50+
let ss = s;
51+
if (ss.startsWith('-')) ss = s.slice(1);
5052
if (orderEnum.indexOf(s, 1) < 0) {
5153
return;
5254
}
5355

54-
sorts += snakeCase(s);
56+
sorts += s.startsWith('-') ? '-' + snakeCase(s) : snakeCase(s);
5557
});
5658
}
5759

src/api/controllers/idea.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import camelcaseKeys from 'camelcase-keys';
22
import {FastifyRequest} from 'fastify';
33
import {Controller, GET, POST, PUT} from 'fastify-decorators';
4+
import {snakeCase} from 'lodash';
45

56
import {
67
UserService,
@@ -22,6 +23,7 @@ import {
2223
ideaCommentSchema,
2324
ideaInputSchema,
2425
paginatedIdeaSchema,
26+
paginatedLeaderboardSchema,
2527
ideaQueryStringSchema,
2628
ideaProblemAreaSchema,
2729
} from '../schemas/idea';
@@ -30,7 +32,7 @@ import {PaginatedResponse, IdeaQueryString} from '../../libs/types';
3032
import {paginateResponse} from '../../libs/utils';
3133

3234
@Controller({route: '/ideas'})
33-
export default class IdeaController {
35+
export class IdeaController {
3436
constructor(
3537
private userService: UserService,
3638
private ideaService: IdeaService,
@@ -68,6 +70,11 @@ export default class IdeaController {
6870
async getAllIdeas(
6971
req: FastifyRequest<{Querystring: IdeaQueryString}>,
7072
): Promise<PaginatedResponse<Idea>> {
73+
const orderEnum = ['solutionType', 'liked'];
74+
if (req.query.sort) {
75+
req.query.sort = transformSort(req.query.sort, orderEnum);
76+
}
77+
7178
const response = await this.ideaService.getAll(req.query);
7279
return paginateResponse(req.query, response);
7380
}
@@ -166,3 +173,44 @@ export default class IdeaController {
166173
return this.ideaProblemAreaService.getAll();
167174
}
168175
}
176+
177+
@Controller({route: '/leaderboards/team'})
178+
export class LeaderboardController {
179+
constructor(private ideaService: IdeaService) {}
180+
181+
@GET({
182+
url: '/',
183+
options: {
184+
schema: {
185+
querystring: ideaQueryStringSchema,
186+
response: {200: paginatedLeaderboardSchema},
187+
},
188+
},
189+
})
190+
async getAllIdeas(
191+
req: FastifyRequest<{Querystring: IdeaQueryString}>,
192+
): Promise<PaginatedResponse<Idea>> {
193+
const orderEnum = ['solutionType', 'liked'];
194+
195+
if (req.query.sort) {
196+
req.query.sort = transformSort(req.query.sort, orderEnum);
197+
}
198+
199+
const response = await this.ideaService.getAll(req.query);
200+
return paginateResponse(req.query, response);
201+
}
202+
}
203+
204+
function transformSort(sort: string, order: string[]): string {
205+
let sorts = '';
206+
sort.split(',').forEach((s: string) => {
207+
let ss = s;
208+
if (ss.startsWith('-')) ss = s.slice(1);
209+
if (order.indexOf(ss) < 0) {
210+
return;
211+
}
212+
213+
sorts += s.startsWith('-') ? '-' + snakeCase(s) : snakeCase(s);
214+
});
215+
return sorts;
216+
}

src/api/schemas/idea.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,24 @@ export const paginatedIdeaSchema = {
5353
},
5454
};
5555

56+
export const paginatedLeaderboardSchema = {
57+
type: 'object',
58+
properties: {
59+
pages: pagination,
60+
data: {
61+
type: 'array',
62+
items: {
63+
type: 'object',
64+
properties: {
65+
solutionName: {type: 'string'},
66+
solutionSupportingPhotos: {type: 'array'},
67+
liked: {type: 'number'},
68+
},
69+
},
70+
},
71+
},
72+
};
73+
5674
export const ideaCommentSchema = {
5775
type: 'object',
5876
properties: {

src/api/services/blog.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default class BlogService {
2929

3030
sorts.forEach((s: string) => {
3131
if (s.startsWith('-')) {
32+
s = s.slice(1);
3233
orderBy[`blogs.${s}`] = 'DESC';
3334
} else {
3435
orderBy[`blogs.${s}`] = 'ASC';

src/api/services/idea.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,24 @@ export default class IdeaService {
4545
});
4646
}
4747

48+
const orderBy = {};
4849
if (sort) {
49-
if (sort.startsWith('-')) {
50-
query = query.orderBy('ideas.solution_name', 'DESC');
51-
} else {
52-
query = query.orderBy('ideas.solution_name', 'ASC');
53-
}
50+
const sorts = sort.split(',');
51+
52+
sorts.forEach((s: string) => {
53+
if (s.startsWith('-')) {
54+
s = s.slice(1);
55+
orderBy[`ideas.${s}`] = 'DESC';
56+
} else {
57+
orderBy[`ideas.${s}`] = 'ASC';
58+
}
59+
});
5460
}
5561

5662
return query
5763
.limit(limit)
5864
.offset(offset)
65+
.orderBy(orderBy)
5966
.leftJoinAndSelect('ideas.problemArea', 'problemArea')
6067
.loadRelationCountAndMap('ideas.totalLikes', 'ideas.likes')
6168
.loadRelationCountAndMap('ideas.totalComments', 'ideas.comments')

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import fastifyCors from 'fastify-cors';
44
import {bootstrap} from 'fastify-decorators';
55
import fastifyMultipart from 'fastify-multipart';
66

7-
import IdeaController from './api/controllers/idea';
7+
import {IdeaController, LeaderboardController} from './api/controllers/idea';
88
import IdeaUserController from './api/controllers/ideaUser';
99
import IdeaLikeController from './api/controllers/ideaLike';
1010
import IdeaCommentController from './api/controllers/ideaComment';
@@ -33,6 +33,7 @@ server.register(fastifyCors, {
3333
});
3434
server.register(bootstrap, {
3535
controllers: [
36+
LeaderboardController,
3637
IdeaController,
3738
IdeaUserController,
3839
IdeaLikeController,

0 commit comments

Comments
 (0)