-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- #405
- Loading branch information
Showing
5 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; | ||
import { HydratedDocument } from 'mongoose'; | ||
|
||
export type UserDocument = HydratedDocument<follow>; | ||
|
||
@Schema() | ||
export class follow { | ||
@Prop({ required: true }) | ||
userId: number; | ||
|
||
@Prop({ required: true }) | ||
followId: number; | ||
} | ||
|
||
export const FollowSchema = SchemaFactory.createForClass(follow); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { MongooseModule } from '@nestjs/mongoose'; | ||
import { FollowSchema, follow } from './db/follow.database.schema'; | ||
|
||
@Module({ | ||
imports: [ | ||
MongooseModule.forFeature([{ name: follow.name, schema: FollowSchema }]), | ||
], | ||
providers: [], | ||
exports: [], | ||
}) | ||
export class FollowModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { UseFilters, UseGuards } from '@nestjs/common'; | ||
import { Args, Mutation, Resolver } from '@nestjs/graphql'; | ||
import { MyUserId } from 'src/auth/myContext'; | ||
import { StatAuthGuard } from 'src/auth/statAuthGuard'; | ||
import { HttpExceptionFilter } from 'src/http-exception.filter'; | ||
import { follow } from './db/follow.database.schema'; | ||
import { FollowService } from './follow.service'; | ||
import { FollowSuccess, UserList } from './model/follow.model'; | ||
|
||
@UseFilters(HttpExceptionFilter) | ||
@UseGuards(StatAuthGuard) | ||
@Resolver() | ||
export class FollowResolver { | ||
constructor(private readonly followService: FollowService) {} | ||
|
||
@Mutation((_returns) => FollowSuccess) | ||
async followUser( | ||
@MyUserId() userId: number, | ||
@Args('login') login: string, | ||
): Promise<follow> { | ||
return await this.followService.followUser(userId, login); | ||
} | ||
|
||
@Mutation((_returns) => FollowSuccess) | ||
async unfollowUser( | ||
@MyUserId() userId: number, | ||
@Args('login') login: string, | ||
): Promise<follow> { | ||
return await this.followService.unfollowUser(userId, login); | ||
} | ||
|
||
@Mutation((_returns) => UserList) | ||
async getFollowerList(login: string): Promise<UserList> { | ||
const user = await this.followService.getFollowerList(login); | ||
const count = await this.followService.getFollowerCount(login); | ||
|
||
return { | ||
count, | ||
user, | ||
}; | ||
} | ||
|
||
@Mutation((_returns) => UserList) | ||
async getFollowingList(login: string): Promise<UserList> { | ||
const user = await this.followService.getFollowingList(login); | ||
const count = await this.followService.getFollowingCount(login); | ||
|
||
return { | ||
count, | ||
user, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import { Model } from 'mongoose'; | ||
import { UserPreview } from 'src/common/models/common.user.model'; | ||
import { follow } from './db/follow.database.schema'; | ||
import { FollowSuccess } from './model/follow.model'; | ||
|
||
@Injectable() | ||
export class FollowService { | ||
constructor( | ||
@InjectModel(follow.name) | ||
private readonly followModel: Model<follow>, | ||
) {} | ||
|
||
// input으로 들어온 유저를 팔로우 함 | ||
async followUser(me: number, login: string): Promise<FollowSuccess> { | ||
//if (me === login) { | ||
// return false; | ||
//} | ||
|
||
//db.add.me->login | ||
await this.followModel.create({}).then((result) => result.toObject()); | ||
|
||
return { | ||
message: 'OK', | ||
userId: 123, | ||
followId: 312, | ||
}; | ||
} | ||
|
||
// input으로 들어온 유저를 언팔로우 함 | ||
// todo: unfollow 성공도 같은걸 (상태) 반환해서 이름 다시 지어야함 | ||
async unfollowUser(me: number, login: string): Promise<FollowSuccess> { | ||
//db.delete.me->login | ||
|
||
//if (me === login) { | ||
// return false; | ||
//} | ||
|
||
return { | ||
message: 'OK', | ||
userId: 123, | ||
followId: 312, | ||
}; | ||
} | ||
|
||
// input 유저 <- 팔로워 목록을 찾아옴 | ||
async getFollowerList(login: string): Promise<UserPreview[]> { | ||
//login이 A<-B 의 A위치에 있는거 find 후 B들의 UserPreview로 합쳐서 반환 | ||
return []; | ||
} | ||
|
||
// input 유저 -> 팔로잉 목록을 찾아옴 | ||
async getFollowingList(login: string): Promise<UserPreview[]> { | ||
//login이 A<-B 의 B위치에 있는거 find 후 A들의 UserPreview로 합쳐서 반환 | ||
return []; | ||
} | ||
|
||
async getFollowerCount(login: string): Promise<number> { | ||
return await this.followModel.countDocuments(); //login filter | ||
} | ||
|
||
async getFollowingCount(login: string): Promise<number> { | ||
return await this.followModel.countDocuments(); //login filter | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Field, ObjectType, createUnionType } from '@nestjs/graphql'; | ||
import { UserPreview } from 'src/common/models/common.user.model'; | ||
|
||
@ObjectType() | ||
export class UserList { | ||
@Field() | ||
user: UserPreview[]; | ||
|
||
@Field() | ||
count: number; | ||
} | ||
|
||
@ObjectType() | ||
export class FollowFail { | ||
@Field() | ||
message: 'fail'; | ||
} | ||
|
||
@ObjectType() | ||
export class FollowSuccess { | ||
@Field() | ||
message: 'OK'; | ||
|
||
@Field() | ||
userId: number; | ||
|
||
@Field() | ||
followId: number; | ||
} | ||
|
||
export const LoginResult = createUnionType({ | ||
name: 'LoginResult', | ||
types: () => [FollowSuccess, FollowFail] as const, | ||
resolveType(value) { | ||
switch (value.message) { | ||
case 'OK': | ||
return FollowSuccess; | ||
case 'fail': | ||
return FollowFail; | ||
} | ||
}, | ||
}); |