Skip to content

Commit

Permalink
raw query에서 typerom의 함수들로 변경중
Browse files Browse the repository at this point in the history
  • Loading branch information
novice0840 committed Jul 31, 2023
1 parent a6e4d1a commit 59c58e9
Show file tree
Hide file tree
Showing 13 changed files with 10,471 additions and 91 deletions.
10,338 changes: 10,338 additions & 0 deletions backend/sample.json

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import { AuthModule } from './auth/auth.module';
import { ChattingModule } from './chatting/chatting.module';
import { UserModule } from './user/user.module';
import { CommentModule } from './comment/comment.module';
import { Chapter, Chatting, Comment, User, Webtoon } from './entity';
import { Chapter } from './entity/chapter.entity';
import { Webtoon } from './entity/webtoon.entity';
import { Chatting } from './entity/chatting.entity';
import { Comment } from './entity/comment.entity';
import { User } from './entity/user.entity';

@Module({
imports: [
Expand All @@ -22,7 +26,8 @@ import { Chapter, Chatting, Comment, User, Webtoon } from './entity';
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
entities: [Chapter, Chatting, Comment, User, Webtoon],
entities: [Webtoon, Chapter, Comment, User, Chatting],
autoLoadEntities: true,
synchronize: true,
}),
ScheduleModule.forRoot(),
Expand Down
3 changes: 2 additions & 1 deletion backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ export class AuthService {
await queryRunner.connect();
const hashedPassword = await hash(createUserDto.password, 10);
try {
await queryRunner.manager.query(
const user = await queryRunner.manager.query(
`insert into user (email, name, hashed_password) values (${createUserDto.email}, ${createUserDto.name}, ${hashedPassword})`,
);
return user;
} catch (error) {
console.log(error);
} finally {
Expand Down
1 change: 1 addition & 0 deletions backend/src/cronjob/crawler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export const crawlingWebtoon = async (day) => {
console.log(error);
}
}

await browser.close();
return webtoons;
};
Expand Down
5 changes: 5 additions & 0 deletions backend/src/cronjob/cronjob.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { CronjobService } from './cronjob.service';
export class CronjobController {
constructor(private readonly cronjobService: CronjobService) {}

@Get('test')
test() {
return this.cronjobService.test();
}

@Get('/init/all')
initAll() {
return this.cronjobService.initAll();
Expand Down
9 changes: 8 additions & 1 deletion backend/src/cronjob/cronjob.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Module } from '@nestjs/common';
import { CronjobService } from './cronjob.service';
import { CronjobController } from './cronjob.controller';
import { Webtoon } from 'src/entity/webtoon.entity';
import { Chapter } from 'src/entity/chapter.entity';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({ providers: [CronjobService], controllers: [CronjobController] })
@Module({
imports: [TypeOrmModule.forFeature([Webtoon, Chapter])],
providers: [CronjobService],
controllers: [CronjobController],
})
export class CronjobModule {}
77 changes: 53 additions & 24 deletions backend/src/cronjob/cronjob.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,32 @@ import {
crawlingChapter,
crawlingRecentChapter,
} from './crawler';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { Webtoon } from 'src/entity/webtoon.entity';
import { Chapter } from 'src/entity/chapter.entity';
@Injectable()
export class CronjobService {
constructor(private readonly datasource: DataSource) {}
constructor(
private readonly datasource: DataSource,
@InjectRepository(Webtoon) private webtoonRepository: Repository<Webtoon>,
@InjectRepository(Chapter) private chapterRepository: Repository<Chapter>,
) {}

test() {
const sample = {
id: 1,
title: 'sample',
author: 'sample',
thumbnail: 'sample',
dayOfWeek: 'das',
starScore: 1,
tags: 'sample',
description: 'sample',
interestCount: 1,
};
return this.webtoonRepository.save([sample]);
}

async initAll() {
await this.initDayOfWeek('MONDAY');
Expand All @@ -35,29 +58,35 @@ export class CronjobService {
const queryRunner = this.datasource.createQueryRunner();
await queryRunner.connect();
try {
let i = 1;
for await (const webtoon of webtoons) {
await queryRunner.manager.query(`
INSERT INTO webtoon (id, title, author, day_of_week, thumbnail, interest_count, star_score, description, tags)
VALUES (${webtoon.id},"${webtoon.title}", "${
webtoon.author
}", '${JSON.stringify(webtoon.dayOfWeek)}', "${webtoon.thumbnail}",
${webtoon.interestCount}, ${webtoon.starScore}, '${
webtoon.description
}', '${JSON.stringify(webtoon.tags)}')
ON DUPLICATE KEY UPDATE title="${webtoon.title}", author="${
webtoon.author
}", day_of_week='${JSON.stringify(webtoon.dayOfWeek)}', thumbnail="${
webtoon.thumbnail
}", interest_count=${webtoon.interestCount}, star_score=${
webtoon.starScore
}, description="${webtoon.description}", tags='${JSON.stringify(
webtoon.tags,
)}';
`);
console.log(`웹툰 ${i}개 update 완료`);
i += 1;
}
// let i = 1;
const changedWebtoon = webtoons.map((webtoon) => ({
...webtoon,
tags: JSON.stringify(webtoon.tags),
dayOfWeek: JSON.stringify(webtoon.dayOfWeek),
}));
return this.webtoonRepository.save([changedWebtoon]);
// for await (const webtoon of webtoons) {
// await queryRunner.manager.query(`
// INSERT INTO webtoon (id, title, author, day_of_week, thumbnail, interest_count, star_score, description, tags)
// VALUES (${webtoon.id},"${webtoon.title}", "${
// webtoon.author
// }", '${JSON.stringify(webtoon.dayOfWeek)}', "${webtoon.thumbnail}",
// ${webtoon.interestCount}, ${webtoon.starScore}, '${
// webtoon.description
// }', '${JSON.stringify(webtoon.tags)}')
// ON DUPLICATE KEY UPDATE title="${webtoon.title}", author="${
// webtoon.author
// }", day_of_week='${JSON.stringify(webtoon.dayOfWeek)}', thumbnail="${
// webtoon.thumbnail
// }", interest_count=${webtoon.interestCount}, star_score=${
// webtoon.starScore
// }, description="${webtoon.description}", tags='${JSON.stringify(
// webtoon.tags,
// )}';
// `);
// console.log(`웹툰 ${i}개 update 완료`);
// i += 1;
// }
} catch (err) {
console.log(err);
} finally {
Expand Down
30 changes: 12 additions & 18 deletions backend/src/entity/chapter.entity.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
import { Entity, Column, PrimaryColumn, ManyToOne, JoinColumn } from 'typeorm';
import Webtoon from './webtoon.entity';
import { Webtoon } from './webtoon.entity';

@Entity()
export default class Chapter {
export class Chapter {
@PrimaryColumn()
id: number;

@PrimaryColumn()
webtoon_id: number;

@Column({ nullable: true })
@Column()
name: string;

@Column({ default: 0 })
average_star: number;
@Column({ name: 'average_star', type: 'float' })
averageStar: number;

@Column({ default: 0 })
total_star: number;
@Column({ name: 'total_star', type: 'float' })
totalStar: number;

@Column({ type: 'date', nullable: true })
upload_date: string;
@Column({ type: 'date', name: 'upload_date' })
uploadDate: string;

@Column({ default: 0 })
star_score: number;

@Column({ nullable: true })
@Column()
thumbnail: string;

@Column({ nullable: true })
comment_number: number;

@Column({ nullable: true })
like_count: number;
@Column({ name: 'like_count' })
likeCount: number;

@ManyToOne(() => Webtoon, (webtoon) => webtoon.id)
@JoinColumn({ name: 'webtoon_id' })
Expand Down
14 changes: 7 additions & 7 deletions backend/src/entity/chatting.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
DeleteDateColumn,
JoinColumn,
} from 'typeorm';
import User from './user.entity';
import { User } from './user.entity';

@Entity()
export default class Chatting {
export class Chatting {
@PrimaryGeneratedColumn('uuid')
id: string;

Expand All @@ -20,13 +20,13 @@ export default class Chatting {
@Column()
content: string;

@CreateDateColumn()
created_at: Date;
@CreateDateColumn({ name: 'created_at' })
createdAt: Date;

@DeleteDateColumn()
deleted_at: Date;
@DeleteDateColumn({ name: 'deleted_at' })
deletedAt: Date;

@ManyToOne(() => User, (user) => user.chatting)
@ManyToOne(() => User, (user) => user.chattings)
@JoinColumn({ name: 'writer_id' })
user: User;
}
14 changes: 7 additions & 7 deletions backend/src/entity/comment.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
JoinColumn,
ManyToOne,
} from 'typeorm';
import User from './user.entity';
import Webtoon from './webtoon.entity';
import { User } from './user.entity';
import { Webtoon } from './webtoon.entity';

@Entity()
export default class Comment {
export class Comment {
@PrimaryGeneratedColumn('uuid')
id: string;

Expand All @@ -30,11 +30,11 @@ export default class Comment {
@Column({ default: 0 })
dislike: number;

@CreateDateColumn()
created_at: Date;
@CreateDateColumn({ name: 'created_at' })
createdAt: Date;

@DeleteDateColumn()
deleted_at: Date;
@DeleteDateColumn({ name: 'deleted_at' })
deletedAt: Date;

@ManyToOne(() => User, (user) => user.comments)
@JoinColumn({ name: 'writer_id' })
Expand Down
12 changes: 6 additions & 6 deletions backend/src/entity/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Chapter from './chapter.entity';
import Chatting from './chatting.entity';
import Comment from './comment.entity';
import User from './user.entity';
import Webtoon from './webtoon.entity';
// import Chapter from './chapter.entity';
// import Chatting from './chatting.entity';
// import Comment from './comment.entity';
// import User from './user.entity';
// import Webtoon from './webtoon.entity';

export { Chapter, Chatting, Comment, User, Webtoon };
// export { Chapter, Chatting, Comment, User, Webtoon };
20 changes: 10 additions & 10 deletions backend/src/entity/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
DeleteDateColumn,
OneToMany,
} from 'typeorm';
import Comment from './comment.entity';
import Chatting from './chatting.entity';
import { Comment } from './comment.entity';
import { Chatting } from './chatting.entity';

@Entity()
export default class User {
export class User {
@PrimaryGeneratedColumn('uuid')
id: string;

Expand All @@ -24,18 +24,18 @@ export default class User {
@Column()
hashed_password: string;

@CreateDateColumn()
created_at: Date;
@CreateDateColumn({ name: 'created_at' })
createdAt: Date;

@UpdateDateColumn()
updated_at: Date;
@UpdateDateColumn({ name: 'updated_at' })
updatedAt: Date;

@DeleteDateColumn()
deleted_at: Date;
@DeleteDateColumn({ name: 'deleted_at' })
deletedAt: Date;

@OneToMany(() => Comment, (comment) => comment.user)
comments: Comment[];

@OneToMany(() => Chatting, (chatting) => chatting.user)
chatting: Chatting[];
chattings: Chatting[];
}
30 changes: 15 additions & 15 deletions backend/src/entity/webtoon.entity.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import { Entity, Column, PrimaryColumn, OneToMany } from 'typeorm';
import Chapter from './chapter.entity';
import Comment from './comment.entity';
import { Chapter } from './chapter.entity';
import { Comment } from './comment.entity';

@Entity()
export default class Webtoon {
export class Webtoon {
@PrimaryColumn()
id: number;

@Column({ nullable: true })
title?: string;
@Column()
title: string;

@Column({ nullable: true })
@Column()
author: string;

@Column({ nullable: true })
day_of_week: string;
@Column({ name: 'day_of_week' })
dayOfWeek: string;

@Column({ nullable: true })
@Column()
thumbnail: string;

@Column({ default: 0 })
interest_count: number;
@Column({ name: 'interest_count' })
interestCount: number;

@Column({ default: 0 })
star_score: number;
@Column({ name: 'star_score' })
starScore: number;

@Column({ type: 'text', nullable: true })
@Column({ type: 'text' })
description: string;

@Column({ nullable: true })
@Column()
tags: string;

@OneToMany(() => Chapter, (chapter) => chapter.webtoon)
Expand Down

0 comments on commit 59c58e9

Please sign in to comment.