Skip to content

Commit

Permalink
Update notices page
Browse files Browse the repository at this point in the history
  • Loading branch information
taedonn committed Nov 7, 2023
1 parent 2e632c9 commit 26f2c3a
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 56 deletions.
54 changes: 24 additions & 30 deletions src/pages/admin/notices/[noticeId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,39 +74,33 @@ const NoticePage = ({params}: any) => {

if (createdDate.value === "") {
setCreatedDateAlert(true);
} else if (updatedDate.value === "") {
setUpdatedDateAlert(true);
} else if (title.value === "") {
setTitleAlert(true);
} else if (content.value === "") {
setContentAlert(true);
} else {
setIsLoading(true);

// 답변 완료하고 메일 보내기
// await axios.post("/api/admin/issue", {
// action: "issue_id",
// email: issue.issue_email,
// content: issue.issue_content,
// reply: txt.value,
// })
// .then(async () => {
// await axios.post("/api/admin/issue", {
// action: "issue_closed",
// issue_id: issue.issue_id,
// issue_reply: txt.value,
// issue_closed: issueClosed,
// issue_closed_type: "Closed",
// })
// .then(res => {
// console.log(res.data.msg);
// setIsLoading(false);
// setReplySuccess("success");
// window.scrollTo({top: 0});
// })
// .catch(err => {
// console.log(err);
// setReplySuccess("fail");
// });
// })
// .catch(err => {
// console.log(err);
// setReplySuccess("fail");
// });
// 답변 완료하고 DB에 저장하기
await axios.post("/api/admin/notices", {
action: "edit",
id: notice.notice_id,
show_type: noticeShow,
created_date: new Date(createdDate.value),
updated_date: new Date(updatedDate.value),
title: title.value,
content: content.value,
})
.then(res => {
console.log(res.data.msg);
setIsEdited("success");
})
.catch(err => {
console.log(err);
setIsEdited("fail");
});
}
setIsLoading(false);
}
Expand Down
24 changes: 14 additions & 10 deletions src/pages/admin/notices/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ const NoticeList = ({params}: any) => {
// 페이지 변경 시 데이터 다시 불러오기
useEffect(() => {
const fetchNewData = async () => {
await axios.post('/api/admin/notices', {
action: "list",
page: page,
filter: filter,
text: text
await axios.get('/api/admin/notices', {
params: {
action: "list",
page: page,
filter: filter,
text: text,
}
})
.then((res) => { setList(res.data.list); })
.catch(err => console.log(err));
Expand All @@ -60,11 +62,13 @@ const NoticeList = ({params}: any) => {
setText(textRef.current.value);

// API 호출
await axios.post('/api/admin/notices', {
action: "list",
page: 1,
filter: selectRef.current.value,
text: textRef.current.value
await axios.get('/api/admin/notices', {
params: {
action: "list",
page: 1,
filter: selectRef.current.value,
text: textRef.current.value,
}
})
.then((res) => {
setList(res.data.list);
Expand Down
34 changes: 29 additions & 5 deletions src/pages/api/admin/notices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,37 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
err: err
});
}
} else if (req.body.action === "list") {
} else if (req.body.action === "edit") {
try {
const filter = req.body.filter as string;
await prisma.fontsNotice.update({
where: { notice_id: Number(req.body.id) },
data: {
notice_show_type: req.body.show_type,
notice_created_at: req.body.created_date,
notice_updated_at: req.body.updated_date,
notice_title: req.body.title,
notice_content: req.body.content,
}
});

return res.status(200).json({
msg: "공지 수정 성공"
});
} catch (err) {
return res.status(500).json({
msg: "공지 수정 실패",
err: err,
});
}
}
} else if (req.method === "GET") {
if (req.query.action === "list") {
try {
const filter = req.query.filter as string;

const text = [
{notice_title: {contains: req.body.text as string}},
{notice_content: {contains: req.body.text as string}},
{notice_title: {contains: req.query.text as string}},
{notice_content: {contains: req.query.text as string}},
]

// 공지 목록 페이지 수
Expand All @@ -83,7 +107,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
},
orderBy: [{notice_id: 'desc'}],
take: limit, // 가져오는 데이터 수
skip: Number(req.body.page) === 1 ? 0 : (Number(req.body.page) - 1) * limit
skip: Number(req.query.page) === 1 ? 0 : (Number(req.query.page) - 1) * limit
});

return res.status(200).json({
Expand Down
135 changes: 135 additions & 0 deletions src/pages/api/notices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import prisma from '@/libs/client-prisma';

const limit = 10;

// SSR 공지 목록 페이지 수
export async function FetchNoticesLength() {
const notices = await prisma.fontsNotice.findMany({
select: { notice_id: true },
});
const count = Number(notices.length) % limit > 0 ? Math.floor(Number(notices.length)/limit) + 1 : Math.floor(Number(notices.length)/limit);

return count;
}

// SSR 첫 공지 목록 불러오기
export async function FetchNotices(lastId: number | undefined) {
const list = await prisma.fontsNotice.findMany({
orderBy: [{notice_id: 'desc'}], // 정렬순
take: limit, // 가져오는 데이터 수
skip: lastId ? 1 : 0,
...(lastId && { cursor: {notice_id: lastId} })
});

return list;
}

// 특정 공지 불러오기
export async function FetchNotice(noticeId: number) {
const notice = await prisma.fontsNotice.findUnique({
where: { notice_id: Number(noticeId) }
});

return notice;
}

// 공지 전체 불러오기
export async function FetchAllNotices() {
const notices = await prisma.fontsNotice.findMany({
where: { notice_show_type: true }
});

return notices;
}

// API
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === "POST") {
if (req.body.action === "add") {
try {
await prisma.fontsNotice.create({
data: {
notice_type: req.body.notice_type,
notice_title: req.body.notice_title,
notice_content: req.body.notice_content,
}
});

return res.status(200).json({
msg: "공지 추가 성공",
});
} catch (err) {
return res.status(500).json({
msg: "공지 추가 실패",
err: err
});
}
} else if (req.body.action === "edit") {
try {
await prisma.fontsNotice.update({
where: { notice_id: Number(req.body.id) },
data: {
notice_show_type: req.body.show_type,
notice_created_at: req.body.created_date,
notice_updated_at: req.body.updated_date,
notice_title: req.body.title,
notice_content: req.body.content,
}
});

return res.status(200).json({
msg: "공지 수정 성공"
});
} catch (err) {
return res.status(500).json({
msg: "공지 수정 실패",
err: err,
});
}
}
} else if (req.method === "GET") {
if (req.query.action === "list") {
try {
const filter = req.query.filter as string;

const text = [
{notice_title: {contains: req.query.text as string}},
{notice_content: {contains: req.query.text as string}},
]

// 공지 목록 페이지 수
const length = await prisma.fontsNotice.findMany({
select: { notice_id: true },
where: {
OR: text,
notice_type: filter === "all" ? {} : filter,
}
});
const count = Number(length.length) % limit > 0 ? Math.floor(Number(length.length)/limit) + 1 : Math.floor(Number(length.length)/limit);

// 공지 목록 불러오기
const list = await prisma.fontsNotice.findMany({
where: {
OR: text,
notice_type: filter === "all" ? {} : filter,
},
orderBy: [{notice_id: 'desc'}],
take: limit, // 가져오는 데이터 수
skip: Number(req.query.page) === 1 ? 0 : (Number(req.query.page) - 1) * limit
});

return res.status(200).json({
message: "공지 목록 불러오기 성공",
list: list,
count: count
});
} catch (err) {
return res.status(500).json({
message: "공지 목록 불러오기 실패",
err: err
});
}
}
}
}
Loading

0 comments on commit 26f2c3a

Please sign in to comment.