Skip to content

Commit

Permalink
refactor: ♻️ 한개만 찾는 요소에 대하여 find -> findOne로 변경
Browse files Browse the repository at this point in the history
refactor: ♻️ 팔로우 여부에 대해 나 자신 예외처리 제거, input을 login에서 id로 변경
revert: 🔥 follower, following List 삭제
refactor: ♻️ findOne 할 때 select 추가
refactor: ♻️ evalLog의 fieldExtractor의 커서 검사 사항 추가

- #406
  • Loading branch information
niamu01 committed Apr 10, 2024
1 parent 043c271 commit 912d917
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 97 deletions.
12 changes: 7 additions & 5 deletions app/src/api/cursusUser/cursusUser.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,17 @@ export class CursusUserService {
}

async getuserIdByLogin(login: string): Promise<number | null> {
const userId = await this.findOneAndLean({
filter: { 'user.login': login },
}).then((user) => user?.user.id);
const cursusUser: { user: Pick<cursus_user['user'], 'id'> } | null =
await this.findOneAndLean({
filter: { 'user.login': login },
select: { 'user.id': 1 },
});

if (!userId) {
if (!cursusUser) {
return null;
}

return userId;
return cursusUser.user.id;
}

async findAllAndLean(
Expand Down
64 changes: 3 additions & 61 deletions app/src/follow/follow.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,44 +63,14 @@ export class FollowResolver {
}

@UseGuards(StatAuthGuard)
@Query((_returns) => Boolean, { nullable: true })
@Query((_returns) => Boolean)
async getIsFollowing(
@MyUserId() userId: number,
@Args('target') target: string,
): Promise<boolean | undefined> {
const targetId = await this.followService.userIdByLogin(target);

@Args('targetId') targetId: number,
): Promise<boolean> {
return await this.followService.isFollowing(userId, targetId);
}

@UseGuards(StatAuthGuard)
@Query((_returns) => FollowListWithCount)
async getFollowerList(
@MyUserId() userId: number,
@Args('target') target: string,
@Args('limit', { defaultValue: 3 }) limit: number,
@Args('sortOrder', {
type: () => FollowSortOrder,
defaultValue: FollowSortOrder.FOLLOW_AT_DESC,
})
sortOrder: FollowSortOrder,
): Promise<FollowListWithCount> {
const targetId = await this.followService.userIdByLogin(target);
const count = await this.followService.followerCount(targetId);

const followerList = await this.followService.followerList(
userId,
target,
limit,
sortOrder,
);

return {
count,
followList: followerList,
};
}

@UseGuards(StatAuthGuard)
@Query((_returns) => FollowListPaginated)
async getFollowerPaginated(
Expand All @@ -110,34 +80,6 @@ export class FollowResolver {
return await this.followService.followerPaginated(userId, args);
}

@UseGuards(StatAuthGuard)
@Query((_returns) => FollowListWithCount)
async getFollowingList(
@MyUserId() userId: number,
@Args('target') target: string,
@Args('limit', { defaultValue: 3 }) limit: number,
@Args('sortOrder', {
type: () => FollowSortOrder,
defaultValue: FollowSortOrder.FOLLOW_AT_DESC,
})
sortOrder: FollowSortOrder,
): Promise<FollowListWithCount> {
const targetId = await this.followService.userIdByLogin(target);
const count = await this.followService.followingCount(targetId);

const followingList = await this.followService.followingList(
userId,
target,
limit,
sortOrder,
);

return {
count,
followList: followingList,
};
}

@UseGuards(StatAuthGuard)
@Query((_returns) => FollowListPaginated)
async getFollowingPaginated(
Expand Down
33 changes: 13 additions & 20 deletions app/src/follow/follow.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ export class FollowService {
): Promise<typeof FollowResult> {
const following = await this.cursusUserService.getuserIdByLogin(target);

const alreadyFollow = await this.followModel.find({
userId: userId,
followId: following,
});
const alreadyFollow = await this.followModel.findOne(
{
userId: userId,
followId: following,
},
{ _id: 1 },
);

if (!following || userId === following || alreadyFollow.length) {
if (!following || userId === following || alreadyFollow) {
return { message: 'fail' };
}

Expand Down Expand Up @@ -112,7 +115,6 @@ export class FollowService {
return { message: 'fail' };
}

// getFollowerList("yeju") -> yeju를 팔로우 하는 사람들
async followerList(
userId: number,
target: string,
Expand Down Expand Up @@ -313,20 +315,11 @@ export class FollowService {
return await this.followModel.countDocuments({ userId, filter });
}

async isFollowing(
userId: number,
targetId: number,
): Promise<boolean | undefined> {
let isFollowing: boolean | undefined = undefined;

if (userId !== targetId) {
isFollowing = !!(await this.followModel.findOne({
userId,
targetId,
}));
}

return isFollowing;
async isFollowing(userId: number, targetId: number): Promise<boolean> {
return !!(await this.followModel.findOne({
userId,
targetId,
}));
}

async checkFollowing(
Expand Down
19 changes: 16 additions & 3 deletions app/src/page/evalLog/evalLog.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { BadRequestException, Injectable } from '@nestjs/common';
import { FilterQuery } from 'mongoose';
import { scale_team } from 'src/api/scaleTeam/db/scaleTeam.database.schema';
import {
Expand Down Expand Up @@ -143,7 +143,20 @@ const cursorExtractor: CursorExtractor<EvalLog> = (doc) =>
doc.id.toString() + '_' + doc.header.beginAt.toISOString();

const fieldExtractor: FieldExtractor<EvalLogCursorField> = (cursor: string) => {
const [idString, dateString] = cursor.split('_');
const cursorDataList = cursor.split('_');
if (cursorDataList.length !== 2) {
throw new BadRequestException('Invalid Cursor');
}

const userId = parseInt(cursorDataList[0]);
if (isNaN(userId)) {
throw new BadRequestException('Invalid Cursor');
}

const date = new Date(cursorDataList[1]).getTime();
if (isNaN(date)) {
throw new BadRequestException('Invalid Cursor');
}

return [parseInt(idString), new Date(dateString)];
return [userId, new Date(date)];
};
9 changes: 1 addition & 8 deletions app/src/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ type FollowListPaginated {
pageInfo: CursorPageInfo!
}

type FollowListWithCount {
followList: [FollowList!]!
count: Int!
}

type LinkableAccount {
platform: String!
id: String!
Expand Down Expand Up @@ -641,10 +636,8 @@ 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!]!
getIsFollowing(target: String!): Boolean
getFollowerList(target: String!, limit: Int! = 3, sortOrder: FollowSortOrder! = FOLLOW_AT_DESC): FollowListWithCount!
getIsFollowing(targetId: Int!): Boolean!
getFollowerPaginated(after: String, first: Int! = 20, target: String!, sortOrder: FollowSortOrder! = FOLLOW_AT_DESC): FollowListPaginated!
getFollowingList(target: String!, limit: Int! = 3, sortOrder: FollowSortOrder! = FOLLOW_AT_DESC): FollowListWithCount!
getFollowingPaginated(after: String, first: Int! = 20, target: String!, sortOrder: FollowSortOrder! = FOLLOW_AT_DESC): FollowListPaginated!
}

Expand Down

0 comments on commit 912d917

Please sign in to comment.