Skip to content

Commit

Permalink
Merge pull request #82 from GDSC-PKNU-21-22/refactor/#81
Browse files Browse the repository at this point in the history
Refactor/#81: 중복된 공지사항이 DB에 저장 안되도록 수정
  • Loading branch information
pp449 authored Aug 21, 2023
2 parents c1b2bf5 + b2e010a commit 1fbee84
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 108 deletions.
2 changes: 1 addition & 1 deletion src/apis/notice/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface SeparateNoti {

const getNoticesFromTable = (tableName: string) => {
return new Promise<Notice[]>((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);
Expand Down
201 changes: 96 additions & 105 deletions src/db/data/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ export const saveDepartmentToDB = async (college: College[]): Promise<void> => {
});
});

try {
await Promise.all(saveCollegePromises);
} catch (err) {
console.log(err);
}
await Promise.all(saveCollegePromises);
};

const saveNotice = (notice: Notice, major: string): Promise<void> => {
Expand All @@ -52,137 +48,132 @@ const saveNotice = (notice: Notice, major: string): Promise<void> => {

export const saveNoticeToDB = async (): Promise<void> => {
const selectQuery = 'SELECT * FROM departments;';
try {
const results = await new Promise<College[]>((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<College[]>((resolve, reject) => {
db.query(selectQuery, (error, results) => {
if (error) {
console.error('SELECT 오류:', error);
reject(error);
} else {
resolve(results as College[]);
}
});
});

const savePromises: Promise<void>[] = [];

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<void>[] = [];

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) {
break;
}
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 (
notices: string[],
mode: string,
): Promise<Promise<void>[]> => {
const query = `SELECT link FROM 학교${mode} ORDER BY STR_TO_DATE(uploadDate, '%Y-%m-%d') DESC LIMIT 1;`;
try {
const res = await new Promise<string>((resolve, reject) => {
db.query(query, (err, res) => {
if (err) {
reject(err);
const res = await new Promise<string>((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<void>[] = [];

for (const list of notices) {
const notice = await noticeContentCrawling(list);
if (res === notice.path) break;

savePromises.push(
new Promise<void>((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<void>[] = [];

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<void>((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<void> => {
Expand Down
4 changes: 2 additions & 2 deletions src/db/table/createTables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);`;
Expand Down Expand Up @@ -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
);`;
Expand Down

0 comments on commit 1fbee84

Please sign in to comment.