Skip to content

Commit

Permalink
Merge branch 'BE' into BE-setRedis-#356
Browse files Browse the repository at this point in the history
  • Loading branch information
koomin1227 authored Dec 9, 2023
2 parents 854030a + ba21b38 commit bcc5bf7
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 99 deletions.
117 changes: 64 additions & 53 deletions BE/src/chat/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,63 +69,65 @@ export class ChatService {
}

async findRoomList(userId: string) {
let now = new Set();
const subquery = this.chatRepository
.createQueryBuilder('chat')
.select('chat.id', 'id')
.addSelect('chat.chat_room', 'chat_room')
.addSelect('chat.message', 'message')
.addSelect('chat.create_date', 'create_date')
.where(
'chat.id IN (SELECT MAX(chat.id) FROM chat GROUP BY chat.chat_room)',
);

const rooms = await this.chatRoomRepository
.createQueryBuilder('chat_room')
.innerJoin(
'(' + subquery.getQuery() + ')',
'chat_info',
'chat_room.id = chat_info.chat_room',
)
.leftJoin(
'chat_room.writerUser',
'writer',
'chat_room.writerUser = writer.user_hash',
)
.leftJoin(
'chat_room.userUser',
'user',
'chat_room.userUser = user.user_hash',
)
.leftJoin('chat_room.post', 'post', 'chat_room.post = post.id')
.select([
'chat_room.user',
'chat_room.writer',
'chat_room.id',
'chat_room.post_id',
'chat.message',
'chat.create_date',
'chat_room.id as room_id',
'chat_room.writer as writer',
'writer.nickname as writer_nickname',
'writer.profile_img as writer_profile_img',
'chat_room.user as user',
'user.nickname as user_nickname',
'user.profile_img as user_profile_img',
'chat_room.post_id as post_id',
'post.title as post_title',
'post.thumbnail as post_thumbnail',
'chat_info.create_date as last_chat_date',
'chat_info.message as last_chat',
])
.where('chat_room.user = :userId', {
userId: userId,
})
.orWhere('chat_room.writer = :userId', {
userId: userId,
})
.leftJoin('chat', 'chat', 'chat_room.id = chat.chat_room')
.orderBy('chat.id', 'DESC')
.addSelect(['user.w.user_hash', 'user.w.profile_img', 'user.w.nickname'])
.leftJoin('user', 'user.w', 'user.w.user_hash = chat_room.writer')
.addSelect(['user.u.user_hash', 'user.u.profile_img', 'user.u.nickname'])
.leftJoin('user', 'user.u', 'user.u.user_hash = chat_room.user')
.addSelect(['post.thumbnail', 'post.title'])
.leftJoin('post', 'post', 'post.id = chat_room.post_id')
.where('chat_room.writer = :userId', { userId: userId })
.orWhere('chat_room.user = :userId', { userId: userId })
.orderBy('chat_info.create_date', 'DESC')
.getRawMany();

const result = rooms
.reduce((acc, cur) => {
acc.push({
room_id: cur.chat_room_id,
post_id: cur.chat_room_post_id,
post_title: cur.post_title,
post_thumbnail: cur.post_thumbnail,
user: cur['user.w_user_hash'],
user_profile_img: cur['user.w_profile_img'],
user_nickname: cur['user.w_nickname'],
writer: cur['user.u_user_hash'],
writer_profile_img: cur['user.u_profile_img'],
writer_nickname: cur['user.u_nickname'],
last_chat: cur.chat_message,
last_chat_date: cur.chat_create_date,
});
return acc;
}, [])
.sort((a, b) => {
return b.last_chat_date - a.last_chat_date;
})
.reduce((acc, cur) => {
if (!now.has(cur.room_id)) {
acc.push(cur);
now.add(cur.room_id);
}
return acc;
}, []);

return result;
return rooms.reduce((acc, cur) => {
cur.writer_profile_img =
cur.writer_profile_img === null
? this.configService.get('DEFAULT_PROFILE_IMAGE')
: cur.writer_profile_img;
cur.user_profile_img =
cur.user_profile_img === null
? this.configService.get('DEFAULT_PROFILE_IMAGE')
: cur.user_profile_img;
acc.push(cur);
return acc;
}, []);
}

async findRoomById(roomId: number, userId: string) {
Expand All @@ -142,12 +144,21 @@ export class ChatService {
where: {
id: roomId,
},
relations: ['chats'],
relations: ['chats', 'userUser', 'writerUser'],
});

this.checkAuth(room, userId);

