Skip to content

Commit

Permalink
Merge pull request #108 from classbinu/jinhwan
Browse files Browse the repository at this point in the history
책 수정, 삭제 수정
  • Loading branch information
classbinu authored Feb 16, 2024
2 parents d3dcf8f + eca8fcc commit d86a4a8
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 63 deletions.
29 changes: 0 additions & 29 deletions src/auth/auth_test.http

This file was deleted.

13 changes: 0 additions & 13 deletions src/books/books-test.http

This file was deleted.

8 changes: 5 additions & 3 deletions src/books/books.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ export class BookMongoRepository {
writerId: string,
): Promise<Book> {
try {
const book = await this.bookModel.findById(id).exec();
const bookId = new Types.ObjectId(id);
const book = await this.bookModel.findById(bookId).exec();
if (!book) {
throw new NotFoundException('Book not found');
}
Expand All @@ -166,15 +167,16 @@ export class BookMongoRepository {
}

async deleteBook(id: string, writerId: string): Promise<Book> {
const book = await this.bookModel.findById(id).exec();
const bookId = new Types.ObjectId(id);
const book = await this.bookModel.findById(bookId).exec();
if (!book) {
throw new NotFoundException('Book not found');
}

if (book.userId.toString() !== writerId) {
throw new UnauthorizedException('You are not the writer of this book');
}
return this.bookModel.findByIdAndDelete(id).exec();
return this.bookModel.findByIdAndDelete(bookId).exec();
}

async updateBookLike(id: string, book: Book) {
Expand Down
20 changes: 2 additions & 18 deletions src/books/books.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { Injectable } from '@nestjs/common';

import { Book } from './schema/book.schema';
import { BookHistory } from './schema/book-history.schema';
Expand Down Expand Up @@ -87,26 +87,10 @@ export class BooksService {
updateBookDto: UpdateBookDto,
writerId: string,
): Promise<Book> {
try {
const book = await this.bookRepository.findBookById(id);

if (book.userId.toString() !== writerId) {
throw new UnauthorizedException();
}

return this.bookRepository.updateBook(id, updateBookDto, writerId);
} catch (error) {
throw new Error(`책 업데이트 실패: ${error.message}`);
}
return this.bookRepository.updateBook(id, updateBookDto, writerId);
}

async deleteBook(id: string, writerId: string): Promise<Book> {
const book = await this.bookRepository.findBookById(id);

if (book.userId.toString() !== writerId) {
throw new UnauthorizedException();
}

return this.bookRepository.deleteBook(id, writerId);
}

Expand Down

0 comments on commit d86a4a8

Please sign in to comment.