Skip to content

Commit

Permalink
Merge pull request #53 from modern-agile-team/feature/board-user
Browse files Browse the repository at this point in the history
refactor(2swo/Board-user) 케이스 적용
  • Loading branch information
2swo authored Oct 19, 2023
2 parents 3ec6c2e + 465e701 commit 40f02de
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/boards/controllers/Boards.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class BoardsController {

@Post()
async create(
@Headers('accessToken') accessToken: string,
@Headers('access_token') accessToken: string,
@Body() createBoardDto: CreateBoardDto,
): Promise<Board> {
const userId = await this.tokenService.decodeToken(accessToken);
Expand Down Expand Up @@ -76,7 +76,7 @@ export class BoardsController {

@Delete(':boardId')
async deleteBoard(
@Headers('accessToken') accessToken: string,
@Headers('access_token') accessToken: string,
@Param('boardId') boardId: number,
) {
const userId = await this.tokenService.decodeToken(accessToken);
Expand Down
12 changes: 7 additions & 5 deletions src/friends/controllers/friends.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Body, Controller, Headers, Post } from "@nestjs/common";
import { FriendsService } from "../services/friends.service";
import { TokenService } from "src/auth/services/token.service";
import { User } from "src/users/entities/user.entity";
import { Controller, Headers, Post } from '@nestjs/common';
import { FriendsService } from '../services/friends.service';
import { TokenService } from 'src/auth/services/token.service';

@Controller('friends')
export class FriendsController {
Expand All @@ -11,7 +10,10 @@ export class FriendsController {
) {}

@Post()
async friendRequest(@Headers('access_token') accessToken: string, @Headers('friend_id') friendId:User) {
async friendRequest(
@Headers('access_token') accessToken: string,
@Headers('friend_id') friendId: number,
) {
const userId = await this.tokenService.decodeToken(accessToken);
return await this.friendsService.friendRequest(userId, friendId);
}
Expand Down
18 changes: 14 additions & 4 deletions src/friends/entities/friends.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,23 @@ export class Friend {
@PrimaryGeneratedColumn()
id: number;

@ManyToOne(() => User)
@Column({ name: 'requester_id' })
requesterId: number;

@Column({ name: 'respondent_id' })
respondentId: number;

@ManyToOne(() => User, (userId: User) => userId.requester, {
onDelete: 'CASCADE',
})
@JoinColumn({ name: 'requester_id' })
requesterId: User;
requester: User;

@ManyToOne(() => User)
@ManyToOne(() => User, (userId: User) => userId.respondent, {
onDelete: 'CASCADE',
})
@JoinColumn({ name: 'respondent_id' })
respondentId: User;
respondent: User;

@Column({ type: 'enum', enum: Status, default: Status.PENDING })
status!: Status;
Expand Down
15 changes: 6 additions & 9 deletions src/friends/repositories/friends.repository.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import { Injectable } from "@nestjs/common";
import { EntityManager } from "typeorm";
import { Friend, Status } from "../entities/friends.entity";
import { User } from "src/users/entities/user.entity";
import { Injectable } from '@nestjs/common';
import { EntityManager } from 'typeorm';
import { Friend, Status } from '../entities/friends.entity';

@Injectable()
export class FriendsRepository {
constructor(
private readonly entityManager: EntityManager,
) {}
constructor(private readonly entityManager: EntityManager) {}

async friendRequest(userId: User, friendId: User): Promise<Friend> {
async friendRequest(userId: number, friendId: number): Promise<Friend> {
const friend = new Friend();
friend.requesterId = userId;
friend.respondentId = friendId;
friend.status = Status.PENDING;

return await this.entityManager.save(friend);
}
}
}
13 changes: 5 additions & 8 deletions src/friends/services/friends.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { Injectable } from "@nestjs/common";
import { FriendsRepository } from "../repositories/friends.repository";
import { User } from "src/users/entities/user.entity";
import { Injectable } from '@nestjs/common';
import { FriendsRepository } from '../repositories/friends.repository';

@Injectable()
export class FriendsService {
constructor(
private readonly friendsRepository: FriendsRepository,
) {}
constructor(private readonly friendsRepository: FriendsRepository) {}

async friendRequest(userId: User, friendId: User) {
async friendRequest(userId: number, friendId: number) {
return await this.friendsRepository.friendRequest(userId, friendId);
}
}
}
13 changes: 13 additions & 0 deletions src/users/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { UserImage } from './user-image.entity';
import { Token } from 'src/auth/entities/token.entity';
import { Board } from 'src/boards/entities/board.entity';
import { Friend } from 'src/friends/entities/friends.entity';

@Entity({
name: 'user',
Expand Down Expand Up @@ -43,4 +44,16 @@ export class User {
onDelete: 'CASCADE',
})
token: Token;

@OneToMany(() => Friend, (friend) => friend.requesterId, {
onDelete: 'CASCADE',
})
@JoinColumn({ name: 'requester_id' })
requester: Friend;

@OneToMany(() => Friend, (friend) => friend.respondentId, {
onDelete: 'CASCADE',
})
@JoinColumn({ name: 'respondent_id' })
respondent: Friend;
}

0 comments on commit 40f02de

Please sign in to comment.