Skip to content

Commit

Permalink
Merge pull request #63 from turnaroundwoo/master
Browse files Browse the repository at this point in the history
Feat: #60 Friends API 추가
  • Loading branch information
lovepeacefineapple authored Mar 22, 2023
2 parents 0fe026c + 45ef848 commit 4cad0cd
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 35 deletions.
4 changes: 3 additions & 1 deletion nestjs/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import * as redisStore from "cache-manager-redis-store";
import { MapModule } from "./maps/maps.module";
import { BlockUserModule } from "./blockUsers/blockUsers.module";
import { MailModule } from "./mails/mails.module";
import { FriendsModule } from "./friends/friends.module";

@Module({
imports: [
AuthModule,
BlockUserModule,
FriendsModule,
MailModule,
MapModule,
AuthModule,
UsersModule,
GraphQLModule.forRootAsync<ApolloDriverConfig>({
driver: ApolloDriver,
Expand Down
1 change: 0 additions & 1 deletion nestjs/src/blockUsers/blockUsers.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export class BlockUserResolver {
return this.blockUsersService.deleteBlock({ blockUserId });
}

// 신고
@Mutation(() => User)
async reportOpponent(
@Args("reportedId") reportedId: string, //
Expand Down
2 changes: 0 additions & 2 deletions nestjs/src/blockUsers/blockUsers.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ export class BlockUserService {
});
}

// 물리 삭제로 적용
async deleteBlock({ blockUserId }) {
const result = await this.blockUsersRepository.delete({
blockUserId,
});
return result;
}

// 신고
async createReport({ reportedId }) {
const user = await this.usersRepository.findOne({
where: { id: reportedId },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,5 @@ import { Field, InputType } from "@nestjs/graphql";
@InputType()
export class CreateFriendInput {
@Field(() => String)
id: string;

@Field(() => String)
email: string;
opponentId: string;
}
2 changes: 1 addition & 1 deletion nestjs/src/friends/entities/friend.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class Friend {

@Column()
@Field(() => String)
friend_id: string;
opponentId: string;

@JoinColumn()
@ManyToOne(() => User)
Expand Down
9 changes: 8 additions & 1 deletion nestjs/src/friends/friends.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { User } from "src/users/entities/user.entity";
import { Friend } from "./entities/friend.entity";
import { FriendsResolver } from "./friends.resolver";
import { FriendsService } from "./friends.service";

@Module({
imports: [
TypeOrmModule.forFeature([
Friend, //
User,
]),
],
providers: [],
providers: [
FriendsResolver, //
FriendsService,
],
})
export class FriendsModule {}
27 changes: 24 additions & 3 deletions nestjs/src/friends/friends.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
// 친구 추가
import { Args, Mutation, Query, Resolver } from "@nestjs/graphql";
import { Friend } from "./entities/friend.entity";
import { FriendsService } from "./friends.service";

// 친구 목록 조회
@Resolver()
export class FriendsResolver {
constructor(private readonly friendsService: FriendsService) {}

// 친구 삭제
@Mutation(() => Friend)
addFriend(
@Args("userId") userId: string,
@Args("opponentId") opponentId: string,
) {
return this.friendsService.createFriend({ userId, opponentId });
}

@Query(() => [Friend])
fetchFriends() {
return this.friendsService.findFriendAll();
}

@Mutation(() => Boolean)
deleteFriend(@Args("opponentId") opponentId: string): Promise<boolean> {
return this.friendsService.delete({ opponentId });
}
}
47 changes: 47 additions & 0 deletions nestjs/src/friends/friends.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Injectable, NotAcceptableException } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { User } from "src/users/entities/user.entity";
import { Repository } from "typeorm";
import { Friend } from "./entities/friend.entity";
import { IFriendsServiceDelete } from "./interfaces/friend-service.interface";

@Injectable()
export class FriendsService {
constructor(
@InjectRepository(Friend)
private readonly friendsRepository: Repository<Friend>,

@InjectRepository(User)
private readonly usersRepository: Repository<User>,
) {}

findFriendOne({ opponentId }) {
return this.friendsRepository.findOne({ where: opponentId });
}

findFriendAll() {
return this.friendsRepository.find();
}

async createFriend({ userId, opponentId }) {
const user = await this.friendsRepository.findOne({
where: {
user: { id: userId },
opponentId: opponentId,
},
});
if (user) throw new NotAcceptableException();
return this.friendsRepository.save({
user: { id: userId },
opponentId,
success: false,
});
}

async delete({ opponentId }: IFriendsServiceDelete): Promise<boolean> {
const result = await this.friendsRepository.delete({
opponentId,
});
return result.affected ? true : false;
}
}
3 changes: 3 additions & 0 deletions nestjs/src/friends/interfaces/friend-service.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IFriendsServiceDelete {
opponentId: string;
}
6 changes: 0 additions & 6 deletions nestjs/src/users/interfaces/user-service.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { CreateFriendInput } from "../dto/create-friend.input";
import { CreateUserInput } from "../dto/create-user.dto";
import { UpdateAllInput } from "../dto/update-all.input";
import { UpdateUserPwdInput } from "../dto/update-user.input";
Expand All @@ -17,11 +16,6 @@ export interface IUsersServiceUpdateAllInput {
updateAllInput: UpdateAllInput;
}

export interface IUsersServiceAddFriendInput {
id: string;
createFriendInput: CreateFriendInput;
}

export interface IUsersServiceFindOneById {
id: string;
}
Expand Down
3 changes: 0 additions & 3 deletions nestjs/src/users/users.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,15 @@ export class UsersResolver {
@Args("updateUserPwdInput") updateUserPwdInput: UpdateUserPwdInput,
): Promise<User> {
const id = context.req.user.id;
console.log("✅", id);
return this.usersService.update({ id, updateUserPwdInput });
}

// 회원 정보 업데이트 (age, interest, image)
@Mutation(() => User)
updateUser(
@Context() context: IContext,
@Args("updateAllInput") updateAllInput: UpdateAllInput,
): Promise<User> {
const id = context.req.user.id;
console.log("✅", id);
return this.usersService.updateAll({ id, updateAllInput });
}

Expand Down
14 changes: 1 addition & 13 deletions nestjs/src/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
ConflictException,
Injectable,
NotAcceptableException,
} from "@nestjs/common";
import { ConflictException, Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { User } from "./entities/user.entity";
Expand Down Expand Up @@ -51,14 +47,6 @@ export class UsersService {
});
}

async addFriends({ createFriendInput }) {
const friend = await this.usersRepository.findOne({
where: { id: createFriendInput.id },
});
if (friend) throw new NotAcceptableException();
return this.usersRepository.save({ ...createFriendInput });
}

async update({
id,
updateUserPwdInput,
Expand Down

0 comments on commit 4cad0cd

Please sign in to comment.