Skip to content

Commit

Permalink
Merge pull request #126 from classbinu/jinhwan
Browse files Browse the repository at this point in the history
Jinhwan
  • Loading branch information
classbinu authored Feb 19, 2024
2 parents c062959 + 691bf95 commit 41f61f7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 33 deletions.
6 changes: 3 additions & 3 deletions src/ai/ai.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ export class AiService {

// 기존 삽화를 재생성하는 함수
async updateAiBooksImages(id: string, page: string, userId: string) {
const book = await this.booksService.findBookById(id);
const book = await this.booksService.findBookByBookId(id);
if (!book) {
throw new Error('Book not found');
}
Expand Down Expand Up @@ -419,7 +419,7 @@ export class AiService {
}

async generateNewBookImages(id: string, page: string): Promise<string[]> {
const book = await this.booksService.findBookById(id);
const book = await this.booksService.findBookByBookId(id);
const prompt = book.body.get(page).imagePrompt;
const imageStyle = book.imageStyle || 'cartoon';

Expand All @@ -440,7 +440,7 @@ export class AiService {
userId: string,
base64Dto: Base64Dto,
) {
const book = await this.booksService.findBookById(id);
const book = await this.booksService.findBookByBookId(id);
if (!book) {
throw new Error('Book not found');
}
Expand Down
20 changes: 10 additions & 10 deletions src/books/books.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ export class BooksController {
// 모든책들 가져오는 함수 추가해야함.

@Get(':id')
async findBookById(@Param('id') id: string): Promise<Book> {
return this.booksService.findBookById(id);
async findBookByBookId(@Param('id') id: string): Promise<Book> {
return this.booksService.findBookByBookId(id);
}

@UseGuards(AccessTokenGuard)
Expand All @@ -150,31 +150,31 @@ export class BooksController {
@ApiBearerAuth()
@Post(':bookId/likes')
async addLike(@Param('bookId') bookId: string, @Req() req) {
const userId = req.user.sub;
return await this.booksService.addLike(userId, bookId);
const userObjectId = req.user.sub;
return await this.booksService.addLike(userObjectId, bookId);
}

@UseGuards(AccessTokenGuard)
@ApiBearerAuth()
@Delete(':bookId/likes')
async removeLike(@Param('bookId') bookId: string, @Req() req) {
const userId = req.user.sub;
return await this.booksService.removeLike(userId, bookId);
const userObjectId = req.user.sub;
return await this.booksService.removeLike(userObjectId, bookId);
}

@UseGuards(AccessTokenGuard)
@ApiBearerAuth()
@Post(':bookId/dislikes')
async addDislike(@Param('bookId') bookId: string, @Req() req) {
const userId = req.user.sub;
return await this.booksService.addDislike(userId, bookId);
const userObjectId = req.user.sub;
return await this.booksService.addDislike(userObjectId, bookId);
}

@UseGuards(AccessTokenGuard)
@ApiBearerAuth()
@Delete(':bookId/dislikes')
async removeDislike(@Param('bookId') bookId: string, @Req() req) {
const userId = req.user.sub;
return await this.booksService.removeDislike(userId, bookId);
const userObjectId = req.user.sub;
return await this.booksService.removeDislike(userObjectId, bookId);
}
}
18 changes: 9 additions & 9 deletions src/books/books.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export class BookMongoRepository {
return { total: totalCount, books };
}

async findBookById(id: string): Promise<Book> {
async findBookByBookId(id: string): Promise<Book> {
try {
const book = await this.bookModel
.findByIdAndUpdate(id, { $inc: { count: 1 } }, { new: true })
Expand All @@ -134,7 +134,7 @@ export class BookMongoRepository {
}
return book;
} catch (error) {
Logger.error(`findBookById 실패: ${error.message}`);
Logger.error(`findBookByBookId 실패: ${error.message}`);
throw new NotFoundException(
`책 불러오기 실패했어요. 다시 시도해 주세요.`,
);
Expand Down Expand Up @@ -190,18 +190,18 @@ export class BookMongoRepository {
}
}

async addLike(userId: string, bookId: string) {
async addLike(userObjectId: string, bookId: string) {
try {
const book = await this.findBookById(bookId);
const book = await this.findBookByBookId(bookId);
if (!book) {
throw new NotFoundException('책이 없어요.');
}

if (book.likes.includes(new Types.ObjectId(userId))) {
if (book.likes.includes(new Types.ObjectId(userObjectId))) {
throw new BadRequestException('이미 좋아요를 누르셨어요.');
}

book.likes.push(new Types.ObjectId(userId));
book.likes.push(new Types.ObjectId(userObjectId));
book.likesCount = book.likes.length;

return await this.updateBookLike(bookId, book);
Expand All @@ -213,7 +213,7 @@ export class BookMongoRepository {

async removeLike(userId: string, bookId: string) {
try {
const book = await this.findBookById(bookId);
const book = await this.findBookByBookId(bookId);
if (!book) {
throw new NotFoundException('책이 없어요.');
}
Expand Down Expand Up @@ -255,7 +255,7 @@ export class BookMongoRepository {

async addDislike(userId: string, bookId: string) {
try {
const book = await this.findBookById(bookId);
const book = await this.findBookByBookId(bookId);
if (!book) {
throw new NotFoundException('책이 없어요.');
}
Expand All @@ -274,7 +274,7 @@ export class BookMongoRepository {

async removeDislike(userId: string, bookId: string) {
try {
const book = await this.findBookById(bookId);
const book = await this.findBookByBookId(bookId);
if (!book) {
throw new NotFoundException('책이 없어요.');
}
Expand Down
12 changes: 6 additions & 6 deletions src/books/books.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export class BooksService {
return this.bookRepository.findAllBooks(query, page, limit);
}

async findBookById(id: string): Promise<Book> {
return this.bookRepository.findBookById(id);
async findBookByBookId(id: string): Promise<Book> {
return this.bookRepository.findBookByBookId(id);
}

async updateBook(
Expand All @@ -96,16 +96,16 @@ export class BooksService {
return this.bookRepository.deleteBook(id, writerId);
}

async addLike(userId: string, bookId: string) {
async addLike(userObjectId: string, bookId: string) {
try {
const result = await this.bookRepository.addLike(userId, bookId);
const result = await this.bookRepository.addLike(userObjectId, bookId);

// 책을 쓴 유저의 아이디를 가져옵니다.
const authorBook = await this.bookRepository.findBookById(bookId);
const authorBook = await this.bookRepository.findBookByBookId(bookId);
const authorInfo = await this.usersService.findById(
authorBook.userId.toString(),
);
const userInfo = await this.usersService.findById(userId);
const userInfo = await this.usersService.findById(userObjectId);
// 알림 보내기
const authorSocketId = this.notiGateway.getUserSocketId(
authorInfo._id.toString(),
Expand Down
13 changes: 8 additions & 5 deletions src/noti/noti.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ export class NotiService {
return createdNoti.save();
}

async findMissedNotifications(userId: string): Promise<NotiDocument[]> {
return this.notiModel.find({ receiverId: userId, status: 'unread' }).exec();
async findMissedNotifications(userObjectId: string): Promise<NotiDocument[]> {
return this.notiModel
.find({ receiverId: userObjectId, status: 'unread' })
.exec();
}

async markAsRead(notifications: NotiDocument[]): Promise<void> {
Expand All @@ -30,10 +32,11 @@ export class NotiService {
}
}

async sendMissedNotifications(userId: string) {
const userSocketId = this.notiGateway.getUserSocketId(userId);
async sendMissedNotifications(userObjectId: string) {
const userSocketId = this.notiGateway.getUserSocketId(userObjectId);
if (userSocketId) {
const missedNotifications = await this.findMissedNotifications(userId);
const missedNotifications =
await this.findMissedNotifications(userObjectId);
this.notiGateway.server
.to(userSocketId)
.emit('missedNotifications', missedNotifications);
Expand Down

0 comments on commit 41f61f7

Please sign in to comment.