Skip to content

Commit

Permalink
feat: followpagination (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
niamu01 committed Dec 28, 2023
1 parent 7ceb414 commit aa268cc
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/src/follow/db/follow.database.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { HydratedDocument } from 'mongoose';

export type UserDocument = HydratedDocument<follow>;

@Schema()
@Schema({ collection: 'follows' })
export class follow {
@Prop({ required: true })
userId: number;
Expand Down
17 changes: 17 additions & 0 deletions app/src/follow/dto/followList.dto.getFollowList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ArgsType, Field, extend, registerEnumType } from '@nestjs/graphql';
import { PaginationCursorArgs } from 'src/pagination/cursor/dtos/pagination.cursor.dto';

export enum FollowListSortOrder {
FOLLOW_AT_ASC = '1',
FOLLOW_AT_DESC = '-1',
}

registerEnumType(FollowListSortOrder, { name: 'FollowListSortOrder' });

@ArgsType()
export class FollowListPaginatedArgs extends PaginationCursorArgs {
@Field((_type) => FollowListSortOrder, {
defaultValue: FollowListSortOrder.FOLLOW_AT_DESC,
})
sortOrder: FollowListSortOrder;
}
2 changes: 2 additions & 0 deletions app/src/follow/follow.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { CursusUserModule } from 'src/api/cursusUser/cursusUser.module';
import { PaginationCursorModule } from 'src/pagination/cursor/pagination.cursor.module';
import { FollowSchema, follow } from './db/follow.database.schema';
import { FollowResolver } from './follow.resolve';
import { FollowService } from './follow.service';
Expand All @@ -9,6 +10,7 @@ import { FollowService } from './follow.service';
imports: [
MongooseModule.forFeature([{ name: follow.name, schema: FollowSchema }]),
CursusUserModule,
PaginationCursorModule,
],
providers: [FollowResolver, FollowService],
})
Expand Down
29 changes: 28 additions & 1 deletion app/src/follow/follow.resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@ import { PubSub } from 'graphql-subscriptions';
import { MyUserId } from 'src/auth/myContext';
import { StatAuthGuard } from 'src/auth/statAuthGuard';
import { HttpExceptionFilter } from 'src/http-exception.filter';
import {
FollowListPaginatedArgs,
FollowListSortOrder,
} from './dto/followList.dto.getFollowList';
import { FollowService } from './follow.service';
import { FollowListWithCount, FollowResult } from './model/follow.model';
import {
FollowListPaginated,
FollowListWithCount,
FollowResult,
} from './model/follow.model';

const pubSub = new PubSub();

Expand Down Expand Up @@ -65,17 +73,36 @@ export class FollowResolver {
return followResult;
}

@UseGuards(StatAuthGuard)
@Query((_returns) => FollowListPaginated)
async getFollowerPaginated(
@MyUserId() userId: number,
@Args('target') target: string,
@Args()
followListPaginatedArgs: FollowListPaginatedArgs,
): Promise<FollowListPaginated> {
return await this.followService.followerPaginated({
userId,
target,
followListPaginatedArgs,
});
}

