HyeonJun Jeon

[[Modify] Migrate subjectData to DB

...@@ -8,7 +8,7 @@ async function route() { ...@@ -8,7 +8,7 @@ async function route() {
8 8
9 // (userID, date) => schedules 9 // (userID, date) => schedules
10 schedules_dateRouter.get("/", async (req, res) => { 10 schedules_dateRouter.get("/", async (req, res) => {
11 - console.log("/db/schedules_date"); 11 + // console.log("/db/schedules_date");
12 try { 12 try {
13 const queryString = ` 13 const queryString = `
14 SELECT sc.label, sc.type, sc.description, sc.url, sc.detail, sbj.name, us.color 14 SELECT sc.label, sc.type, sc.description, sc.url, sc.detail, sbj.name, us.color
......
...@@ -6,12 +6,15 @@ const userSubjectRouter = express.Router(); ...@@ -6,12 +6,15 @@ const userSubjectRouter = express.Router();
6 async function route() { 6 async function route() {
7 const connection = await mysql2.createConnection(connectOption); 7 const connection = await mysql2.createConnection(connectOption);
8 8
9 - // (userID) => subjects 9 + // (userID) => [{userID, subjectID, nickname, status, color, name}, ...]
10 userSubjectRouter.get("/", async (req, res) => { 10 userSubjectRouter.get("/", async (req, res) => {
11 console.log("/db/user-subject"); 11 console.log("/db/user-subject");
12 try { 12 try {
13 const queryString = ` 13 const queryString = `
14 - SELECT subjectID, nickname, status, color FROM \`user-subject\` us 14 + SELECT us.userID, us.subjectID, us.nickname, us.status, us.color, sub.name
15 + FROM \`user-subject\` us
16 + INNER JOIN subjects sub
17 + ON us.subjectID = sub.ID
15 WHERE us.userID = ${req.query.userID}`; 18 WHERE us.userID = ${req.query.userID}`;
16 const [results] = await connection.query(queryString); 19 const [results] = await connection.query(queryString);
17 res.send(results); 20 res.send(results);
...@@ -20,6 +23,23 @@ async function route() { ...@@ -20,6 +23,23 @@ async function route() {
20 res.end(); 23 res.end();
21 } 24 }
22 }); 25 });
26 +
27 + // (userID, subjectID, status)
28 + userSubjectRouter.put("/check", async (req, res) => {
29 + console.log("/db/user-subject/check");
30 + try {
31 + const queryString = `
32 + UPDATE \`user-subject\`
33 + SET status = ${req.body.status}
34 + WHERE userID = ${req.body.userID}
35 + AND subjectID = ${req.body.subjectID}`;
36 + await connection.query(queryString);
37 + res.end();
38 + } catch (e) {
39 + console.log(e);
40 + res.end();
41 + }
42 + });
23 } 43 }
24 route(); 44 route();
25 45
......
...@@ -2,7 +2,6 @@ import { useContext, useEffect, useState } from "react"; ...@@ -2,7 +2,6 @@ import { useContext, useEffect, useState } from "react";
2 import localforage from "localforage"; 2 import localforage from "localforage";
3 3
4 import { toYMD, toYMDStr } from "../utils/Dates"; 4 import { toYMD, toYMDStr } from "../utils/Dates";
5 -import { scheForage } from "../utils/LocalForage";
6 5
7 import { CalendarStateContext } from "../pages/Calendar"; 6 import { CalendarStateContext } from "../pages/Calendar";
8 import ScheduleItem from "./ScheduleItem"; 7 import ScheduleItem from "./ScheduleItem";
......
...@@ -11,12 +11,7 @@ const Side = () => { ...@@ -11,12 +11,7 @@ const Side = () => {
11 11
12 for (const code in subsObj) { 12 for (const code in subsObj) {
13 sideSubjects.push( 13 sideSubjects.push(
14 - <SideSubject 14 + <SideSubject key={code} subject={subsObj[code]} dispatch={dispatch} />
15 - key={code}
16 - code={code}
17 - subject={subsObj[code]}
18 - dispatch={dispatch}
19 - />
20 ); 15 );
21 } 16 }
22 17
......
1 -const SideSubject = ({ code, subject, dispatch }) => { 1 +const SideSubject = ({ subject, dispatch }) => {
2 const defaultColor = "#EFEFEF"; 2 const defaultColor = "#EFEFEF";
3 3
4 const check = (e) => { 4 const check = (e) => {
5 - dispatch({ type: "CHECKED", code }); 5 + dispatch({ type: "CHECKED", subjectID: subject.subjectID });
6 - if (subject.selected) e.target.style["background-color"] = defaultColor; 6 + if (subject.status) e.target.style["background-color"] = defaultColor;
7 else e.target.style["background-color"] = subject.color; 7 else e.target.style["background-color"] = subject.color;
8 }; 8 };
9 9
...@@ -13,7 +13,7 @@ const SideSubject = ({ code, subject, dispatch }) => { ...@@ -13,7 +13,7 @@ const SideSubject = ({ code, subject, dispatch }) => {
13 className="ssc" 13 className="ssc"
14 onClick={check} 14 onClick={check}
15 style={{ 15 style={{
16 - backgroundColor: subject.selected ? subject.color : defaultColor, 16 + backgroundColor: subject.status ? "#" + subject.color : defaultColor,
17 }} 17 }}
18 ></div> 18 ></div>
19 19
......
1 import React, { useEffect, useReducer, useState } from "react"; 1 import React, { useEffect, useReducer, useState } from "react";
2 import { useNavigate, Route, Routes } from "react-router-dom"; 2 import { useNavigate, Route, Routes } from "react-router-dom";
3 3
4 -import { subForage } from "../utils/LocalForage";
5 -
6 import Month from "../components/Month"; 4 import Month from "../components/Month";
7 import Header from "../components/Header"; 5 import Header from "../components/Header";
8 import Side from "../components/Side"; 6 import Side from "../components/Side";
...@@ -14,10 +12,14 @@ export const CalendarStateContext = React.createContext(); ...@@ -14,10 +12,14 @@ export const CalendarStateContext = React.createContext();
14 const render = (subsObj, args) => { 12 const render = (subsObj, args) => {
15 switch (args.type) { 13 switch (args.type) {
16 case "CHECKED": 14 case "CHECKED":
17 - const sub = subsObj[args.code]; 15 + const sub = subsObj[args.subjectID];
18 - sub.selected = !sub.selected; 16 + sub.status = !sub.status;
19 - subForage.setItem(args.code, sub); 17 + axios.put("http://localhost:3001/db/user-subject/check", {
20 - return { ...subsObj, [args.code]: sub }; 18 + userID: sub.userID,
19 + subjectID: args.subjectID,
20 + status: +sub.status,
21 + });
22 + return { ...subsObj, [args.subjectID]: sub };
21 case "INIT": 23 case "INIT":
22 return args.subsObj; 24 return args.subsObj;
23 default: 25 default:
...@@ -40,11 +42,10 @@ const Calendar = () => { ...@@ -40,11 +42,10 @@ const Calendar = () => {
40 if (!(await localforage.getItem("session"))) return navigate("/login"); 42 if (!(await localforage.getItem("session"))) return navigate("/login");
41 // get user's subjects 43 // get user's subjects
42 const userID = await localforage.getItem("userID"); 44 const userID = await localforage.getItem("userID");
43 - const subjects = await axios.get( 45 + const { data: subjects } = await axios.get(
44 "http://localhost:3001/db/user-subject", 46 "http://localhost:3001/db/user-subject",
45 { params: { userID } } 47 { params: { userID } }
46 - ).data; 48 + );
47 - console.log(subjects);
48 let tsubsObj = {}; 49 let tsubsObj = {};
49 for (const sub of subjects) { 50 for (const sub of subjects) {
50 tsubsObj[sub.subjectID] = sub; 51 tsubsObj[sub.subjectID] = sub;
......