Skip to content

Commit

Permalink
merge :: apply friend list api (#16)
Browse files Browse the repository at this point in the history
* add : apply friend list

* chore :: code format

* fix :: find element
  • Loading branch information
jyk1029 authored Mar 4, 2024
1 parent 855430e commit 9f1a5ac
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 13 deletions.
14 changes: 11 additions & 3 deletions src/application/domain/user/controller/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Controller, Param, Post, Req, UseFilters, UseGuards } from "@nestjs/common";
import { Controller, Get, Param, Post, UseFilters, UseGuards } from "@nestjs/common";
import { GlobalExceptionFilter } from "../../../../infrastructure/global/filter/global.exception.filter";
import { UserService } from "../service/user.service";
import { AuthGuard } from "@nestjs/passport";
import { CurrentUser } from "../../../../infrastructure/global/decorator/current-user";
import { UserEntity } from "../../../../infrastructure/domain/user/persistence/user.entity";

@UseFilters(GlobalExceptionFilter)
@Controller('user')
Expand All @@ -13,7 +15,13 @@ export class UserController {

@UseGuards(AuthGuard())
@Post('apply/:id')
async applyFriend(@Req() req) {
await this.userService.applyFriend(req)
async applyFriend(@Param() id, @CurrentUser() user: UserEntity) {
await this.userService.applyFriend(id, user)
}

@UseGuards(AuthGuard())
@Get('apply')
async queryApplyFriend(@CurrentUser() user: UserEntity) {
return await this.userService.queryApplyFriend(user)
}
}
9 changes: 9 additions & 0 deletions src/application/domain/user/dto/user.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class QueryApplyFriendListResponse {
users: FriendListElement[]
}

export class FriendListElement {
nickname: string
profile: string
isTurnOn: boolean
}
42 changes: 37 additions & 5 deletions src/application/domain/user/service/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,55 @@
import { HttpException, Injectable } from "@nestjs/common";
import { HttpException, HttpStatus, Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { FriendApplyEntity } from "../../../../infrastructure/domain/user/persistence/friend.apply.entity";
import { FriendListElement, QueryApplyFriendListResponse } from "../dto/user.dto";
import { UserEntity } from "../../../../infrastructure/domain/user/persistence/user.entity";

@Injectable()
export class UserService {
constructor(
@InjectRepository(FriendApplyEntity)
private friendApplyRepository: Repository<FriendApplyEntity>
private friendApplyRepository: Repository<FriendApplyEntity>,
@InjectRepository(UserEntity)
private userRepository: Repository<UserEntity>,
) {
}

async applyFriend(req) {
async applyFriend(id, req) {
let friendApply = new FriendApplyEntity();
let friend = await this.userRepository.findOne({ where: id })

if (!friend) {
throw new HttpException('User(Friend) Not Found', HttpStatus.NOT_FOUND)
}

if (await this.friendApplyRepository.existsBy({ requestUserId: req, receiveUserId: friend })) {
throw new HttpException('Already Apply', HttpStatus.CONFLICT)
}

friendApply.status = 'WAIT'
friendApply.requestUserId = req.authInfo.id
friendApply.receiveUserId = req.user.id
friendApply.requestUserId = req
friendApply.receiveUserId = friend

await this.friendApplyRepository.save(friendApply)
}

async queryApplyFriend(req) {

let friendList = await this.friendApplyRepository.findBy({ receiveUserId: req })
let friendListResponse = new QueryApplyFriendListResponse();

friendListResponse.users = await Promise.all(friendList.map(async (friend) => {
let requestUser = await this.userRepository.findOneBy(friend.requestUserId);
let element = new FriendListElement();

element.nickname = requestUser.nickname
element.profile = requestUser.profile
element.isTurnOn = requestUser.isTurnOn

return element
}))

return friendListResponse
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ export class FriendApplyEntity {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column('enum', { enum: ['WAIT', 'ACCEPT', 'REJECT'], nullable: false, default: 'WAIT' })
@Column('enum', { enum: [ 'WAIT', 'ACCEPT', 'REJECT' ], nullable: false, default: 'WAIT' })
status; // WAIT = 대기 / ACCEPT = 수락 / REJECT = 거절

@ManyToOne(() => UserEntity)
@ManyToOne(() => UserEntity, { lazy: true })
@JoinColumn({ name: 'request_user_id' })
requestUserId: string;
requestUserId: UserEntity;

@ManyToOne(() => UserEntity)
@ManyToOne(() => UserEntity, { lazy: true })
@JoinColumn({ name: 'receive_user_id' })
receiveUserId: string;
receiveUserId: UserEntity;
}
3 changes: 3 additions & 0 deletions src/infrastructure/domain/user/persistence/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export class UserEntity {
@Column('varchar', { nullable: false, length: 3000 })
profile: string;

@Column('boolean', { nullable: false, default: true })
isTurnOn: boolean;

@Column('enum', { enum: [ 'LOCAL', 'GOOGLE', 'KAKAO', 'NAVER' ], nullable: false })
provider;
}

0 comments on commit 9f1a5ac

Please sign in to comment.