From 8b27208dfa196e7408033de3f8041f44255866c3 Mon Sep 17 00:00:00 2001 From: Lee sang Yeop Date: Sat, 4 Nov 2023 23:15:55 +0900 Subject: [PATCH 1/4] =?UTF-8?q?Fix:=20=EB=B6=88=ED=95=84=EC=9A=94=ED=95=9C?= =?UTF-8?q?=20=ED=8C=8C=EC=9D=BC=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/data/handler.ts | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/db/data/handler.ts diff --git a/src/db/data/handler.ts b/src/db/data/handler.ts deleted file mode 100644 index e69de29b..00000000 From ff2bd930c723177c7f54e962043187be0fb0f9f0 Mon Sep 17 00:00:00 2001 From: Lee sang Yeop Date: Sat, 4 Nov 2023 23:16:48 +0900 Subject: [PATCH 2/4] =?UTF-8?q?Fix:=20mysql2=20=EB=9D=BC=EC=9D=B4=EB=B8=8C?= =?UTF-8?q?=EB=9F=AC=EB=A6=AC=EC=9D=98=20=EC=BF=BC=EB=A6=AC=EB=AC=B8=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=20=EC=8B=9C=20promise=20=EB=A5=BC=20?= =?UTF-8?q?=EB=B0=98=ED=99=98=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/db/index.ts b/src/db/index.ts index 23cdd9b2..71afdc94 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -1,5 +1,5 @@ import env from '@config'; -import mysql from 'mysql2'; +import mysql from 'mysql2/promise'; let DBHost; From 40ed8cc73d82e7c034c03801179b5dd2541606c9 Mon Sep 17 00:00:00 2001 From: Lee sang Yeop Date: Sat, 4 Nov 2023 23:44:26 +0900 Subject: [PATCH 3/4] =?UTF-8?q?Fix:=20=EC=B4=88=EA=B8=B0=20=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=84=B0=EB=B2=A0=EC=9D=B4=EC=8A=A4=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=EC=8B=9C=20=ED=85=8C=EC=9D=B4=EB=B8=94=EC=9D=84=20?= =?UTF-8?q?ERD=20=ED=85=8C=EC=9D=B4=EB=B8=94=EC=97=90=20=EB=AA=85=EC=8B=9C?= =?UTF-8?q?=EB=90=9C=EB=8D=B0=EB=A1=9C=20=EC=83=9D=EC=84=B1=EB=90=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/table/createTables.ts | 205 ++++++++++++++++------------------- 1 file changed, 95 insertions(+), 110 deletions(-) diff --git a/src/db/table/createTables.ts b/src/db/table/createTables.ts index 758cde14..c6ef66aa 100644 --- a/src/db/table/createTables.ts +++ b/src/db/table/createTables.ts @@ -1,150 +1,135 @@ -import { College } from 'src/@types/college'; import db from 'src/db'; -const createDepartmentTable = () => { +const createDepartmentTable = async () => { const createTableQuery = `CREATE TABLE departments ( id INT PRIMARY KEY AUTO_INCREMENT, - collegeName VARCHAR(255) NOT NULL, - departmentName VARCHAR(255) NOT NULL, - departmentSubName VARCHAR(255) NOT NULL, - departmentLink VARCHAR(255) NOT NULL + college_name VARCHAR(50) NOT NULL, + department_name VARCHAR(100) NOT NULL, + department_subname VARCHAR(100) NOT NULL, + department_link VARCHAR(255) NOT NULL, + graduation_link VARCHAR(255) );`; - db.query(createTableQuery, (error) => { - if (error) { - console.log('학과 테이블 생성 실패', error); - } else { - console.log('학과 테이블 생성 성공!'); - } - }); -}; - -const createGraduationTable = () => { - const createGraduationQuery = `CREATE TABLE graduation ( - id INT PRIMARY KEY AUTO_INCREMENT, - department VARCHAR(255) NOT NULL, - link VARCHAR(255) NOT NULL - );`; - - db.query(createGraduationQuery, (error) => { - if (error) { - console.log('졸업요건 테이블 생성 실패', error); - } else { - console.log('졸업요건 테이블 생성 성공!'); - } - }); + try { + await db.execute(createTableQuery); + console.log('학과 테이블 생성 성공!'); + } catch (error) { + console.log('학과 테이블 생성 실패', error); + } }; -const createNoticeTable = (college: College[]) => { - for (const data of college) { - const major = - data.departmentSubName !== '-' - ? data.departmentSubName - : data.departmentName; - for (const tableName of [`${major}고정`, `${major}일반`]) { - const createTableQuery = `CREATE TABLE ${tableName} ( - id INT PRIMARY KEY AUTO_INCREMENT, +const createMajorNoticeTable = async () => { + const createTableQuery = `CREATE TABLE major_notices ( + id BIGINT PRIMARY KEY AUTO_INCREMENT, title VARCHAR(255) NOT NULL, link VARCHAR(255) NOT NULL UNIQUE, - uploadDate VARCHAR(255) NOT NULL + upload_date VARCHAR(20) NOT NULL, + rep_yn BOOLEAN, + department_id INT NOT NULL, + FOREIGN KEY (department_id) REFERENCES departments(id) );`; - - db.query(createTableQuery, (error) => { - if (error) { - console.log('테이블 생성 실패', error); - } else { - console.log('학과공지 테이블 생성 성공!'); - } - }); - } + try { + await db.execute(createTableQuery); + console.log('학과 공지사항 테이블 생성 성공!'); + } catch (error) { + console.log('학과 공지사항 테이블 생성 실패', error); } }; -const createSubscribeTable = (college: College[]) => { - for (const data of college) { - const major = - data.departmentSubName !== '-' - ? data.departmentSubName - : data.departmentName; +const createNoticeTable = async () => { + const createTableQuery = `CREATE TABLE notices ( + id INT PRIMARY KEY AUTO_INCREMENT, + title VARCHAR(255) NOT NULL, + link VARCHAR(255) NOT NULL UNIQUE, + uploadDate VARCHAR(20) NOT NULL, + author VARCHAR(50) NOT NULL, + rep_yn BOOLEAN, + category ENUM('SCHOOL', 'LANGUAGE') NOT NULL + );`; + + try { + await db.execute(createTableQuery); + console.log('공지사항 테이블 생성 성공!'); + } catch (error) { + console.log('공지사항 테이블 생성 실패', error); + } +}; - const tableName = `${major}구독`; - const createTableQuery = `CREATE TABLE ${tableName} ( - id INT PRIMARY KEY AUTO_INCREMENT, - user VARCHAR(600) NOT NULL UNIQUE +const createSubscribeTable = async () => { + const createTableQuery = `CREATE TABLE subscribe_users ( + id BIGINT PRIMARY KEY AUTO_INCREMENT, + user VARCHAR(600) NOT NULL UNIQUE, + department_id INT NOT NULL, + FOREIGN KEY (department_id) REFERENCES departments(id) );`; - db.query(createTableQuery, (error) => { - if (error) { - console.log('테이블 생성 실패', error); - } else { - console.log('학과구독 테이블 생성 성공!'); - } - }); + try { + await db.execute(createTableQuery); + console.log('알림 구독 테이블 생성 성공!'); + } catch (error) { + console.log('알림 구독 테이블 생성 실패', error); } }; -const createSchoolNoticeTable = () => { - for (const tableName of [`학교고정`, `학교일반`]) { - const createTableQuery = `CREATE TABLE ${tableName} ( - id INT PRIMARY KEY AUTO_INCREMENT, - title VARCHAR(255) NOT NULL, - link VARCHAR(255) NOT NULL UNIQUE, - uploadDate VARCHAR(255) NOT NULL - );`; +const createSubscribeKeywordTable = async () => { + const createTableQuery = `CREATE TABLE subscribe_keywords ( + id BIGINT PRIMARY KEY AUTO_INCREMENT, + keyword VARCHAR(100) NOT NULL UNIQUE, + user_id BIGINT NOT NULL, + FOREIGN KEY (user_id) REFERENCES subscribe_users(id) + );`; - db.query(createTableQuery, (error) => { - if (error) { - console.log('테이블 생성 실패', error); - } else { - console.log('학교 테이블 생성 성공!'); - } - }); + try { + await db.execute(createTableQuery); + console.log('알림 구독 키워드 테이블 생성 성공!'); + } catch (error) { + console.log('알림 구독 키워드 테이블 생성 실패', error); } }; -const createWhalebeDataTable = () => { - const createTableQuery = `CREATE TABLE 웨일비 ( - id INT PRIMARY KEY AUTO_INCREMENT, - title VARCHAR(255) NOT NULL UNIQUE, - date VARCHAR(255) NOT NULL, - imgUrl VARCHAR(255) NOT NULL, - link VARCHAR(255) NOT NULL +const createWhalebeDataTable = async () => { + const createTableQuery = `CREATE TABLE whalebe ( + id BIGINT PRIMARY KEY AUTO_INCREMENT, + title VARCHAR(255) NOT NULL, + link VARCHAR(255) NOT NULL UNIQUE, + operating_period VARCHAR(30) NOT NULL, + recruitment_period VARCHAR(30) NOT NULL, + imgurl VARCHAR(500) NOT NULL );`; - db.query(createTableQuery, (error) => { - if (error) { - console.log('웨일비 DB 생성 실패', error); - return; - } + try { + await db.execute(createTableQuery); console.log('웨일비 테이블 생성 성공!'); - }); + } catch (error) { + console.log('웨일비 테이블 생성 실패', error); + } }; -const createLanguageDataTable = () => { - const createTableQuery = `CREATE TABLE 어학공지 ( - id INT PRIMARY KEY AUTO_INCREMENT, +const createRecruitNoticeTable = async () => { + const createTableQuery = `CREATE TABLE recruit_notices ( + id BIGINT PRIMARY KEY AUTO_INCREMENT, title VARCHAR(255) NOT NULL, link VARCHAR(255) NOT NULL UNIQUE, - uploadDate VARCHAR(255) NOT NULL + upload_date VARCHAR(30) NOT NULL, + recruitment_period VARCHAR(30) NOT NULL );`; - db.query(createTableQuery, (error) => { - if (error) { - console.log('어학 DB 생성 실패', error); - return; - } - console.log('어학 테이블 생성 성공!'); - }); + try { + await db.execute(createTableQuery); + console.log('채용 공지 테이블 생성 성공!'); + } catch (error) { + console.log('채용 공지 생성 실패', error); + } }; -const createAllTables = (college: College[]) => { +const createAllTables = () => { createDepartmentTable(); + createMajorNoticeTable(); + createNoticeTable(); + createSubscribeTable(); + createSubscribeKeywordTable(); createWhalebeDataTable(); - createLanguageDataTable(); - createGraduationTable(); - createSchoolNoticeTable(); - createNoticeTable(college); - createSubscribeTable(college); + createRecruitNoticeTable(); }; export default createAllTables; From 11ab12198596ce5f98a97e0c8097e62725cbe820 Mon Sep 17 00:00:00 2001 From: Lee sang Yeop Date: Sat, 4 Nov 2023 23:45:43 +0900 Subject: [PATCH 4/4] =?UTF-8?q?Fix:=20=EC=84=9C=EB=B2=84=20=EC=B5=9C?= =?UTF-8?q?=EC=B4=88=20=EC=8B=A4=ED=96=89=20=EC=8B=9C=20=EB=8F=99=EC=9E=91?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=ED=95=A8=EC=88=98=EC=97=90=EC=84=9C=20db?= =?UTF-8?q?=20=EC=BF=BC=EB=A6=AC=EB=AC=B8=EC=9D=84=20call=20back=20->=20as?= =?UTF-8?q?ync/await=20=EB=B0=A9=EC=8B=9D=EC=9C=BC=EB=A1=9C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/startCrawlingData.ts | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/hooks/startCrawlingData.ts b/src/hooks/startCrawlingData.ts index 01175fb8..9f889658 100644 --- a/src/hooks/startCrawlingData.ts +++ b/src/hooks/startCrawlingData.ts @@ -3,7 +3,7 @@ import { saveGraduationRequirementToDB } from '@db/data/graduation'; import { saveLanguageNoticeToDB } from '@db/data/languageHandler'; import { saveDepartmentToDB, - saveNoticeToDB, + saveMajorNoticeToDB, saveSchoolNoticeToDB, saveWhalebeToDB, } from '@db/data/noticeHandler'; @@ -11,24 +11,20 @@ import db from '@db/index'; import createNoticeTable from '@db/table/createTables'; import { RowDataPacket } from 'mysql2'; -export const initialCrawling = () => { +export const initialCrawling = async () => { try { const checkInitialQuery = "SHOW TABLES LIKE 'departments';"; - db.query(checkInitialQuery, async (err, res) => { - if (err) return; - const rows = res as RowDataPacket[]; - console.log(rows.length); - if (rows.length === 0) { - const collegeList = await collegeCrawling(); - createNoticeTable(collegeList); - await saveDepartmentToDB(collegeList); - await saveLanguageNoticeToDB(); - await saveGraduationRequirementToDB(); - await saveSchoolNoticeToDB(); - await saveWhalebeToDB(); - await saveNoticeToDB(); - } - }); + const [rows] = await db.execute(checkInitialQuery); + if (rows.length > 0) return; + + const collegeList = await collegeCrawling(); + createNoticeTable(); + await saveDepartmentToDB(collegeList); + await saveLanguageNoticeToDB(); + await saveGraduationRequirementToDB(); + await saveSchoolNoticeToDB(); + await saveWhalebeToDB(); + await saveMajorNoticeToDB(); } catch (err) { console.log(err); }