Skip to content

Commit

Permalink
feat: ✨ follow api 형식 제안
Browse files Browse the repository at this point in the history
  • Loading branch information
niamu01 committed Feb 20, 2024
1 parent 09eaf83 commit 767225e
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 0 deletions.
15 changes: 15 additions & 0 deletions app/src/api/follow/db/follow.database.schema.ts
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);
12 changes: 12 additions & 0 deletions app/src/api/follow/follow.module.ts
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 {}
53 changes: 53 additions & 0 deletions app/src/api/follow/follow.resolve.ts
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,
};
}
}
66 changes: 66 additions & 0 deletions app/src/api/follow/follow.service.ts
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
}
}
42 changes: 42 additions & 0 deletions app/src/api/follow/model/follow.model.ts
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;
}
},
});

0 comments on commit 767225e

Please sign in to comment.