return {
writer: room.writer,
writer_profile_img:
room.writerUser.profile_img === null
? this.configService.get('DEFAULT_PROFILE_IMAGE')
: room.writerUser.profile_img,
user: room.user,
user_profile_img:
room.userUser.profile_img === null
? this.configService.get('DEFAULT_PROFILE_IMAGE')
: room.userUser.profile_img,
post_id: room.post_id,
chat_log: room.chats,
};
Expand Down
6 changes: 3 additions & 3 deletions BE/src/post/post.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { MultiPartBody } from '../utils/multiPartBody.decorator';
import { PostListDto } from './dto/postList.dto';
import { AuthGuard } from 'src/utils/auth.guard';
import { UserHash } from 'src/utils/auth.decorator';
import { FileSizeValidator } from '../utils/files.validator';
import { FilesSizeValidator } from '../utils/files.validator';

@Controller('posts')
@ApiTags('posts')
Expand All @@ -41,7 +41,7 @@ export class PostController {
@Post()
@UseInterceptors(FilesInterceptor('image', 12))
async postsCreate(
@UploadedFiles(new FileSizeValidator())
@UploadedFiles(new FilesSizeValidator())
files: Array<Express.Multer.File>,
@MultiPartBody(
'post_info',
Expand All @@ -68,7 +68,7 @@ export class PostController {
@UseInterceptors(FilesInterceptor('image', 12))
async postModify(
@Param('id') id: number,
@UploadedFiles(new FileSizeValidator())
@UploadedFiles(new FilesSizeValidator())
files: Array<Express.Multer.File>,
@MultiPartBody(
'post_info',
Expand Down
6 changes: 1 addition & 5 deletions BE/src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ export class UsersController {
@Post()
@UseInterceptors(FileInterceptor('profileImage'))
async usersCreate(
@UploadedFile(
new ParseFilePipe({
validators: [new MaxFileSizeValidator({ maxSize: 1024 * 1024 * 20 })],
}),
)
@UploadedFile(new FileSizeValidator())
file: Express.Multer.File,
@MultiPartBody(
'profile',
Expand Down
29 changes: 8 additions & 21 deletions BE/src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,11 @@ export class UsersService {
file: Express.Multer.File,
userId: string,
) {
if (body === undefined) {
throw new HttpException('수정 할 것이 없는데 요청을 보냈습니다.', 400);
}
await this.checkAuth(id, userId);

const nickname = body.nickname;
const isImageChanged = body.is_image_changed;
if (nickname) {
await this.changeNickname(id, nickname);
if (body) {
await this.changeNickname(id, body.nickname);
}
if (isImageChanged !== undefined) {
if (file !== undefined) {
await this.changeImages(id, file);
}
}
Expand All @@ -128,18 +122,11 @@ export class UsersService {

async changeImages(userId: string, file: Express.Multer.File) {
try {
if (file === undefined) {
await this.userRepository.update(
{ user_hash: userId },
{ profile_img: null },
);
} else {
const fileLocation = await this.s3Handler.uploadFile(file);
await this.userRepository.update(
{ user_hash: userId },
{ profile_img: fileLocation },
);
}
const fileLocation = await this.s3Handler.uploadFile(file);
await this.userRepository.update(
{ user_hash: userId },
{ profile_img: fileLocation },
);
} catch (e) {
throw new HttpException('서버 오류입니다.', 500);
}
Expand Down
22 changes: 19 additions & 3 deletions BE/src/utils/files.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
} from '@nestjs/common';

@Injectable()
export class FileSizeValidator implements PipeTransform {
export class FilesSizeValidator implements PipeTransform {
transform(value: any, metadata: ArgumentMetadata): any {
if (value === undefined || value.length === 0) {
if (value.length === 0) {
return value;
}
const maxSize = 1024 * 1024 * 20;
Expand All @@ -20,6 +20,22 @@ export class FileSizeValidator implements PipeTransform {
);
}
}
return undefined;
return value;
}
}

export class FileSizeValidator implements PipeTransform {
transform(value: any, metadata: ArgumentMetadata): any {
if (value === undefined) {
return value;
}
const maxSize = 1024 * 1024 * 20;
const file: Express.Multer.File = value;
if (file.size > maxSize) {
throw new BadRequestException(
`File ${file.originalname} exceeds the allowed size limit`,
);
}
return value;
}
}
14 changes: 0 additions & 14 deletions BE/src/utils/redis.ts

This file was deleted.

0 comments on commit bcc5bf7

Please sign in to comment.