@UseGuards(StatAuthGuard)
@Query((_returns) => FollowListWithCount)
async getFollowerList(
@MyUserId() userId: number,
@Args('target') target: string,
// @Args('sortOrder') sortOrder: FollowListSortOrder,
@Args('limit', { defaultValue: 3 }) limit: number,
): Promise<FollowListWithCount> {
let order: FollowListSortOrder = FollowListSortOrder.FOLLOW_AT_DESC;

const followerList = await this.followService.getFollowerList(
userId,
target,
limit,
order,
);
const count = await this.followService.getFollowerCount(target);

Expand Down
78 changes: 75 additions & 3 deletions app/src/follow/follow.service.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { FilterQuery, Model } from 'mongoose';
import { UserPreview } from 'src/common/models/common.user.model';
import {
QueryArgs,
findAllAndLean,
} from 'src/database/mongoose/database.mongoose.query';
import {
CursorExtractor,
FieldExtractor,
PaginationCursorService,
} from 'src/pagination/cursor/pagination.cursor.service';
import { CursusUserService } from '../api/cursusUser/cursusUser.service';
import { follow } from './db/follow.database.schema';
import type { FollowList, FollowResult } from './model/follow.model';
import type {
FollowList,
FollowListPaginated,
FollowResult,
} from './model/follow.model';
import { PaginationCursorArgs } from 'src/pagination/cursor/dtos/pagination.cursor.dto';
import {
FollowListPaginatedArgs,
FollowListSortOrder,
} from './dto/followList.dto.getFollowList';

type FollowListCursorField = [number, string];

@Injectable()
export class FollowService {
constructor(
@InjectModel(follow.name)
private readonly followModel: Model<follow>,
private readonly cursusUserService: CursusUserService,
private readonly paginationCursorService: PaginationCursorService,
) {}

async findAllAndLean(queryArgs?: QueryArgs<follow>): Promise<follow[]> {
Expand Down Expand Up @@ -59,6 +76,7 @@ export class FollowService {

return {
message: 'OK',
//id: `${result.userId}_${result.followId}`,
userId: result.userId,
followId: result.followId,
};
Expand Down Expand Up @@ -93,11 +111,42 @@ export class FollowService {
return { message: 'fail' };
}

async followerPaginated({
userId,
target,
followListPaginatedArgs,
}: {
userId: number;
target: string;
followListPaginatedArgs: FollowListPaginatedArgs;
}): Promise<FollowListPaginated> {
//filter 받기
const totalCount = await this.getFollowerCount(target);

if (followListPaginatedArgs.after) {
//after 구현 전
}

const FollowList: FollowList[] = await this.getFollowerList(
userId,
target,
followListPaginatedArgs.first + 1,
followListPaginatedArgs.sortOrder,
);

return this.paginationCursorService.toPaginated(
FollowList.slice(0, followListPaginatedArgs.first),
totalCount,
FollowList.length > followListPaginatedArgs.first,
cursorExtractor,
);
}
// getFollowerList("yeju") -> yeju를 팔로우 하는 사람들
async getFollowerList(
userId: number,
target: string,
limit: number,
sortOrder: FollowListSortOrder,
): Promise<FollowList[]> {
//target의 id
const targetId = await this.cursusUserService.getuserIdByLogin(target);
Expand All @@ -106,10 +155,22 @@ export class FollowService {
throw new NotFoundException();
}

//default value 로 변경
let order: 'asc' | 'desc' = 'desc';

switch (sortOrder) {
case FollowListSortOrder.FOLLOW_AT_ASC:
order = 'asc';
break;
case FollowListSortOrder.FOLLOW_AT_DESC:
order = 'desc';
break;
}

//target을 팔로우 하는 사람들
const follower: follow[] = await this.findAllAndLean({
filter: { followId: targetId },
sort: { _id: 'desc' },
sort: { _id: order },
limit,
});

Expand Down Expand Up @@ -222,3 +283,14 @@ export class FollowService {
return Promise.all(followList);
}
}

const cursorExtractor: CursorExtractor<FollowList> = (doc) =>
doc.user.id.toString() + '_' + doc.user.login;

const fieldExtractor: FieldExtractor<FollowListCursorField> = (
cursor: string,
) => {
const [idString, loginString] = cursor.split('_');

return [parseInt(idString), loginString];
};
4 changes: 4 additions & 0 deletions app/src/follow/model/follow.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Field, ObjectType, createUnionType } from '@nestjs/graphql';
import { UserPreview } from 'src/common/models/common.user.model';
import { CursorPaginated } from 'src/pagination/cursor/models/pagination.cursor.model';

@ObjectType()
export class FollowList {
Expand All @@ -10,6 +11,9 @@ export class FollowList {
user: UserPreview;
}

@ObjectType()
export class FollowListPaginated extends CursorPaginated(FollowList) {}

@ObjectType()
export class FollowListWithCount {
@Field((_type) => [FollowList])
Expand Down
28 changes: 22 additions & 6 deletions app/src/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,27 @@ type UserRankingIndexPaginated {
pageNumber: Int!
}

type CursorPageInfo {
totalCount: Int!
hasNextPage: Boolean!
endCursor: String
}

type FollowList {
isFollowing: Boolean
user: UserPreview!
}

type FollowListEdge {
cursor: String!
node: FollowList!
}

type FollowListPaginated {
edges: [FollowListEdge!]!
pageInfo: CursorPageInfo!
}

type FollowListWithCount {
followList: [FollowList!]!
count: Int!
Expand Down Expand Up @@ -102,12 +118,6 @@ type ProjectPreview {
difficulty: Int
}

type CursorPageInfo {
totalCount: Int!
hasNextPage: Boolean!
endCursor: String
}

type TeamPreview {
id: Int!
name: String!
Expand Down Expand Up @@ -631,6 +641,7 @@ type Query {
getEvalLogs(after: String, first: Int! = 20, corrector: String, corrected: String, projectName: String, outstandingOnly: Boolean! = false, imperfectOnly: Boolean! = false, sortOrder: EvalLogSortOrder! = BEGIN_AT_DESC): EvalLogsPaginated!
getSetting: Setting!
getExpTable: [ExpTable!]!
getFollowerPaginated(target: String!, after: String, first: Int! = 20, sortOrder: FollowListSortOrder! = FOLLOW_AT_DESC): FollowListPaginated!
getFollowerList(target: String!, limit: Int! = 3): FollowListWithCount!
getFollowingList(target: String!, limit: Int! = 3): FollowListWithCount!
}
Expand All @@ -640,6 +651,11 @@ enum EvalLogSortOrder {
BEGIN_AT_DESC
}

enum FollowListSortOrder {
FOLLOW_AT_ASC
FOLLOW_AT_DESC
}

type Mutation {
ftLogin(ftCode: String!): LoginSuccess!
googleLogin(google: GoogleLoginInput!, ftCode: String): LoginResult!
Expand Down

0 comments on commit aa268cc

Please sign in to comment.