Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update기능, 폐업한 가게 DB에서 삭제 기능 추가 #75

Merged
merged 17 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"editor.formatOnSave": true,
"editor.formatOnPaste": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
},
"typescript.tsdk": "node_modules\\typescript\\lib"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 설정은 다른 운영체제에서 경로로 인한 오류가 발생할 수 있을 것 같아요. 어떤 용도인지 알려주실수 있을까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

어제 실행 오류를 해결하려고 하다보니 생긴 부분인것 같습니다

}
11 changes: 6 additions & 5 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import morgan from "morgan";
import testRouter from "./test/test.router.ts";
import dotenv from "dotenv";
import { userContract, userRouter } from "./user/user_router";
import { dbScheduler } from "./scheduler/scheduler";
import { dbScheduler } from "./scheduler/scheduler.ts";
import db from "./db/models";
import schedule from "node-schedule";
import { createExpressEndpoints, initServer } from "@ts-rest/express";
Expand All @@ -14,6 +14,7 @@ import { reviewRouter, reviewContract } from "./review/mod.ts";
import { restaurantsContract, restaurantsRouter } from "./restaurants";
import { initContract } from "@ts-rest/core";
import { regionsContract, regionsRouter } from "./regions";
import {} from "node:fs";

dotenv.config();

Expand All @@ -29,7 +30,7 @@ app.use("/openapi", swaggerUi.serve, swaggerUi.setup(openApiDocument));

db.sequelize
.sync({
force: true, //임시
force: false, //임시
})
.then(() => {
console.log("connected");
Expand Down Expand Up @@ -73,9 +74,9 @@ createExpressEndpoints(reviewContract, reviewRouter, app, jwtMiddleware);

app.listen(3000, () => {
console.log("Server On");
// schedule.scheduleJob("0 * * * * *", function () {
// dbScheduler();
// });
schedule.scheduleJob("0 * * * * *", function () {
dbScheduler();
});
Comment on lines +77 to +79
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
schedule.scheduleJob("0 * * * * *", function () {
dbScheduler();
});
schedule.scheduleJob("0 * * * * *", dbScheduler);

함수를 인자 없이 그대로 실행한다면, 함수를 그대로 전달하면 가독성을 올릴 수 있어요!

});

/*
Expand Down
3 changes: 1 addition & 2 deletions src/scheduler/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import { allInsertDB } from "./all_insert";
export const dbScheduler = async () => {
console.log(new Date() + "Running Scheduler !!! ");

// 오늘의 요일 0 ~ 6
const nowData = await db.Restaurant.count();

if (nowData > 0) {
console.log("0보다 큼");
await updateRestData();
} else {
console.log("DB에 데이터 없음");
await allInsertDB();
}
};
30 changes: 30 additions & 0 deletions src/scheduler/sectors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"0": {
"data1": "Genrestrtcate",
"data2": "Genrestrtsash"
},
"1": {
"data1": "Genrestrtfastfood",
"data2": "Genrestrtstandpub"
},
"2": {
"data1": "Genrestrtlunch",
"data2": "Genrestrtjpnfood"
},
"3": {
"data1": "Genrestrtbsrpcook",
"data2": "Genrestrtchifood'"
},
"4": {
"data1": "Genrestrttratearm",
"data2": "Genrestrtbuff"
},
"5": {
"data1": "Genrestrtsoup",
"data2": "Genrestrtfugu"
},
"6": {
"data1": "Genrestrtmovmntcook",
"data2": "Kidscafe"
}
}
93 changes: 92 additions & 1 deletion src/scheduler/update_restaurant.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,97 @@
import axios from "axios";
import db from "../db/models/index";
import { readFileSync } from "node:fs";
import { join, dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { restaurantsRouter } from "../restaurants";

export const updateRestData = async () => {
console.log("업데이트 ");
const today = new Date(2023, 11, 10).getDay();
// 하루에 2개의 업종별로 업데이트
const path = join(dirname(fileURLToPath(import.meta.url)), "sectors.json");
const raw = readFileSync(path, { encoding: "utf8" });
const secotrs = JSON.parse(raw);

await updateDB(secotrs[today].data1);
};

const updateDB = async (_sectors: string) => {
const key = process.env.api_key;

try {
//업종명

const getHeadUrl =
"https://openapi.gg.go.kr/" +
_sectors +
"?key=" +
key +
"&type=json&pSize=5&pindex=1";

const getHead = await axios.get(getHeadUrl);
const headData = getHead.data;

// 데이터 총 개수
// const total = headData.GENRESTRT[0].head[0].list_total_count;
const total = 5;
// 한번에 가져올 데이터 수
const chunkSize = 2;
var cnt = 0;
for (let idx = 1; idx < total; idx += chunkSize) {
const url =
"https://openapi.gg.go.kr/" +
_sectors +
"?key=" +
key +
"&type=json&pSize=" +
chunkSize +
"&pindex=" +
Math.floor(idx / chunkSize + 1);

// 전체 데이터 가져오기
const response = await axios.get(url);
const responseData = response.data;
const restaurants = responseData[_sectors][1].row;

for (let i in restaurants) {
// 폐업한 가게 중에서 DB에 데이터가 있다면 삭제
if (restaurants[i].BSN_STATE_NM == "폐업") {
const closed = await db.Restaurant.findOne({
where: {
restaurant_name: restaurants[i].BIZPLC_NM,
adress: restaurants[i].REFINE_LOTNO_ADDR,
},
});
// 삭제 로직
if (closed !== null) {
db.Restaurant.destroy({
where: {
restaurant_name: restaurants[i].BIZPLC_NM,
adress: restaurants[i].REFINE_LOTNO_ADDR,
},
});
}
} else {
// 영업 중인 가게 정보 업데이트
db.Restaurant.update(
{
restaurant_name: restaurants[i].BIZPLC_NM,
restaurant_type: restaurants[i].SANITTN_BIZCOND_NM,
adress: restaurants[i].REFINE_LOTNO_ADDR, // 도로명 주소가 없는 식당이 존재 함
lat: Number(restaurants[i].REFINE_WGS84_LAT),
lon: Number(restaurants[i].REFINE_WGS84_LOGT),
},
{
where: {
restaurant_name: restaurants[i].BIZPLC_NM,
adress: restaurants[i].REFINE_LOTNO_ADDR,
},
},
);
}
Comment on lines +56 to +91
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for...of 문법을 사용해 가독성을 올리고 실수를 줄일 수 있을 것 같아요!

}
}
} catch (error) {
console.log("api 호출 중 오류 발생 : ", error);
}
};