Skip to content

Commit

Permalink
Merge pull request #146 from GDSC-PKNU-Official/issue/#145
Browse files Browse the repository at this point in the history
Issue/#145: 데이터베이스 구조 변경에 따른 모든 로직 변경
  • Loading branch information
pp449 authored Nov 26, 2023
2 parents 69755f1 + 77bcd0e commit d5bc751
Show file tree
Hide file tree
Showing 17 changed files with 464 additions and 532 deletions.
30 changes: 23 additions & 7 deletions src/@types/college.ts
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;
}
56 changes: 28 additions & 28 deletions src/apis/majorDecision/service.ts
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);
}
};
101 changes: 47 additions & 54 deletions src/apis/notice/service.ts
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;
};
12 changes: 8 additions & 4 deletions src/apis/subscribe/controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { subscribeMajor, unsubscribeMajor } from '@apis/subscribe/service';
import {
pushNotification,
subscribeMajor,
unsubscribeMajor,
} from '@apis/subscribe/service';
import express, { Request, Response } from 'express';

const router = express.Router();
Expand All @@ -15,8 +19,8 @@ router.post('/major', async (req: Request, res: Response) => {

router.delete('/major', async (req: Request, res: Response) => {
try {
const { subscription, major } = req.body;
await unsubscribeMajor(subscription, major);
const { subscription } = req.body;
await unsubscribeMajor(subscription);
} catch (error) {
console.error(error);
} finally {
Expand All @@ -27,7 +31,7 @@ router.delete('/major', async (req: Request, res: Response) => {
// router.post('/push', async (req: Request, res: Response) => {
// try {
// const { major } = req.body.data;
// pushNotification(major);
// pushNotification(major, ['안녕', '내 이름은', '곱등이']);
// } catch (error) {
// console.error(error);
// } finally {
Expand Down
Loading

0 comments on commit d5bc751

Please sign in to comment.