-
-
sadsad
-
-
-
+
+
+
+
{schedule.title}
+
+
+ handleClick(schedule.id)}>
-
2020~353543135
-
sadsds adsdsadsdsad sdsadsdsadsdsadsdsadsdsadsdsadsdsadsd
+
+ {(schedule.start === schedule.end) ? schedule.start : schedule.start + "~" + schedule.end}
+
+
{schedule.memo}
)
diff --git a/client/src/components/Buttons/BtnGroup.js b/client/src/components/Buttons/BtnGroup.js
index 8c2cbe91a52696f933c298e115ba6831289c551c..29fd12d5f0e348fe2a464b54579bb95931721b2f 100644
--- a/client/src/components/Buttons/BtnGroup.js
+++ b/client/src/components/Buttons/BtnGroup.js
@@ -1,13 +1,13 @@
import { useHistory } from "react-router-dom";
import styles from "./buttons.module.scss";
-const BtnGroup = ({ disabled, handleSubmit }) => {
+const BtnGroup = ({ text, disabled, handleSubmit }) => {
const history = useHistory();
return (
-
+
)
}
diff --git a/client/src/components/Form/ScheduleForm.js b/client/src/components/Form/ScheduleForm.js
index bbed623527ef14c061ce47a0f43fd5a6b69c3993..cec09dd2eebd54eed1e90ad31387d1325865d8c9 100644
--- a/client/src/components/Form/ScheduleForm.js
+++ b/client/src/components/Form/ScheduleForm.js
@@ -1,12 +1,11 @@
import { useState, useEffect } from "react";
-import { Redirect, useLocation } from "react-router-dom";
+import { Redirect, useParams } from "react-router-dom";
import BtnGroup from "../Buttons/BtnGroup.js";
-import scheduleApi from "../../apis/schedule.api.js";
+import scheduleApi from "../../apis/schedule.api";
import catchErrors from "../../utils/catchErrors.js";
import styles from "./form.module.scss";
const ScheduleForm = () => {
- const location = useLocation()
const [schedule, setSchedule] = useState({
title: "",
startDate: "",
@@ -20,6 +19,11 @@ const ScheduleForm = () => {
const [disabled, setDisabled] = useState(true)
const [success, setSuccess] = useState(false)
const [error, setError] = useState("")
+ const { scheduleId } = useParams()
+
+ useEffect(() => {
+ if (scheduleId) getOne(scheduleId)
+ }, [])
useEffect(() => {
let isMounted = true;
@@ -40,6 +44,16 @@ const ScheduleForm = () => {
};
}, [schedule])
+ async function getOne(id) {
+ try {
+ setError("")
+ const resSchedule = await scheduleApi.getOne(id)
+ setSchedule({ ...schedule, ...resSchedule })
+ } catch (error) {
+ catchErrors(error, setError)
+ }
+ }
+
function handleChange(e) {
const { name, value } = e.target
if (name === "allDay") {
@@ -55,8 +69,14 @@ const ScheduleForm = () => {
e.preventDefault()
try {
setError("")
- if (schedule.allDay === "on") setSchedule({ ...schedule, startTime: '00:00', endTime: '23:59' })
- await scheduleApi.submit()
+ if (scheduleId) {
+ await scheduleApi.edit(scheduleId, schedule)
+ alert('해당 일정이 성공적으로 수정되었습니다.')
+ }
+ else {
+ await scheduleApi.submit(schedule)
+ alert('해당 일정이 성공적으로 등록되었습니다.')
+ }
setSuccess(true)
} catch (error) {
catchErrors(error, setError)
@@ -64,20 +84,18 @@ const ScheduleForm = () => {
}
if (success) {
- alert('해당 일정이 성공적으로 등록되었습니다.')
return
}
return (
-
diff --git a/server/controllers/ku.controller.js b/server/controllers/ku.controller.js
index 7ac6b817ee82efde76a63f246e30fdadd8a7e5bd..20d41cca30a9a9b99b257aa225412783abeb856d 100644
--- a/server/controllers/ku.controller.js
+++ b/server/controllers/ku.controller.js
@@ -1,8 +1,66 @@
import { KU } from "../db/index.js";
+import sequelize from 'sequelize';
-const create = async (req, res) => {
+const { Op } = sequelize
+
+const findbyId = async (req, res, next) => {
+ try {
+ const id = req.scheduleId
+ if (id) {
+ const findSchedule = await KU.findOne({ where: { id: id } })
+ if (!findSchedule) throw new Error("해당 일정을 찾지 못했습니다.")
+ else {
+ const { title, start, end, memo } = findSchedule
+ const startDate = dateToString(start, "full")
+ const endDate = dateToString(end, "full")
+ req.schedule = { title, startDate: startDate, endDate: endDate, memo }
+ }
+ next()
+ } else next()
+ } catch (error) {
+ return res.status(500).send(error.message || "일정 가져오는 중 에러 발생")
+ }
+}
+
+const findbyDate = async (req, res, next) => {
try {
+ if (req.date) {
+ const date = new Date(req.date)
+ const findList = await KU.findAll({
+ where: {
+ [Op.and]: [
+ {
+ start: {
+ [Op.lte]: date
+ }
+ }, {
+ end: {
+ [Op.gte]: date
+ }
+ }
+ ]
+ },
+ order: [['updatedAt', 'DESC']]
+ })
+ findList.forEach(schedule => {
+ schedule.dataValues.start = dateToString(schedule.start, "twoYear")
+ schedule.dataValues.end = dateToString(schedule.end, "twoYear")
+ })
+ req.scheduleList = findList
+ next()
+ } else next()
+ } catch (error) {
+ return res.status(500).send(error.message || "일정 가져오는 중 에러 발생")
+ }
+}
+const create = async (req, res) => {
+ try {
+ const { title, startDate, endDate, memo } = req.body
+ const start = new Date(startDate)
+ const end = new Date(endDate)
+ const newSchedule = await KU.create({ title: title, start: start, end: end, memo: memo })
+ return res.json(newSchedule)
} catch (error) {
return res.status(500).send(error.message || "일정 등록 중 에러 발생")
}
@@ -10,7 +68,11 @@ const create = async (req, res) => {
const edit = async (req, res) => {
try {
-
+ const { scheduleId } = req.query
+ const { title, startDate, endDate, memo } = req.body
+ const updated = await KU.update({ title: title, start: startDate, end: endDate, memo: memo }, { where: { id: scheduleId } })
+ if (!updated) throw new Error("해당 일정의 일부 정보를 수정하는데 실패하였습니다.")
+ else return res.send(200)
} catch (error) {
return res.status(500).send(error.message || "일정 수정 중 에러 발생")
}
@@ -18,14 +80,51 @@ const edit = async (req, res) => {
const remove = async (req, res) => {
try {
-
+ const { scheduleId } = req.query
+ const deleted = await KU.destroy({ where: { id: scheduleId } })
+ if (!deleted) throw new Error("해당 일정을 삭제하는데 실패하였습니다.")
+ else return res.send(200)
} catch (error) {
return res.status(500).send(error.message || "일정 삭제 중 에러 발생")
}
}
+const querySeparation = async (req, res, next) => {
+ try {
+ const { scheduleId, date } = req.query
+ req.scheduleId = scheduleId
+ req.date = date
+ next()
+ } catch (error) {
+ return res.status(500).send(error.message || "일정 가져오는 중 에러 발생")
+ }
+}
+
+const send = async (req, res) => {
+ try {
+ const result = req.schedule || req.scheduleList
+ return res.json(result)
+ } catch (error) {
+ return res.status(500).send(error.message || "일정 가져오는 중 에러 발생")
+ }
+}
+
+function dateToString(dateObj, method) {
+ const year = dateObj.getFullYear()
+ const year_disit = String(year).substring(2, 4)
+ const month = dateObj.getMonth() + 1
+ const date = dateObj.getDate()
+
+ if (method === "full") return [year, (month > 9 ? "" : "0") + month, (date > 9 ? "" : "0") + date].join("-")
+ else if (method === "twoYear") return [year_disit, (month > 9 ? "" : "0") + month, (date > 9 ? "" : "0") + date].join("-")
+}
+
export default {
+ findbyId,
+ findbyDate,
create,
edit,
- remove
+ remove,
+ querySeparation,
+ send
}
\ No newline at end of file
diff --git a/server/models/ku.model.js b/server/models/ku.model.js
index d9a7093df6331c30dd7ddc95c81d0605add97c91..49ed5357b665a79897e4e964f9a15d0c09a4e02d 100644
--- a/server/models/ku.model.js
+++ b/server/models/ku.model.js
@@ -23,7 +23,8 @@ const KUModel = (sequelize) => {
type: DataTypes.DATE
},
memo: {
- type: DataTypes.TEXT
+ type: DataTypes.TEXT,
+ defaultValue: ""
}
},
{
diff --git a/server/models/schedule.model.js b/server/models/schedule.model.js
index 7fea6c08c4662a32825b6061ddce6258d207544b..91362df9bbea75ffd5a753d9d0c933478be3ed6a 100644
--- a/server/models/schedule.model.js
+++ b/server/models/schedule.model.js
@@ -23,10 +23,12 @@ const ScheduleModel = (sequelize) => {
type: DataTypes.DATE
},
location: {
- type:DataTypes.STRING
+ type:DataTypes.STRING,
+ defaultValue: ""
},
memo: {
- type: DataTypes.TEXT
+ type: DataTypes.TEXT,
+ defaultValue: ""
}
},
{
diff --git a/server/routes/schedule.route.js b/server/routes/schedule.route.js
index 8dedeaa11e8f7e85c242a2fa1ccd89a9f68c258c..b2602992bb749e0db62663ba5284122a418047e0 100644
--- a/server/routes/schedule.route.js
+++ b/server/routes/schedule.route.js
@@ -6,6 +6,7 @@ const router = express.Router();
router
.route("/ku")
+ .get(kuCtrl.querySeparation, kuCtrl.findbyId, kuCtrl.findbyDate, kuCtrl.send)
.post(kuCtrl.create)
.put(kuCtrl.edit)
.delete(kuCtrl.remove)