From fed9d57cbfa6583c7f7e3be556db564d3df35507 Mon Sep 17 00:00:00 2001 From: Lee sang Yeop Date: Sun, 20 Aug 2023 23:27:35 +0900 Subject: [PATCH 1/3] =?UTF-8?q?Feat:=20=ED=95=99=EA=B3=BC,=20=ED=95=99?= =?UTF-8?q?=EA=B5=90=20=EA=B3=B5=EC=A7=80=EC=82=AC=ED=95=AD=20=ED=85=8C?= =?UTF-8?q?=EC=9D=B4=EB=B8=94=EC=97=90=EC=84=9C=20=EA=B3=B5=EC=A7=80?= =?UTF-8?q?=EC=82=AC=ED=95=AD=20URL=20link=EC=97=90=20unique=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/table/createTables.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/db/table/createTables.ts b/src/db/table/createTables.ts index 328a7f6e..42e8bba8 100644 --- a/src/db/table/createTables.ts +++ b/src/db/table/createTables.ts @@ -45,7 +45,7 @@ const createNoticeTable = (college: College[]) => { const createTableQuery = `CREATE TABLE ${tableName} ( id INT PRIMARY KEY AUTO_INCREMENT, title VARCHAR(255) NOT NULL, - link VARCHAR(255) NOT NULL, + link VARCHAR(255) NOT NULL UNIQUE, content TEXT NOT NULL, uploadDate VARCHAR(255) NOT NULL );`; @@ -66,7 +66,7 @@ const createSchoolNoticeTable = () => { const createTableQuery = `CREATE TABLE ${tableName} ( id INT PRIMARY KEY AUTO_INCREMENT, title VARCHAR(255) NOT NULL, - link VARCHAR(255) NOT NULL, + link VARCHAR(255) NOT NULL UNIQUE, content TEXT NOT NULL, uploadDate VARCHAR(255) NOT NULL );`; From 255096e9053b6263b618a29c3f8a8697f6200987 Mon Sep 17 00:00:00 2001 From: Lee sang Yeop Date: Sun, 20 Aug 2023 23:45:55 +0900 Subject: [PATCH 2/3] =?UTF-8?q?Refactor:=20=ED=95=84=EC=9A=94=ED=95=9C=20?= =?UTF-8?q?=EA=B3=B3=EC=97=90=EC=84=9C=EB=A7=8C=20try=20~=20catch=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=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/data/handler.ts | 201 ++++++++++++++++++++--------------------- 1 file changed, 96 insertions(+), 105 deletions(-) diff --git a/src/db/data/handler.ts b/src/db/data/handler.ts index 9de74b83..95c9d993 100644 --- a/src/db/data/handler.ts +++ b/src/db/data/handler.ts @@ -23,11 +23,7 @@ export const saveDepartmentToDB = async (college: College[]): Promise => { }); }); - try { - await Promise.all(saveCollegePromises); - } catch (err) { - console.log(err); - } + await Promise.all(saveCollegePromises); }; const saveNotice = (notice: Notice, major: string): Promise => { @@ -52,65 +48,65 @@ const saveNotice = (notice: Notice, major: string): Promise => { export const saveNoticeToDB = async (): Promise => { const selectQuery = 'SELECT * FROM departments;'; - try { - const results = await new Promise((resolve, reject) => { - db.query(selectQuery, (error, results) => { - if (error) { - console.error('SELECT 오류:', error); - reject(error); - } else { - resolve(results as College[]); - } - }); + const results = await new Promise((resolve, reject) => { + db.query(selectQuery, (error, results) => { + if (error) { + console.error('SELECT 오류:', error); + reject(error); + } else { + resolve(results as College[]); + } }); + }); - const savePromises: Promise[] = []; - - for (const row of results) { - const college: College = { - collegeName: row.collegeName, - departmentName: row.departmentName, - departmentSubName: row.departmentSubName, - departmentLink: row.departmentLink, - }; - - const noticeLink = await noticeCrawling(college); - const noticeLists = await noticeListCrawling(noticeLink); - const major = - college.departmentSubName === '-' - ? college.departmentName - : college.departmentSubName; - - if (noticeLists.pinnedNotice !== undefined) { - const pinnedNotiQuery = `SELECT link FROM ${major}고정 ORDER BY STR_TO_DATE(uploadDate, '%Y-%m-%d') DESC LIMIT 1;`; - let pinnedNotiLink = ''; - db.query(pinnedNotiQuery, async (err, res) => { - if (err) { - console.log(err); - } else { - const rows = res as RowDataPacket[]; - if (Array.isArray(rows) && rows.length > 0) { - pinnedNotiLink = rows[0].link; - } - for (const notice of noticeLists.pinnedNotice) { - const result = await noticeContentCrawling(notice); - if (result.path === pinnedNotiLink) break; - savePromises.push(saveNotice(result, major + '고정')); - } - } - }); - } + const savePromises: Promise[] = []; - const normalNotiQuery = `SELECT link FROM ${major}일반 ORDER BY STR_TO_DATE(uploadDate, '%Y-%m-%d') DESC LIMIT 1;`; - let normalNotiLink = ''; - db.query(normalNotiQuery, async (err, res) => { + for (const row of results) { + const college: College = { + collegeName: row.collegeName, + departmentName: row.departmentName, + departmentSubName: row.departmentSubName, + departmentLink: row.departmentLink, + }; + + const noticeLink = await noticeCrawling(college); + const noticeLists = await noticeListCrawling(noticeLink); + const major = + college.departmentSubName === '-' + ? college.departmentName + : college.departmentSubName; + + if (noticeLists.pinnedNotice !== undefined) { + const pinnedNotiQuery = `SELECT link FROM ${major}고정 ORDER BY STR_TO_DATE(uploadDate, '%Y-%m-%d') DESC LIMIT 1;`; + let pinnedNotiLink = ''; + db.query(pinnedNotiQuery, async (err, res) => { if (err) { console.log(err); } else { const rows = res as RowDataPacket[]; if (Array.isArray(rows) && rows.length > 0) { - normalNotiLink = rows[0].link; + pinnedNotiLink = rows[0].link; + } + for (const notice of noticeLists.pinnedNotice) { + const result = await noticeContentCrawling(notice); + if (result.path === pinnedNotiLink) break; + savePromises.push(saveNotice(result, major + '고정')); } + } + }); + } + + const normalNotiQuery = `SELECT link FROM ${major}일반 ORDER BY STR_TO_DATE(uploadDate, '%Y-%m-%d') DESC LIMIT 1;`; + let normalNotiLink = ''; + db.query(normalNotiQuery, async (err, res) => { + if (err) { + console.log(err); + } else { + try { + const rows = res as RowDataPacket[]; + if (Array.isArray(rows) && rows.length > 0) + normalNotiLink = rows[0].link; + for (const notice of noticeLists.normalNotice) { const result = await noticeContentCrawling(notice); if (result.path === normalNotiLink) { @@ -118,14 +114,14 @@ export const saveNoticeToDB = async (): Promise => { } savePromises.push(saveNotice(result, major + '일반')); } + } catch (error) { + console.log('cheerio 에러', error); } - }); - } - - await Promise.all(savePromises); - } catch (error) { - console.error('에러 발생:', error); + } + }); } + + await Promise.all(savePromises); }; const saveSchoolNotice = async ( @@ -133,56 +129,51 @@ const saveSchoolNotice = async ( mode: string, ): Promise[]> => { const query = `SELECT link FROM 학교${mode} ORDER BY STR_TO_DATE(uploadDate, '%Y-%m-%d') DESC LIMIT 1;`; - try { - const res = await new Promise((resolve, reject) => { - db.query(query, (err, res) => { - if (err) { - reject(err); + const res = await new Promise((resolve, reject) => { + db.query(query, (err, res) => { + if (err) { + reject(err); + } else { + const rows = res as RowDataPacket[]; + if (Array.isArray(rows) && rows.length > 0) { + const link = rows[0].link; + resolve(link); } else { - const rows = res as RowDataPacket[]; - if (Array.isArray(rows) && rows.length > 0) { - const link = rows[0].link; - resolve(link); - } else { - resolve(''); - } + resolve(''); } - }); + } }); + }); - const saveNoticeQuery = `INSERT INTO 학교${mode} (title, link, content, uploadDate) VALUES (?, ?, ?, ?);`; - const savePromises: Promise[] = []; - - for (const list of notices) { - const notice = await noticeContentCrawling(list); - if (res === notice.path) break; - - savePromises.push( - new Promise((resolve, reject) => { - const values = [ - notice.title, - notice.path, - notice.description, - notice.date, - ]; - db.query(saveNoticeQuery, values, (error) => { - if (error) { - console.error('데이터 입력 실패', error); - reject(error); - } else { - console.log('학교 공지사항 입력 성공!'); - resolve(); - } - }); - }), - ); - } + const saveNoticeQuery = `INSERT INTO 학교${mode} (title, link, content, uploadDate) VALUES (?, ?, ?, ?);`; + const savePromises: Promise[] = []; - return savePromises; - } catch (error) { - console.error('에러 발생:', error); - return []; + for (const list of notices) { + const notice = await noticeContentCrawling(list); + if (res === notice.path) break; + + savePromises.push( + new Promise((resolve, reject) => { + const values = [ + notice.title, + notice.path, + notice.description, + notice.date, + ]; + db.query(saveNoticeQuery, values, (error) => { + if (error) { + console.error('데이터 입력 실패', error); + reject(error); + } else { + console.log('학교 공지사항 입력 성공!'); + resolve(); + } + }); + }), + ); } + + return savePromises; }; export const saveSchoolNoticeToDB = async (): Promise => { From b2e010a2f729f4ecfadff8731c20849010589f82 Mon Sep 17 00:00:00 2001 From: Lee sang Yeop Date: Sun, 20 Aug 2023 23:46:22 +0900 Subject: [PATCH 3/3] =?UTF-8?q?Fix:=20=EA=B3=B5=EC=A7=80=EC=82=AC=ED=95=AD?= =?UTF-8?q?=20=EB=B6=88=EB=9F=AC=EC=98=AC=20=EB=95=8C=20=EB=82=A0=EC=A7=9C?= =?UTF-8?q?=EC=88=9C=EC=9C=BC=EB=A1=9C=20=EC=A0=95=EB=A0=AC=ED=95=98?= =?UTF-8?q?=EC=97=AC=20=EB=B6=88=EB=9F=AC=EC=98=A4=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apis/notice/service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/apis/notice/service.ts b/src/apis/notice/service.ts index 1cbef058..e20a2668 100644 --- a/src/apis/notice/service.ts +++ b/src/apis/notice/service.ts @@ -8,7 +8,7 @@ interface SeparateNoti { const getNoticesFromTable = (tableName: string) => { return new Promise((resolve, reject) => { - const getNoticesQuery = `SELECT * FROM ${tableName}`; + const getNoticesQuery = `SELECT * FROM ${tableName} ORDER BY STR_TO_DATE(uploadDate, '%Y-%m-%d') DESC;`; db.query(getNoticesQuery, (err: Error, res: Notice[]) => { if (err) reject(err); if (res !== undefined && res.length > 0) resolve(res);