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); 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 => { diff --git a/src/db/table/createTables.ts b/src/db/table/createTables.ts index 09a25623..14dfaa2e 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 );`; @@ -89,7 +89,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 );`;