-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Issue/#145: 데이터베이스 구조 변경에 따른 모든 로직 변경
- Loading branch information
Showing
17 changed files
with
464 additions
and
532 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,29 @@ | ||
export interface College { | ||
collegeName: string; | ||
departmentName: string; | ||
departmentSubName: string; | ||
departmentLink: string; | ||
id?: number; | ||
college_name: string; | ||
department_name: string; | ||
department_subname: string; | ||
department_link: string; | ||
graduation_link?: string; | ||
} | ||
|
||
export interface Notice { | ||
export interface Notices { | ||
id?: number; | ||
title: string; | ||
link: string; | ||
author?: string; | ||
rep_yn?: boolean | NoticeBoolean; | ||
description?: string; | ||
upload_date: string; | ||
category?: NoticeCategory; | ||
} | ||
|
||
export type NoticeCategory = 'SCHOOL' | 'LANGUAGE'; | ||
export type NoticeBoolean = 1 | 0; | ||
|
||
export interface WhalebeData { | ||
title: string; | ||
path: string; | ||
description: string; | ||
date: string; | ||
imgUrl: string; | ||
link: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,44 @@ | ||
import db from '@db/index'; | ||
import { selectQuery } from '@db/query/dbQueryHandler'; | ||
import notificationToSlack from 'src/hooks/notificateToSlack'; | ||
|
||
interface CollegesName { | ||
collegeName: string; | ||
college_name: string; | ||
} | ||
|
||
interface DepartmentsName { | ||
departmentName: string; | ||
departmentSubName: string; | ||
department_name: string; | ||
department_subname: string; | ||
} | ||
|
||
export const getCollegesName = async (): Promise<string[]> => { | ||
return new Promise((resolve, reject) => { | ||
const getCollegesQuery = `SELECT DISTINCT collegeName from departments ORDER BY collegeName;`; | ||
db.query(getCollegesQuery, (err: Error, res: CollegesName[]) => { | ||
if (err) reject(err); | ||
const colleges: string[] = []; | ||
for (const college of res) { | ||
colleges.push(college.collegeName); | ||
} | ||
resolve(colleges); | ||
}); | ||
}); | ||
const getCollegesQuery = `SELECT DISTINCT college_name from departments ORDER BY college_name;`; | ||
try { | ||
const colleges = await selectQuery<CollegesName[]>(getCollegesQuery); | ||
return colleges.map((college) => college.college_name); | ||
} catch (error) { | ||
notificationToSlack(error); | ||
} | ||
}; | ||
|
||
export const getDepartmentsName = async ( | ||
collegeName: string, | ||
): Promise<string[]> => { | ||
return new Promise((resolve, reject) => { | ||
const getDepartmentsQuery = `SELECT departmentName, departmentSubName FROM departments WHERE collegeName = '${collegeName}' ORDER BY departmentName;`; | ||
db.query(getDepartmentsQuery, (err: Error, res: DepartmentsName[]) => { | ||
if (err) reject(err); | ||
const departments: string[] = []; | ||
for (const department of res) { | ||
const major = | ||
department.departmentSubName === '-' | ||
? department.departmentName | ||
: department.departmentName + ' ' + department.departmentSubName; | ||
departments.push(major); | ||
} | ||
resolve(departments); | ||
const getDepartmentsQuery = `SELECT department_name, department_subname FROM departments WHERE college_name = '${collegeName}' ORDER BY department_name;`; | ||
|
||
try { | ||
const departments = await selectQuery<DepartmentsName[]>( | ||
getDepartmentsQuery, | ||
); | ||
|
||
return departments.map((department) => { | ||
const major = | ||
department.department_subname === '-' | ||
? department.department_name | ||
: department.department_name + ' ' + department.department_subname; | ||
return major; | ||
}); | ||
}); | ||
} catch (error) { | ||
notificationToSlack(error); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,73 @@ | ||
import { WhalebeData } from '@crawling/whalebeCrawling'; | ||
import db from '@db/index'; | ||
import { Notice } from 'src/@types/college'; | ||
import { selectQuery } from '@db/query/dbQueryHandler'; | ||
import { NoticeCategory, Notices, WhalebeData } from 'src/@types/college'; | ||
import notificationToSlack from 'src/hooks/notificateToSlack'; | ||
import { getDepartmentIdByMajor } from 'src/utils/majorUtils'; | ||
|
||
interface SeparateNoti { | ||
고정: Notice[]; | ||
일반: Notice[]; | ||
고정: Notices[]; | ||
일반: Notices[]; | ||
} | ||
|
||
const getNoticesFromTable = (tableName: string) => { | ||
return new Promise<Notice[]>((resolve, reject) => { | ||
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); | ||
resolve([]); | ||
}); | ||
}); | ||
const getNoticesFromTable = async ( | ||
tableName: string, | ||
category: NoticeCategory, | ||
) => { | ||
const getNoticesQuery = `SELECT * FROM ${tableName} WHERE category = '${category}' ORDER BY STR_TO_DATE(upload_date, '%Y-%m-%d') DESC;`; | ||
try { | ||
const notices = await selectQuery<Notices[]>(getNoticesQuery); | ||
return notices; | ||
} catch (error) { | ||
notificationToSlack(error.message + 'notices 테이블 조회 실패'); | ||
return []; | ||
} | ||
}; | ||
|
||
export const getNotices = async (department: string): Promise<SeparateNoti> => { | ||
const [fixNotices, normalNotices] = await Promise.all([ | ||
getNoticesFromTable(`${department}고정`), | ||
getNoticesFromTable(`${department}일반`), | ||
]); | ||
const majorId = await getDepartmentIdByMajor(department); | ||
const query = `SELECT * FROM major_notices WHERE department_id = ${majorId};`; | ||
const major_notices = await selectQuery<Notices[]>(query); | ||
console.log(major_notices); | ||
|
||
const notices: SeparateNoti = { | ||
고정: [...fixNotices], | ||
일반: [...normalNotices], | ||
고정: [...major_notices.filter((notice) => notice.rep_yn === 1)], | ||
일반: [...major_notices], | ||
}; | ||
|
||
return notices; | ||
}; | ||
|
||
export const getSchoolNotices = async (): Promise<SeparateNoti> => { | ||
const [fixNotices, normalNotices] = await Promise.all([ | ||
getNoticesFromTable('학교고정'), | ||
getNoticesFromTable('학교일반'), | ||
]); | ||
const noticeLists = await getNoticesFromTable('notices', 'SCHOOL'); | ||
|
||
const notices: SeparateNoti = { | ||
고정: [...fixNotices], | ||
일반: [...normalNotices], | ||
고정: [...noticeLists.filter((notice) => notice.rep_yn === 1)], | ||
일반: [...noticeLists], | ||
}; | ||
|
||
return notices; | ||
}; | ||
|
||
export const getWhalebe = async (): Promise<WhalebeData[]> => { | ||
const query = 'SELECT * FROM 웨일비;'; | ||
return new Promise<WhalebeData[]>((resolve) => { | ||
db.query(query, (err, res) => { | ||
if (err) notificationToSlack('웨일비 조회 실패'); | ||
const whalebeData = res as WhalebeData[]; | ||
const today = new Date(); | ||
const todayString = `${today.getFullYear()}.${String( | ||
today.getMonth() + 1, | ||
).padStart(2, '0')}.${String(today.getDate()).padStart(2, '0')}`; | ||
const query = 'SELECT * FROM whalebe;'; | ||
|
||
try { | ||
const whalebeData = await selectQuery<WhalebeData[]>(query); | ||
const today = new Date(); | ||
const todayString = `${today.getFullYear()}.${String( | ||
today.getMonth() + 1, | ||
).padStart(2, '0')}.${String(today.getDate()).padStart(2, '0')}`; | ||
|
||
const filteredData = whalebeData | ||
.filter((data) => data.date >= todayString) | ||
.slice(0, 7); | ||
resolve(filteredData); | ||
}); | ||
}); | ||
const filteredData = whalebeData | ||
.filter((data) => data.date >= todayString) | ||
.slice(0, 7); | ||
return filteredData; | ||
} catch (error) { | ||
notificationToSlack('웨일비 조회 실패'); | ||
return; | ||
} | ||
}; | ||
|
||
export const getLanguage = async (): Promise<Notice[]> => { | ||
const query = `SELECT * FROM 어학공지 ORDER BY STR_TO_DATE(uploadDate, '%Y-%m-%d') DESC;`; | ||
return new Promise<Notice[]>((resolve) => { | ||
db.query(query, (err, res) => { | ||
if (err) { | ||
notificationToSlack('어학 공지 응답 실패'); | ||
resolve([]); | ||
return; | ||
} | ||
const languageNoti = res as Notice[]; | ||
resolve(languageNoti); | ||
}); | ||
}); | ||
export const getLanguage = async (): Promise<Notices[]> => { | ||
const languageNotices = await getNoticesFromTable('notices', 'LANGUAGE'); | ||
return languageNotices; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.