-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from turnaroundwoo/master
Feat: #60 Friends API 추가
- Loading branch information
Showing
12 changed files
with
88 additions
and
35 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
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
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
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
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
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 |
---|---|---|
@@ -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 {} |
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 |
---|---|---|
@@ -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 }); | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,3 @@ | ||
export interface IFriendsServiceDelete { | ||
opponentId: string; | ||
} |
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
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
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