Commit bb632885 authored by Kim, Subin's avatar Kim, Subin
Browse files

plan

parent 9271b22e
import axios from "axios"; import axios from "axios";
import baseUrl from "../utils/baseUrl"; import baseUrl from "../utils/baseUrl";
const getDetail = async (id) => { const getDetail = async (planId) => {
const url = `${baseUrl}/api/plan/getDetail/${id}` const url = `${baseUrl}/api/plan/${planId}`
const { data } = await axios.get(url) const { data } = await axios.get(url)
return data return data
} }
const addPlan = async (info, id) => { // const addPlan = async (info, id) => {
console.log('addPlan 확인', id) // console.log('addPlan 확인', id)
const url = `${baseUrl}/api/plan/addplan/${id}` // const url = `${baseUrl}/api/plan/addplan/${id}`
const { data } = await axios.post(url, { info, id }); // const { data } = await axios.post(url, { info, id });
// return data
// }
// const editPlan = async (info, id) => {
// console.log('editPlan확인', id)
// const url = `${baseUrl}/api/plan/edit/${id}`
// const { data } = await axios.put(url, { info, id });
// return data
// }
const submit = async (info) => {
const url = `${baseUrl}/api/plan`
const { data } = await axios.post(url, info)
return data return data
} }
const editPlan = async (info, id) => { const edit = async (info, planId) => {
console.log('editPlan확인', id) const url = `${baseUrl}/api/plan/${planId}`
const url = `${baseUrl}/api/plan/edit/${id}` const { data } = await axios.put(url, info)
const { data } = await axios.put(url, { info, id });
return data return data
} }
const remove = async (planId, userId) => { const remove = async (planId) => {
const url = `${baseUrl}/api/plan/${planId}?userId=${userId}` const url = `${baseUrl}/api/plan/${planId}`
const { data } = await axios.delete(url) const { data } = await axios.delete(url)
return data return data
} }
const planApi = { const planApi = {
getDetail, getDetail,
addPlan, // addPlan,
editPlan, // editPlan,
submit,
edit,
remove remove
}; };
......
...@@ -14,7 +14,7 @@ const StudyPlanEditForm = () => { ...@@ -14,7 +14,7 @@ const StudyPlanEditForm = () => {
const [selected, setSelected] = useState(""); const [selected, setSelected] = useState("");
const [error, setError] = useState(""); const [error, setError] = useState("");
const [success, setSuccess] = useState(false) const [success, setSuccess] = useState(false)
const [subName, setSubName] = useState(""); const [subjectName, setSubjectName] = useState("");
const [studyplan, setStudyplan] = useState({ const [studyplan, setStudyplan] = useState({
studyplanTitle: "", studyplanTitle: "",
endDate: "", endDate: "",
...@@ -26,7 +26,7 @@ const StudyPlanEditForm = () => { ...@@ -26,7 +26,7 @@ const StudyPlanEditForm = () => {
useEffect(() => { useEffect(() => {
let isMounted = true; let isMounted = true;
const checkInfo = { studyplanTitle: studyplan.studyplanTitle, endDate: studyplan.endDate, selected: selected } const checkInfo = { studyplanTitle: studyplan.studyplanTitle, endDate: studyplan.endDate, selected: studyplan.selected }
if (studyplan.deadline === "on") { if (studyplan.deadline === "on") {
checkInfo.endTime = studyplan.endTime checkInfo.endTime = studyplan.endTime
} else { } else {
...@@ -43,47 +43,52 @@ const StudyPlanEditForm = () => { ...@@ -43,47 +43,52 @@ const StudyPlanEditForm = () => {
useEffect(() => { useEffect(() => {
getSubject(user.id) getSubject(user.id)
if (params.hasOwnProperty('planId')) { console.log("useEffect params 확인", params)
console.log('planId params확인'); if (params.subjectId) setStudyplan({...studyplan, selected: params.subjectId })
getInfo(params.planId); else if (params.planId) getInfo(params.planId);
} // if (params.hasOwnProperty('planId')) {
// console.log('planId params확인');
// getInfo(params.planId);
// }
}, []) }, [])
async function getInfo(id) { async function getSubject(id) {
try { try {
setError("") setError("")
const result = await planApi.getDetail(id) const result = await subjectApi.subjectTitle(id)
console.log('수정 getInfo result', result) setList(result)
if (result.endTime) {
setStudyplan({
studyplanTitle: result.title,
endDate: result.endDate,
endTime: result.endTime,
memo: result.memo,
deadline: result.deadline,
selected: result.subjectId
})
setSubName(result.subjectName)
} else {
setStudyplan({
studyplanTitle: result.title,
endDate: result.endDate,
memo: result.memo,
deadline: result.deadline,
selected: result.subjectId
})
setSubName(result.subjectName)
}
} catch (error) { } catch (error) {
catchErrors(error, setError) catchErrors(error, setError)
} }
} }
async function getSubject(id) { async function getInfo(planId) {
try { try {
setError("") setError("")
const result = await subjectApi.subjectTitle(id) const result = await planApi.getDetail(planId)
setList(result) console.log('수정 getInfo result', result)
setStudyplan({ ...studyplan, ...result })
// setSubjectName(result.subjectName)
// if (result.endTime) {
// setStudyplan({
// studyplanTitle: result.title,
// endDate: result.endDate,
// endTime: result.endTime,
// memo: result.memo,
// deadline: result.deadline,
// selected: result.subjectId
// })
// setSubjectName(result.subjectName)
// } else {
// setStudyplan({
// studyplanTitle: result.title,
// endDate: result.endDate,
// memo: result.memo,
// deadline: result.deadline,
// selected: result.subjectId
// })
// setSubjectName(result.subjectName)
// }
} catch (error) { } catch (error) {
catchErrors(error, setError) catchErrors(error, setError)
} }
...@@ -102,17 +107,13 @@ const StudyPlanEditForm = () => { ...@@ -102,17 +107,13 @@ const StudyPlanEditForm = () => {
e.preventDefault(); e.preventDefault();
try { try {
setError("") setError("")
studyplan.selected = selected if (params.subjectId) {
if (params.hasOwnProperty('subjectId')) {
//등록함수 실행 //등록함수 실행
console.log('등록함수') await planApi.submit(studyplan, params.subjectId)
const result = await planApi.addPlan(studyplan, params.subjectId)
alert("해당 학업계획이 성공적으로 등록되었습니다.") alert("해당 학업계획이 성공적으로 등록되었습니다.")
} else { } else {
//수정함수 실행 //수정함수 실행
console.log('수정함수') await planApi.edit(studyplan, params.planId)
const result = await planApi.editPlan(studyplan, params.planId)
console.log('수정 후 result확인', result)
alert("해당 학업계획이 성공적으로 수정되었습니다.") alert("해당 학업계획이 성공적으로 수정되었습니다.")
} }
setSuccess(true) setSuccess(true)
...@@ -122,24 +123,28 @@ const StudyPlanEditForm = () => { ...@@ -122,24 +123,28 @@ const StudyPlanEditForm = () => {
studyplanTitle: "", studyplanTitle: "",
endDate: "", endDate: "",
deadline: "", deadline: "",
memo: "" memo: "",
selected: ""
}) })
} }
} }
function handleSelect(e) {
setSelected(e.target.value);
}
if (success) { if (success) {
return <Redirect to="/studyplan" /> return <Redirect to="/studyplan" />
} }
return ( return (
<div className="pt-5"> <div className="pt-5">
<select className={`form-select mb-4 ${styles.selectInput}`} aria-label="Choose subject" onChange={handleSelect}> {console.log("studyplan---",studyplan)}
{studyplan.selected ? <option value={studyplan.selected} selected>{subName}</option> : <option selected>관련 과목을 선택해주세요.</option>} <select className={`form-select mb-4 ${styles.selectInput}`} name="selected" aria-label="Choose subject" onChange={handleChange}>
{subjectList.length !== 0 ? subjectList.map((i) => <option value={i.id}>{i.name}</option>) : null} {subjectList.length !== 0 ? subjectList.map((subject, idx) => {
if (idx === 0) return <>
<option selected={studyplan.selected === "" ? true : false}>관련 과목을 선택해주세요.</option>
<option value={subject.id} selected={studyplan.selected === subject.id ? true : false}>{subject.name}</option>
</>
else return <option value={subject.id} selected={studyplan.selected === subject.id ? true : false}>{subject.name}</option>
})
: <option selected>새로운 과목을 만들어주세요.</option>}
</select> </select>
<input type="text" name="studyplanTitle" <input type="text" name="studyplanTitle"
className={`form-control shadow-none rounded-0 mb-5 ${styles.textInput}`} className={`form-control shadow-none rounded-0 mb-5 ${styles.textInput}`}
......
...@@ -7,7 +7,7 @@ const PlanLineList = ({ subjectId, planList = [] }) => { ...@@ -7,7 +7,7 @@ const PlanLineList = ({ subjectId, planList = [] }) => {
{planList.length !== 0 ? planList.map(plan => <div className="d-flex justify-content-between"> {planList.length !== 0 ? planList.map(plan => <div className="d-flex justify-content-between">
<p className="card-text mb-1">- {plan.title}</p> <p className="card-text mb-1">- {plan.title}</p>
<input className={`form-check-input shadow-none ${styles.checkBox}`} type="checkbox" /> <input className={`form-check-input shadow-none ${styles.checkBox}`} type="checkbox" />
</div>) : <Link className="text-decoration-none link-dark" to={`/studyplan/edit/add/${subjectId}`}> </div>) : <Link className="text-decoration-none link-dark" to={`/studyplan/submit/${subjectId}`}>
<div className="d-flex"> <div className="d-flex">
<i className="bi bi-plus"></i> <i className="bi bi-plus"></i>
<p className="card-text mb-1">새로운 계획 추가하기</p> <p className="card-text mb-1">새로운 계획 추가하기</p>
......
import { Plan, Subject } from "../db/index.js"; import { Plan, Subject } from "../db/index.js";
import * as ConvertDate from "./schedule.controller.js"; import * as ConvertDate from "./schedule.controller.js";
const addPlan = async (req, res) => { // const addPlan = async (req, res) => {
console.log('server/addPlan req.body', req.body) // console.log('server/addPlan req.body', req.body)
// try {
// let end = null;
// let tf = false;
// const { info } = req.body
// const { studyplanTitle, endDate, endTime, deadline, memo, selected } = info
// console.log('제목확인', studyplanTitle)
// if (deadline === "on") {
// end = new Date(endDate + " " + endTime)
// tf = true
// } else {
// end = new Date(endDate)
// }
// const result = await Plan.create({
// subjectId: selected,
// title: studyplanTitle,
// deadline: end,
// memo: memo,
// timeChecked: tf,
// checked: false
// })
// return res.json(result)
// } catch (error) {
// console.log(error)
// return res.status(500).send(error.message || "계획저장 에러발생")
// }
// }
// const editPlan = async (req, res) => {
// const { info, id } = req.body
// console.log('editPlan info', info, '|', id)
// try {
// let result = null
// if (info.deadline === "on") {
// result = await Plan.update({
// subjectId: info.selected,
// title: info.studyplanTitle,
// deadline: new Date(info.endDate + " " + info.endTime),
// memo: info.memo,
// timeChecked: true,
// checked: false
// }, { where: { id: id } })
// res.send(200)
// } else {
// result = await Plan.update({
// subjectId: info.selected,
// title: info.studyplanTitle,
// deadline: new Date(info.endDate),
// memo: info.memo,
// timeChecked: false,
// checked: false
// }, {
// where: { id: id }
// })
// }
// if (!result) {
// throw new Error("과목정보 수정 에러발생")
// } else {
// return res.send(200)
// }
// } catch (error) {
// console.log(error)
// return res.status(500).send(error.message || "계획수정 에러발생")
// }
// }
// const getInfo = async (req, res) => {
// console.log('server/getInfo req.params', req.params)
// try {
// let deadlineStr = null
// let endTimeStr = null
// const { planId } = req.params;
// const findInfo = await Plan.findOne({ where: { id: planId } })
// const Info = findInfo.dataValues;
// const getSubTitle = await Subject.findAll({
// attributes: ['name'],
// where: { id: Info.subjectId }
// })
// deadlineStr = ConvertDate.dateToString(Info.deadline, "full")
// endTimeStr = ConvertDate.dateToString(Info.deadline, "time")
// if (Info.timeChecked) {
// res.json({
// subjectId: Info.subjectId,
// subjectName: getSubTitle[0].dataValues.name,
// title: Info.title,
// endDate: deadlineStr,
// endTime: endTimeStr,
// deadline: "on",
// memo: Info.memo
// })
// } else {
// res.json({
// subjectId: Info.subjectId,
// subjectName: getSubTitle[0].dataValues.name,
// title: Info.title,
// endDate: deadlineStr,
// deadline: "",
// memo: Info.memo
// })
// }
// } catch (error) {
// console.log(error)
// return res.status(500).send(error.message || "계획 가져오기 에러발생")
// }
// }
const getOne = async (req, res) => {
try { try {
let end = null; let findPlan = null
let tf = false; const { planId } = req.query
const { info } = req.body console.log("get One", planId)
const { studyplanTitle, endDate, endTime, deadline, memo, selected } = info if (planId) findPlan = await Plan.findOne({ where: { id: planId } })
console.log('제목확인', studyplanTitle) if (!findPlan) throw new Error("학업 계획 정보를 찾지 못했습니다.")
if (deadline === "on") { return res.json(find)
end = new Date(endDate + " " + endTime)
tf = true
} else {
end = new Date(endDate)
}
const result = await Plan.create({
subjectId: selected,
title: studyplanTitle,
deadline: end,
memo: memo,
timeChecked: tf,
checked: false
})
return res.json(result)
} catch (error) { } catch (error) {
console.log(error) return res.status(500).send(error.message || "학업계획 조회 중 에러 발생")
return res.status(500).send(error.message || "계획저장 에러발생")
} }
} }
const editPlan = async (req, res) => { const create = async (req, res) => {
const { info, id } = req.body
console.log('editPlan info', info, '|', id)
try { try {
let result = null let date = null
if (info.deadline === "on") { let check_v = false
result = await Plan.update({ const { studyplanTitle, endDate, endTime, memo, deadline, selected } = req.body
subjectId: info.selected, if (deadline === "on") {
title: info.studyplanTitle, date = new Date(endDate + " " + endTime)
deadline: new Date(info.endDate + " " + info.endTime), check_v = true
memo: info.memo, } else date = new Date(endDate)
timeChecked: true, const newPlan = await Plan.create({ title: studyplanTitle, deadline: date, memo: memo, timeChecked: check_v, subjectId: selected })
checked: false return res.json(newPlan)
}, { where: { id: id } })
res.send(200)
} else {
result = await Plan.update({
subjectId: info.selected,
title: info.studyplanTitle,
deadline: new Date(info.endDate),
memo: info.memo,
timeChecked: false,
checked: false
}, {
where: { id: id }
})
}
if (!result) {
throw new Error("과목정보 수정 에러발생")
} else {
return res.send(200)
}
} catch (error) { } catch (error) {
console.log(error) return res.status(500).send(error.message || "학업계획 생성 중 에러 발생")
return res.status(500).send(error.message || "계획수정 에러발생")
} }
} }
const getInfo = async (req, res) => { const edit = async (req, res) => {
console.log('server/getInfo req.params', req.params)
try { try {
let deadlineStr = null const planId = req.planId
let endTimeStr = null let date = null
let check_v = false
const { planId } = req.params; const { studyplanTitle, endDate, endTime, memo, deadline, selected } = req.body
const findInfo = await Plan.findOne({ where: { id: planId } }) if (deadline === "on") {
const Info = findInfo.dataValues; date = new Date(endDate + " " + endTime)
check_v = true
const getSubTitle = await Subject.findAll({ } else date = new Date(endDate)
attributes: ['name'], const updated = await Plan.updated({ title: studyplanTitle, deadline: date, memo: memo, timeChecked: check_v, subjectId: selected }, { where: { id: planId } })
where: { id: Info.subjectId } if (!updated) throw new Error("해당 학업계획의 일부 정보를 수정하는데 실패하였습니다.")
}) else return res.send(200)
} catch (error) {
return res.status(500).send(error.message || "학업계획 수정 중 에러 발생")
}
}
deadlineStr = ConvertDate.dateToString(Info.deadline, "full") const remove = async (req, res) => {
endTimeStr = ConvertDate.dateToString(Info.deadline, "time") try {
const planId = req.planId
const deleted = await Plan.destroy({ where: { id: planId } })
if (!deleted) throw new Error("해당 과목을 삭제하는데 실패하였습니다.")
else return res.send(200)
} catch (error) {
return res.status(500).send(error.message || "학업계획 삭제 중 에러 발생")
}
}
if (Info.timeChecked) { const getParams = async (req, res, next) => {
res.json({ try {
subjectId: Info.subjectId, const { planId } = req.params
subjectName: getSubTitle[0].dataValues.name, req.planId = planId
title: Info.title, next()
endDate: deadlineStr,
endTime: endTimeStr,
deadline: "on",
memo: Info.memo
})
} else {
res.json({
subjectId: Info.subjectId,
subjectName: getSubTitle[0].dataValues.name,
title: Info.title,
endDate: deadlineStr,
deadline: "",
memo: Info.memo
})
}
} catch (error) { } catch (error) {
console.log(error) return res.status(500).send(error.message || "일정 가져오는 중 에러 발생")
return res.status(500).send(error.message || "계획 가져오기 에러발생")
} }
} }
export default { export default {
addPlan, getOne,
editPlan, create,
getInfo edit,
remove,
getParams
// addPlan,
// editPlan,
// getInfo
} }
\ No newline at end of file
...@@ -12,19 +12,24 @@ const PlanModel = (sequelize) => { ...@@ -12,19 +12,24 @@ const PlanModel = (sequelize) => {
primaryKey: true primaryKey: true
}, },
title: { title: {
type: DataTypes.STRING type: DataTypes.STRING,
allowNull: false
}, },
deadline: { deadline: {
type: DataTypes.DATE type: DataTypes.DATE,
allowNull: false
}, },
memo: { memo: {
type: DataTypes.STRING type: DataTypes.STRING,
defaultValue: ""
}, },
timeChecked: { timeChecked: {
type: DataTypes.BOOLEAN type: DataTypes.BOOLEAN,
defaultValue: false
}, },
checked: { checked: {
type: DataTypes.BOOLEAN type: DataTypes.BOOLEAN,
defaultValue: false
} }
}, },
{ {
......
...@@ -12,13 +12,16 @@ const SubjectModel = (sequelize) => { ...@@ -12,13 +12,16 @@ const SubjectModel = (sequelize) => {
primaryKey: true primaryKey: true
}, },
name: { name: {
type: DataTypes.STRING type: DataTypes.STRING,
allowNull: false
}, },
prof: { prof: {
type: DataTypes.STRING type: DataTypes.STRING,
defaultValue: ""
}, },
room: { room: {
type: DataTypes.STRING type: DataTypes.STRING,
defaultValue: ""
} }
}, },
{ {
......
...@@ -4,14 +4,15 @@ import planCtrl from '../controllers/plan.controller.js' ...@@ -4,14 +4,15 @@ import planCtrl from '../controllers/plan.controller.js'
const router = express.Router(); const router = express.Router();
router router
.route("/addplan/:subjectId") .route("/")
.post(planCtrl.addPlan) .post(planCtrl.create)
router router
.route("/edit/:subjectId") .route("/:planId")
.put(planCtrl.editPlan) .get(planCtrl.getOne)
.put(planCtrl.edit)
.delete(planCtrl.remove)
router.param("planId", planCtrl.getParams)
router
.route("/getDetail/:planId")
.get(planCtrl.getInfo)
export default router; export default router;
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment