Commit 671bf731 authored by Choi Ga Young's avatar Choi Ga Young
Browse files

subject관련 DB, server 연결 작업중

parent 58e8f781
...@@ -28,6 +28,7 @@ function App() { ...@@ -28,6 +28,7 @@ function App() {
<Route path="/studyplan/edit" component={StudyPlanEditPage} /> <Route path="/studyplan/edit" component={StudyPlanEditPage} />
<Route path="/studyplan/:" component={StudyPlanPage} /> <Route path="/studyplan/:" component={StudyPlanPage} />
<Route path="/studyplan" component={StudyPlanListPage} /> <Route path="/studyplan" component={StudyPlanListPage} />
<Route path="/subject/edit/:subjectId" component={SubjectEditPage} />
<Route path="/subject/edit" component={SubjectEditPage} /> <Route path="/subject/edit" component={SubjectEditPage} />
<Route path="/admin/edit" component={ScheduleEditPage} /> <Route path="/admin/edit" component={ScheduleEditPage} />
<Route path="/admin" component={AdminPage} /> <Route path="/admin" component={AdminPage} />
......
import axios from "axios";
const addsubject = async (info) => {
console.log('info확인', info)
const url = `/api/subject/addsubject`;
const { data, status } = await axios.post(url, info);
console.log('data status', data, '|', status)
return { data, status }
}
const subjectApi = {
addsubject
};
export default subjectApi
\ No newline at end of file
import { useHistory } from "react-router-dom"; import { useHistory } from "react-router-dom";
import styles from "./buttons.module.scss"; import styles from "./buttons.module.scss";
const BtnGroup = ({ disabled }) => { const BtnGroup = ({ disabled, handleSubmit }) => {
const history = useHistory(); const history = useHistory();
return ( return (
<div className="d-flex justify-content-around my-4"> <div className="d-flex justify-content-around my-4">
<button className="btn btn-white col-5 shadow-none border-dark" type="button" onClick={() => history.goBack()}>취소</button> <button className="btn btn-white col-5 shadow-none border-dark" type="button" onClick={() => history.goBack()}>취소</button>
<button className={`btn btn-crimson col-5 ${styles.disabledBtn}`} type="submit" disabled={disabled}>등록</button> <button className={`btn btn-crimson col-5 ${styles.disabledBtn}`} type="button" onClick={handleSubmit} disabled={disabled}>등록</button>
</div> </div>
) )
} }
......
import { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
import BtnGroup from "../Buttons/BtnGroup"; import BtnGroup from "../Buttons/BtnGroup";
import styles from "./form.module.scss"; import styles from "./form.module.scss";
import { useParams } from 'react-router-dom';
import subjectApi from '../../apis/subject.api';
import catchErrors from '../../utils/catchErrors.js';
const SubjectForm = () => { const SubjectForm = () => {
const { subjectId } = useParams();
const [error, setError] = useState("");
const [subject, setSubject] = useState({ const [subject, setSubject] = useState({
lectureName: "", lectureName: "",
prof: "", prof: "",
...@@ -28,6 +33,22 @@ const SubjectForm = () => { ...@@ -28,6 +33,22 @@ const SubjectForm = () => {
setSubject({ ...subject, [name]: value }) setSubject({ ...subject, [name]: value })
} }
async function handleSubmit(e) {
e.preventDefault();
try {
if (subjectId) {
//수정함수 실행
} else {
await subjectApi.addsubject(subject)
}
} catch (error) {
catchErrors(error, setError)
}
}
return ( return (
<> <>
<div className="position-absolute top-50 start-50 translate-middle" style={{ width: "80%" }}> <div className="position-absolute top-50 start-50 translate-middle" style={{ width: "80%" }}>
...@@ -46,7 +67,7 @@ const SubjectForm = () => { ...@@ -46,7 +67,7 @@ const SubjectForm = () => {
</div> </div>
</div> </div>
<div className="pt-2"> <div className="pt-2">
<BtnGroup disabled={disabled} /> <BtnGroup disabled={disabled} handleSubmit={handleSubmit} />
</div> </div>
</div> </div>
</> </>
......
import { Subject } from '../db/index.js';
const addsubject = async (req, res) => {
console.log('server/addsubject req.body', req.body)
const { lectureName, prof, classRoom } = req.body;
try {
const findName = await Subject.findOne({ where: { name: lectureName } });
if (findName) {
throw new Error("이미 있는 과목입니다.")
}
await Subject.create({
name: lectureName,
prof: prof,
room: classRoom,
})
} catch (error) {
console.log(error)
return res.status(500).send(error.message || "과목저장 에러발생")
}
}
export default {
addsubject
}
\ No newline at end of file
import { Sequelize } from "sequelize" import { Sequelize } from "sequelize"
import dbConfig from "../config/db.config.js" import dbConfig from "../config/db.config.js"
import UserModel from "../models/user.model.js"; import UserModel from "../models/user.model.js";
import TodoModel from "../models/todo.model.js";
import SubjectModel from "../models/subject.model.js";
const sequelize = new Sequelize( const sequelize = new Sequelize(
String(dbConfig.database), String(dbConfig.database),
...@@ -19,8 +21,14 @@ const sequelize = new Sequelize( ...@@ -19,8 +21,14 @@ const sequelize = new Sequelize(
); );
const User = UserModel(sequelize) const User = UserModel(sequelize)
const Todo = TodoModel(sequelize)
const Subject = SubjectModel(sequelize)
Subject.belongsTo(User)
export { export {
sequelize, sequelize,
User, User,
Todo,
Subject
} }
\ No newline at end of file
import Sequelize from "sequelize";
const { DataTypes } = Sequelize;
const SubjectModel = (sequelize) => {
const Subject = sequelize.define(
"subject",
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true
},
name: {
type: DataTypes.STRING
},
prof: {
type: DataTypes.STRING
},
room: {
type: DataTypes.STRING
}
},
{
timestamps: true,
freezeTableName: true,
tableName: "subjects",
}
);
return Subject
};
export default SubjectModel;
\ No newline at end of file
import express from "express"; import express from "express";
import userRouter from './user.route.js'; import userRouter from './user.route.js';
import subjectRouter from './subject.route.js';
const router = express.Router(); const router = express.Router();
router.use('/auth', userRouter) router.use('/auth', userRouter)
router.use('/subject', subjectRouter)
export default router; export default router;
\ No newline at end of file
import express from 'express';
import subjectCtrl from '../controllers/subject.controller.js';
const router = express.Router();
router
.route("/addsubject")
.post(subjectCtrl.addsubject)
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