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

관람료-상영관타입 연관 완성

parent 9fdda5c9
...@@ -16,8 +16,8 @@ const getTicketFee = async () => { ...@@ -16,8 +16,8 @@ const getTicketFee = async () => {
return data return data
} }
const getTicketFeeOne = async (theaterType) => { const getTicketFeeOne = async (theatertypeId) => {
const { data } = await axios.get(`${baseUrl}/api/info/ticketfee/${theaterType}`) const { data } = await axios.get(`${baseUrl}/api/info/ticketfee/${theatertypeId}`)
return data return data
} }
...@@ -26,8 +26,8 @@ const editTicketFee = async (ticketFeeInfo) => { ...@@ -26,8 +26,8 @@ const editTicketFee = async (ticketFeeInfo) => {
return data return data
} }
const removeTicketFee = async (theaterType) => { const removeTicketFee = async (theatertypeId) => {
const { data } = await axios.delete(`${baseUrl}/api/info/ticketfee?theaterType=${theaterType}`) const { data } = await axios.delete(`${baseUrl}/api/info/ticketfee?theaterTypeId=${theatertypeId}`)
return data return data
} }
......
...@@ -4,7 +4,8 @@ import catchErrors from "../../utils/catchErrors.js"; ...@@ -4,7 +4,8 @@ import catchErrors from "../../utils/catchErrors.js";
import styles from "./admin.module.scss"; import styles from "./admin.module.scss";
const INIT_TICKETFEE = { const INIT_TICKETFEE = {
theaterType: "", theatertypeId: 0,
theaterTypeName: "",
weekdays: "", weekdays: "",
weekend: "", weekend: "",
morning: "", morning: "",
...@@ -43,10 +44,11 @@ const TicketEditForm = ({ editFee, formRef }) => { ...@@ -43,10 +44,11 @@ const TicketEditForm = ({ editFee, formRef }) => {
return ( return (
<form ref={formRef} onSubmit={handleSubmit}> <form ref={formRef} onSubmit={handleSubmit}>
{console.log("fidsapd===",editFee)}
<div className="d-flex row row-cols-2 mb-2 mb-md-3 gx-0 gy-2 gy-md-0"> <div className="d-flex row row-cols-2 mb-2 mb-md-3 gx-0 gy-2 gy-md-0">
<label htmlfor="theaterType" className="col-md-auto col-form-label text-center text-md-start">상영관 종류</label> <label htmlfor="theaterType" className="col-md-auto col-form-label text-center text-md-start">상영관 종류</label>
<div className="col-md-4 col-lg-3 mx-md-2"> <div className="col-md-4 col-lg-3 mx-md-2">
<input className={`form-control ${styles.shadowNone}`} type="text" id="theaterType" name="theaterType" value={ticketFee.theaterType} onChange={handleChange} /> <input className={`form-control ${styles.shadowNone}`} type="text" id="theaterTypeName" name="theaterTypeName" value={ticketFee.theaterTypeName} onChange={handleChange} />
</div> </div>
<label htmlfor="defaultPrice" className="col-md-auto col-form-label text-center text-md-start">기본 가격</label> <label htmlfor="defaultPrice" className="col-md-auto col-form-label text-center text-md-start">기본 가격</label>
<div className="col-md-3 col-lg-2 mx-md-2"> <div className="col-md-3 col-lg-2 mx-md-2">
......
...@@ -20,10 +20,10 @@ const TicketFeeTable = ({ setEditFee, formRef }) => { ...@@ -20,10 +20,10 @@ const TicketFeeTable = ({ setEditFee, formRef }) => {
} }
} }
async function editRow(theaterType) { async function editRow(theatertypeId) {
try { try {
setError("") setError("")
const res = await cinemaApi.getTicketFeeOne(theaterType) const res = await cinemaApi.getTicketFeeOne(theatertypeId)
setEditFee({ ...res }) setEditFee({ ...res })
formRef?.current.scrollIntoView({ behavior: "smooth", block: "center" }) formRef?.current.scrollIntoView({ behavior: "smooth", block: "center" })
} catch (error) { } catch (error) {
...@@ -31,10 +31,10 @@ const TicketFeeTable = ({ setEditFee, formRef }) => { ...@@ -31,10 +31,10 @@ const TicketFeeTable = ({ setEditFee, formRef }) => {
} }
} }
async function deleteData(theaterType) { async function deleteData(theatertypeId) {
try { try {
setError("") setError("")
await cinemaApi.removeTicketFee(theaterType) await cinemaApi.removeTicketFee(theatertypeId)
alert("해당 관람료 정보를 성공적으로 삭제했습니다.") alert("해당 관람료 정보를 성공적으로 삭제했습니다.")
getInfo() getInfo()
} catch (error) { } catch (error) {
...@@ -64,7 +64,7 @@ const TicketFeeTable = ({ setEditFee, formRef }) => { ...@@ -64,7 +64,7 @@ const TicketFeeTable = ({ setEditFee, formRef }) => {
{ticketFee.length !== 0 ? ticketFee.map(info => {ticketFee.length !== 0 ? ticketFee.map(info =>
<> <>
<tr> <tr>
<td rowSpan="6" className={`d-block d-md-table-cell ${styles.Row} ${styles.type}`}>{info.theaterType}</td> <td rowSpan="6" className={`d-block d-md-table-cell ${styles.Row} ${styles.type}`}>{info.theatertype.theaterTypeName}</td>
<td rowSpan="3" className={`d-block d-md-table-cell ${styles.Row} ${styles.moreData}`} data-label="- 청소년 / 성인 / 경로">주중(~)</td> <td rowSpan="3" className={`d-block d-md-table-cell ${styles.Row} ${styles.moreData}`} data-label="- 청소년 / 성인 / 경로">주중(~)</td>
<td className="d-inline-block d-md-table-cell">조조 (06:00 ~ )</td> <td className="d-inline-block d-md-table-cell">조조 (06:00 ~ )</td>
<td className="d-inline-block d-md-table-cell">{priceToString(info.weekdays + info.morning + info.youth + info.defaultPrice)}</td> <td className="d-inline-block d-md-table-cell">{priceToString(info.weekdays + info.morning + info.youth + info.defaultPrice)}</td>
...@@ -72,8 +72,8 @@ const TicketFeeTable = ({ setEditFee, formRef }) => { ...@@ -72,8 +72,8 @@ const TicketFeeTable = ({ setEditFee, formRef }) => {
<td className="d-inline-block d-md-table-cell">{priceToString(info.weekdays + info.morning + info.senior + info.defaultPrice)}</td> <td className="d-inline-block d-md-table-cell">{priceToString(info.weekdays + info.morning + info.senior + info.defaultPrice)}</td>
<td rowSpan="6" className="d-none d-md-table-cell"> <td rowSpan="6" className="d-none d-md-table-cell">
<div className="d-flex flex-column"> <div className="d-flex flex-column">
<button type="button" className="btn btn-primary my-1" onClick={() => editRow(info.theaterType)}>수정</button> <button type="button" className="btn btn-primary my-1" onClick={() => editRow(info.theatertypeId)}>수정</button>
<button type="button" className="btn btn-danger my-1" onClick={() => deleteData(info.theaterType)}>삭제</button> <button type="button" className="btn btn-danger my-1" onClick={() => deleteData(info.theatertypeId)}>삭제</button>
</div> </div>
</td> </td>
</tr> </tr>
...@@ -109,8 +109,8 @@ const TicketFeeTable = ({ setEditFee, formRef }) => { ...@@ -109,8 +109,8 @@ const TicketFeeTable = ({ setEditFee, formRef }) => {
<td className="d-inline-block d-md-table-cell">{priceToString(info.weekend + info.night + info.senior + info.defaultPrice)}</td> <td className="d-inline-block d-md-table-cell">{priceToString(info.weekend + info.night + info.senior + info.defaultPrice)}</td>
<td className={`d-block d-md-none ${styles.borderTop}`}> <td className={`d-block d-md-none ${styles.borderTop}`}>
<div className="d-flex justify-content-end"> <div className="d-flex justify-content-end">
<button type="button" className="btn btn-primary" onClick={() => editRow(info.theaterType)}>수정</button> <button type="button" className="btn btn-primary" onClick={() => editRow(info.theatertypeId)}>수정</button>
<button type="button" className="btn btn-danger ms-2" onClick={() => deleteData(info.theaterType)}>삭제</button> <button type="button" className="btn btn-danger ms-2" onClick={() => deleteData(info.theatertypeId)}>삭제</button>
</div> </div>
</td> </td>
</tr> </tr>
......
import { TicketFee } from "../db/index.js"; import { TheaterType, TicketFee } from "../db/index.js";
const getAll = async (req, res) => { const getAll = async (req, res) => {
try { try {
const findAll = await TicketFee.findAll({ attributes: { exclude: ['createdAt', 'updatedAt'] } }) const findAll = await TicketFee.findAll({ attributes: { exclude: ['createdAt', 'updatedAt'] }, include: [ TheaterType ] })
return res.json(findAll) return res.json(findAll)
} catch (error) { } catch (error) {
return res.status(500).send(error.message || "관람료 정보 가져오는 중 에러 발생") return res.status(500).send(error.message || "관람료 정보 가져오는 중 에러 발생")
...@@ -11,8 +11,9 @@ const getAll = async (req, res) => { ...@@ -11,8 +11,9 @@ const getAll = async (req, res) => {
const getOne = async (req, res) => { const getOne = async (req, res) => {
try { try {
const { theaterType } = req.params const { theaterTypeId } = req.params
const find = await TicketFee.findOne({ where: { theaterType: theaterType }, attributes: { exclude: ['createdAt', 'updatedAt'] } }) const find = await TicketFee.findOne({ where: { theatertypeId: theaterTypeId }, attributes: { exclude: ['createdAt', 'updatedAt'] }, include: [ TheaterType ] })
find.dataValues.theaterTypeName = find.dataValues.theatertype.dataValues.theaterTypeName
if (!find) throw new Error("해당 정보를 찾지 못했습니다."); if (!find) throw new Error("해당 정보를 찾지 못했습니다.");
return res.json(find) return res.json(find)
} catch (error) { } catch (error) {
...@@ -22,26 +23,29 @@ const getOne = async (req, res) => { ...@@ -22,26 +23,29 @@ const getOne = async (req, res) => {
const edit = async (req, res) => { const edit = async (req, res) => {
try { try {
const { theaterType } = req.body const { theatertypeId, theaterTypeName, defaultPrice, weekdays, weekend, morning, day, night, youth, adult, senior } = req.body
let response = null let response = null
const result = await TicketFee.findOrCreate({ const result = await TheaterType.findOrCreate({
where: { theaterType: theaterType }, where: { id: theatertypeId },
defaults: { ...req.body } defaults: { theaterTypeName: theaterTypeName }
}) })
if (!result[1]) { if (result[1]) {
const updateData = await TicketFee.update({ ...req.body }, { where: { theaterType: theaterType } }) response = await TicketFee.create({ theatertypeId: result[0].id, defaultPrice, weekdays, weekend, morning, day, night, youth, adult, senior })
response = updateData } else {
} else response = result[0] await TheaterType.update({ theaterTypeName: theaterTypeName }, { where: { id: theatertypeId } })
response = await TicketFee.update({ defaultPrice, weekdays, weekend, morning, day, night, youth, adult, senior }, { where: { theatertypeId: result[0].id } })
}
return res.json(response) return res.json(response)
} catch (error) { } catch (error) {
return res.status(500).send(error.message || "관람료 정보 수정 중 에러 발생") return res.status(500).send(error.message || "관람료 정보 추가 및 수정 중 에러 발생")
} }
} }
const remove = async (req, res) => { const remove = async (req, res) => {
try { try {
const { theaterType } = req.query const { theaterTypeId } = req.query
const delNum = await TicketFee.destroy({ where: { theaterType: theaterType } }) const delNum = await TicketFee.destroy({ where: { theatertypeId: theaterTypeId } })
await TheaterType.destroy({ where: { id: theaterTypeId } })
if (delNum) res.json(delNum) if (delNum) res.json(delNum)
else throw new Error("해당 정보를 서버에서 삭제하는데 실패했습니다."); else throw new Error("해당 정보를 서버에서 삭제하는데 실패했습니다.");
} catch (error) { } catch (error) {
......
...@@ -10,7 +10,7 @@ router ...@@ -10,7 +10,7 @@ router
.put(cinemaCtrl.edit) .put(cinemaCtrl.edit)
router router
.route("/ticketfee/:theaterType") .route("/ticketfee/:theaterTypeId")
.get(ticketfeeCtrl.getOne) .get(ticketfeeCtrl.getOne)
router router
......
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