Skip to content

Commit 42b7a0e

Browse files
committed
✨ followers and following paginated list
1 parent 7b4ba3e commit 42b7a0e

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

graphql/profile.graphql

+6
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ input ToggleFollowInput {
6060
action: ToggleAction!
6161
}
6262

63+
input PaginationInput {
64+
page: Int
65+
}
66+
6367
type Mutation {
6468
# Authorize user with Github
6569
authorizeGithub(input: FakeInput): AuthorizeGithubOutput!
@@ -70,4 +74,6 @@ type Mutation {
7074
type Query {
7175
getUser: User!
7276
search(input: SearchInput!): [RestrictedUserOther!]!
77+
getFollowers(input: PaginationInput): [RestrictedUserOther!]!
78+
getFollowing(input: PaginationInput): [RestrictedUserOther!]!
7379
}

src/graphql.types.ts

+8
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ export class ToggleFollowInput {
6565
action: ToggleAction;
6666
}
6767

68+
export class PaginationInput {
69+
page?: Nullable<number>;
70+
}
71+
6872
export class User {
6973
id: string;
7074
name: string;
@@ -138,6 +142,10 @@ export abstract class IQuery {
138142
abstract getUser(): User | Promise<User>;
139143

140144
abstract search(input: SearchInput): RestrictedUserOther[] | Promise<RestrictedUserOther[]>;
145+
146+
abstract getFollowers(input?: Nullable<PaginationInput>): RestrictedUserOther[] | Promise<RestrictedUserOther[]>;
147+
148+
abstract getFollowing(input?: Nullable<PaginationInput>): RestrictedUserOther[] | Promise<RestrictedUserOther[]>;
141149
}
142150

143151
export class GithubAuth {

src/profile/profile.resolver.ts

+25
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
ToggleFollowInput,
66
RestrictedUserSelf,
77
RestrictedUserOther,
8+
PaginationInput,
89
} from 'src/graphql.types';
910
import { ProfileService } from './profile.service';
1011

@@ -65,4 +66,28 @@ export class ProfileResolver {
6566
throw new Error('Invalid request, token not found');
6667
return await this.profileService.toggleFollow(input, token);
6768
}
69+
70+
@Query('getFollowers')
71+
async getFollowers(
72+
@Args('input') input: PaginationInput,
73+
@Context() context,
74+
): Promise<RestrictedUserOther[]> {
75+
const authorization = context.req.headers.authorization;
76+
const token = authorization?.split(' ')[1];
77+
if (token === undefined)
78+
throw new Error('Invalid request, token not found');
79+
return await this.profileService.getFollowers(input?.page || 1, token);
80+
}
81+
82+
@Query('getFollowing')
83+
async getFollowing(
84+
@Args('input') input: PaginationInput,
85+
@Context() context,
86+
): Promise<RestrictedUserOther[]> {
87+
const authorization = context.req.headers.authorization;
88+
const token = authorization?.split(' ')[1];
89+
if (token === undefined)
90+
throw new Error('Invalid request, token not found');
91+
return await this.profileService.getFollowing(input?.page || 1, token);
92+
}
6893
}

src/profile/profile.service.ts

+69
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,75 @@ export class ProfileService {
168168
};
169169
}
170170

171+
/**
172+
* It returns a list of users who follow the current user.
173+
* @param {number} page - The page number of the paginated list.
174+
* @param {string} token - The token of the user who is requesting the followers.
175+
* @returns An array of restricted user objects.
176+
*/
177+
async getFollowers(
178+
page: number,
179+
token: string,
180+
): Promise<RestrictedUserOther[]> {
181+
const user = await decode(token, this.prisma);
182+
const userId = user.id;
183+
const followers = await this.prisma.user.findMany({
184+
where: {
185+
followedBy: {
186+
some: {
187+
id: userId,
188+
},
189+
},
190+
},
191+
select: {
192+
id: true,
193+
name: true,
194+
email: true,
195+
codeforcesUsername: true,
196+
leetcodeUsername: true,
197+
description: true,
198+
profilePicture: true,
199+
},
200+
skip: (page - 1) * PAGINATION_LIMIT,
201+
take: PAGINATION_LIMIT,
202+
});
203+
return followers;
204+
}
205+
206+
/**
207+
* It returns a list of users who are following the current user.
208+
* @param {number} page - The page number of the paginated list.
209+
* @param {string} token - The token of the user who is requesting the data.
210+
* @returns An array of restricted user objects.
211+
*/
212+
async getFollowing(
213+
page: number,
214+
token: string,
215+
): Promise<RestrictedUserOther[]> {
216+
const user = await decode(token, this.prisma);
217+
const userId = user.id;
218+
const following = await this.prisma.user.findMany({
219+
where: {
220+
following: {
221+
some: {
222+
id: userId,
223+
},
224+
},
225+
},
226+
select: {
227+
id: true,
228+
name: true,
229+
email: true,
230+
codeforcesUsername: true,
231+
leetcodeUsername: true,
232+
description: true,
233+
profilePicture: true,
234+
},
235+
skip: (page - 1) * PAGINATION_LIMIT,
236+
take: PAGINATION_LIMIT,
237+
});
238+
return following;
239+
}
171240
/**
172241
* It deletes all GithubAuth records that were created more than 10 minutes ago
173242
* and that belong to the given userId.

0 commit comments

Comments
 (0)