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

#556 공지사항 디비 스킴 추가 및 공지사항 api 엔드포인트 추가 #557

Open
wants to merge 15 commits into
base: dev
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ app.use("/locations", require("./src/routes/locations"));
app.use("/reports", require("./src/routes/reports"));
app.use("/notifications", require("./src/routes/notifications"));
app.use("/fare", require("./src/routes/fare"));
app.use("/notice", require("./src/routes/notice"));

// [Middleware] 전역 에러 핸들러. 에러 핸들러는 router들보다 아래에 등록되어야 합니다.
app.use(require("./src/middlewares/errorHandler"));
Expand Down
13 changes: 13 additions & 0 deletions src/modules/stores/mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,18 @@ const taxiFareSchema = Schema(
}
);

const noticeSchema = Schema(
{
title: { type: String, required: true },
notion_url: { type: String, required: true },
is_pinned: { type: Boolean, default: false },
is_active: { type: Boolean, default: true },
},
{
timestamps: true, // 최근 업데이트 시간 기록용
}
);

mongoose.set("strictQuery", true);

const database = mongoose.connection;
Expand Down Expand Up @@ -259,4 +271,5 @@ module.exports = {
),
adminLogModel: mongoose.model("AdminLog", adminLogSchema),
taxiFareModel: mongoose.model("TaxiFare", taxiFareSchema),
noticeModel: mongoose.model("Notice", noticeSchema),
};
43 changes: 43 additions & 0 deletions src/routes/docs/notice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const { objectIdPattern } = require("./utils");

const tag = "notice";
const apiPrefix = "/notice";

const noticeDocs = {};

noticeDocs[`${apiPrefix}/list`] = {
get: {
tags: [tag],
summary: "공지사항 목록 반환",
description: "공지사항의 목록을 반환합니다.",
responses: {
200: {
description: "예상 택시 요금 반환 성공",
content: {
"application/json": {
schema: {
type: "object",
properties: {
_id: { type: "string", pattern: objectIdPattern },
title: { type: "string", pattern: objectIdPattern },
is_pinned: { type: "boolean" },
is_active: { type: "boolean" },
createdAt: { type: "boolean" },
},
},
},
},
},
500: {
description: "notice/list: Failed to load notices",
content: {
"text/html": {
example: "notice/list: Failed to load notices",
},
},
},
},
},
};

module.exports = noticeDocs;
6 changes: 6 additions & 0 deletions src/routes/docs/swaggerDocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const usersDocs = require("./users");
const roomsDocs = require("./rooms");
const chatsDocs = require("./chats");
const fareDocs = require("./fare");
const noticeDocs = require("./notice");
const { port, nodeEnv } = require("../../../loadenv");

const serverList = [
Expand Down Expand Up @@ -75,6 +76,10 @@ const swaggerDocs = {
name: "fare",
description: "예상 택시 금액 계산",
},
{
name: "notice",
description: "공지사항 조회",
},
],
consumes: ["application/json"],
produces: ["application/json"],
Expand All @@ -88,6 +93,7 @@ const swaggerDocs = {
...chatsDocs,
...roomsDocs,
...fareDocs,
...noticeDocs,
},
components: {
schemas: {
Expand Down
9 changes: 9 additions & 0 deletions src/routes/notice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const express = require("express");

const { getNotices } = require("../services/notice");

const router = express.Router();

router.get("/list", getNotices);

module.exports = router;
24 changes: 24 additions & 0 deletions src/services/notice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { noticeModel } = require("../modules/stores/mongo");

const getNotices = async (req, res) => {
try {
const pinnedNotice = await noticeModel.findOne({
is_active: true,
is_pinned: true,
});
const notices = await noticeModel.find({
is_active: true,
is_pinned: false,
});
if (pinnedNotice) {
return res.status(200).json({ notices: [pinnedNotice, ...notices] });
}
return res.status(200).json({ notices });
} catch (e) {
return res.status(500).send("notice/list: Failed to load notices");
}
};

module.exports = {
getNotices,
};
Loading