Skip to content

Commit

Permalink
fix: nullable이 아닌 엔티티 필드를 필수로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
scarf005 committed Jul 11, 2023
1 parent a868cbf commit 3316b9c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 41 deletions.
54 changes: 29 additions & 25 deletions backend/src/books/books.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,40 +147,44 @@ class BooksRepository extends Repository<Book> {
.getRawOne();
}

async updateBookInfo(bookInfo: UpdateBookInfo): Promise<void> {
await this.bookInfo.update(bookInfo.id, bookInfo as BookInfo);
async updateBookInfo({
id, categoryId, ...rest
}: UpdateBookInfo): Promise<void> {
await this.bookInfo.update(id, {
id,
categoryId: Number(categoryId),
...rest,
});
}

async updateBook(book: UpdateBook): Promise<void> {
await this.books.update(book.id, book as Book);
}

async createBookInfo(
target: CreateBookInfo,
): Promise<BookInfo> {
const bookInfo: BookInfo = {
title: target.title,
author: target.author,
publisher: target.publisher,
publishedAt: target.pubdate,
categoryId: Number(target.categoryId),
isbn: target.isbn,
image: target.image,
};
return this.bookInfo.save(bookInfo);
async createBookInfo({
title, author, publisher, pubdate, categoryId, isbn, image,
}: CreateBookInfo): Promise<BookInfo> {
return this.bookInfo.save({
title,
author,
publisher,
publishedAt: pubdate,
categoryId: Number(categoryId),
isbn,
image,
});
}

async createBook(
target: CreateBookInfo,
): Promise<void> {
const book: Book = {
donator: target.donator,
donatorId: target.donatorId,
callSign: target.callSign,
async createBook({
donator, donatorId, callSign, infoId,
}: CreateBookInfo): Promise<void> {
await this.books.save({
donator,
donatorId,
callSign,
infoId,
status: 0,
infoId: target.infoId,
};
await this.books.save(book);
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion backend/src/books/books.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export type UpdateBookInfo = {
title: string;
author: string;
publisher: string;
publishedAt: string | Date;
publishedAt: string;
image: string;
categoryId?: string;
}
Expand Down
4 changes: 2 additions & 2 deletions backend/src/entity/entities/Book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Reservation from './Reservation';

class Book {
@PrimaryGeneratedColumn({ type: 'int', name: 'id' })
id?: number;
id: number;

@Column('varchar', { name: 'donator', nullable: true, length: 255 })
donator: string | null;
Expand All @@ -26,7 +26,7 @@ class Book {
name: 'createdAt',
default: () => "'CURRENT_TIMESTAMP(6)'",
})
createdAt?: Date;
createdAt: Date;

@Column()
infoId: number;
Expand Down
26 changes: 13 additions & 13 deletions backend/src/entity/entities/BookInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import SuperTag from './SuperTag';
@Entity('book_info')
class BookInfo {
@PrimaryGeneratedColumn({ type: 'int', name: 'id' })
id?: number;
id: number;

@Column('varchar', { name: 'title', length: 255 })
title?: string;
title: string;

@Column('varchar', { name: 'author', length: 255 })
author?: string;
author: string;

@Column('varchar', { name: 'publisher', length: 255 })
publisher?: string;
publisher: string;

@Column('varchar', { name: 'isbn', nullable: true, length: 255 })
isbn?: string | null;
Expand All @@ -36,38 +36,38 @@ class BookInfo {
name: 'createdAt',
default: () => "'CURRENT_TIMESTAMP(6)'",
})
createdAt?: Date;
createdAt: Date;

@Column('datetime', {
name: 'updatedAt',
default: () => "'CURRENT_TIMESTAMP(6)'",
})
updatedAt?: Date;
updatedAt: Date;

@Column('int', { name: 'categoryId' })
categoryId?: number;
categoryId: number;

@OneToMany(() => Book, (book) => book.info)
books?: Book[];
books: Book[];

@ManyToOne(() => Category, (category) => category.bookInfos, {
onDelete: 'NO ACTION',
onUpdate: 'NO ACTION',
})
@JoinColumn([{ name: 'categoryId', referencedColumnName: 'id' }])
category?: Category;
category: Category;

@OneToMany(() => Likes, (likes) => likes.bookInfo)
likes?: Likes[];
likes: Likes[];

@OneToMany(() => Reservation, (reservation) => reservation.bookInfo)
reservations?: Reservation[];
reservations: Reservation[];

@OneToMany(() => Reviews, (reviews) => reviews.bookInfo)
reviews?: Reviews[];
reviews: Reviews[];

@OneToMany(() => SuperTag, (superTags) => superTags.userId)
superTags?: SuperTag[];
superTags: SuperTag[];
}

export default BookInfo;

0 comments on commit 3316b9c

Please sign in to comment.