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

plan

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