jichoi / lms_front star
PsHooN7979 2024-08-09
240809 박세훈 게시글 수정 추가
@1cc12299054625ca7781dc49b5fa5782a0e117cd
client/views/pages/teacher/Board.vue
--- client/views/pages/teacher/Board.vue
+++ client/views/pages/teacher/Board.vue
@@ -1,8 +1,10 @@
 <template>
   <div class="title-box flex justify-between mb40">
     <p class="title">게시판</p>
-    <select name="" id="">
-      <option value="">A반</option>
+    <select>
+      <option v-for="(item, index) in classList" :key="item.id">
+        {{ item.sclsNm }}
+      </option>
     </select>
   </div>
   <div class="search-wrap flex justify-end mb20">
@@ -99,10 +101,14 @@
 
       // 반 아이디 (추후 세션에서 받는걸로 수정)
       sclsId: "1",
+      classList: [],
 
+      userId: "",
       // 검색어
       searchKeyword: "",
       selectedSearchOption: "bbsTtl",
+
+      selected: null,
     };
   },
   methods: {
@@ -146,6 +152,10 @@
           vm.userNm = res.data.result[0].userNm;
           vm.userId = res.data.result[0].userId;
           vm.totalPosts = res.data.totalBoard;
+          vm.selectClass();
+          sessionStorage.removeItem("selectedBoardList");
+          sessionStorage.removeItem("file");
+          console.log(vm.userId);
         })
         .catch(function (error) {
           console.log("result - error : ", error);
@@ -193,6 +203,30 @@
         });
     },
 
+    // 반 조회
+    selectClass() {
+      const vm = this;
+      console.log(vm.userId);
+      axios({
+        url: "/classes/selectClass.json",
+        method: "post",
+        headers: {
+          "Content-Type": "application/json; charset=UTF-8",
+        },
+        data: {
+          userId: vm.userId,
+        },
+      })
+        .then(function (res) {
+          console.log("classData - response : ", res.data);
+          vm.classList = res.data.data;
+          vm.selected = res.data.data.sclsId;
+        })
+        .catch(function (error) {
+          console.log("classData - error : ", error);
+        });
+    },
+
     createNo(index) {
       return this.totalPosts - (this.currentPage * this.itemsPerPage + index);
     },
client/views/pages/teacher/noticeDetail.vue
--- client/views/pages/teacher/noticeDetail.vue
+++ client/views/pages/teacher/noticeDetail.vue
@@ -1,13 +1,13 @@
 <template>
   <div class="title-box flex justify-between mb40">
-    <p class="title">{{ dataList.bbsCls }}</p>
+    <p class="title">{{ category }}</p>
   </div>
   <div class="board-wrap">
     <div class="flex align-center">
-      <label for="" class="title2">{{ dataList.bbsTtl }}</label>
+      <label for="" class="title2">{{ title }}</label>
     </div>
     <hr />
-    <textarea readonly name="" id="">{{ dataList.bbsCnt }}</textarea>
+    <textarea readonly name="" id="">{{ content }}</textarea>
     <hr />
     <div class="flex align-center">
       <label for="" class="title2">첨부파일</label>
@@ -35,7 +35,14 @@
       목록
     </button>
     <div class="flex">
-      <button type="button" title="글쓰기" class="new-btn mr10">수정</button>
+      <button
+        type="button"
+        title="글쓰기"
+        class="new-btn mr10"
+        @click="goToPage('noticeInsert')"
+      >
+        수정
+      </button>
       <button
         type="button"
         title="글쓰기"
@@ -84,7 +91,9 @@
       if (seletedBoardList) {
         vm.dataList = seletedBoardList;
       }
-      console.log(vm.dataList);
+      vm.title = vm.dataList.bbsTtl;
+      vm.content = vm.dataList.bbsCnt;
+      vm.category = vm.dataList.bbsCls;
     },
 
     findFile() {
@@ -102,6 +111,7 @@
         .then(function (res) {
           console.log("fileInfo - response : ", res.data.list[0]);
           vm.file = res.data.list[0];
+          sessionStorage.setItem("file", JSON.stringify(vm.file));
         })
         .catch(function (error) {
           console.log("result - error : ", error);
client/views/pages/teacher/noticeInsert.vue
--- client/views/pages/teacher/noticeInsert.vue
+++ client/views/pages/teacher/noticeInsert.vue
@@ -1,9 +1,9 @@
 <template>
   <div class="title-box flex justify-between mb40">
-    <p class="title">공지 등록</p>
+    <p class="title">{{ titleLabel }}</p>
   </div>
   <div class="board-wrap">
-    <form @submit.prevent="handleSubmit">
+    <form @submit.prevent="handleButtonAction">
       <div class="flex align-center">
         <label for="title" class="title2">제목</label>
         <input type="text" id="title" class="data-wrap" v-model="title" />
@@ -13,13 +13,16 @@
       <hr />
       <div class="flex align-center">
         <label for="file" class="title2">첨부파일</label>
+        <label for="file" class="title2"> {{ file.fileNm }}</label>
         <input type="file" ref="fileInput" @change="handleFileUpload" />
       </div>
       <div class="flex justify-between mt50">
         <button title="글쓰기" class="new-btn" @click="goToPage('Board')">
           목록
         </button>
-        <button title="글쓰기" class="new-btn">등록</button>
+        <button title="글쓰기" class="new-btn" @click="goToPage('Board')">
+          {{ buttonLabel }}
+        </button>
       </div>
     </form>
   </div>
@@ -38,8 +41,14 @@
       title: "",
       content: "",
       category: "Notice",
+      file: "",
       sclsId: "",
+
       selectedFile: null,
+      dataList: "",
+
+      // 데이터 편집 상태 (true = 수정, false = 등록)
+      isEditMode: false,
     };
   },
   methods: {
@@ -55,14 +64,25 @@
     created() {
       const vm = this;
       const sclsId = JSON.parse(sessionStorage.getItem("sclsId"));
+      const dataList = JSON.parse(sessionStorage.getItem("selectedBoardList"));
+      const file = JSON.parse(sessionStorage.getItem("file"));
 
       if (sclsId) {
         vm.sclsId = sclsId;
+        if (dataList && file) {
+          vm.dataList = dataList;
+          vm.file = file;
+          vm.title = vm.dataList.bbsTtl;
+          vm.content = vm.dataList.bbsCnt;
+          vm.category = vm.dataList.bbsCls;
+          vm.isEditMode = true;
+        } else {
+          vm.isEditMode = false;
+        }
       }
-      console.log(sclsId);
     },
-
-    async handleSubmit() {
+    // 게시글 등록
+    async dataInsert() {
       const vm = this;
       try {
         // 파일 업로드
@@ -94,14 +114,76 @@
         await axios.post("/board/insert.json", newData);
 
         alert("게시물 등록 성공");
+        vm.$router.push({ name: "Board" });
       } catch (error) {
         console.error("게시물 등록 실패 ", error);
         alert("게시물 등록 실패");
       }
     },
+
+    // 게시글 수정
+    async boardUpdate() {
+      const vm = this;
+
+      // 파일 업로드
+      const formData = new FormData();
+      formData.append("files", this.selectedFile);
+
+      const fileUploadResponse = await axios.post(
+        "/file/upload.json",
+        formData,
+        {
+          headers: {
+            "Content-Type": "multipart/form-data",
+          },
+        }
+      );
+
+      // 업로드 후 파일 매니지 아이디 호출
+      const fileMngId = fileUploadResponse.data.fileMngId;
+
+      const newData = {
+        bbsTtl: vm.title,
+        bbsCls: vm.category,
+        bbsCnt: vm.content,
+        fileMngId: fileMngId,
+        sclsId: vm.sclsId,
+        bbsId: this.dataList.bbsId,
+      };
+      await axios({
+        url: "/board/update.json",
+        method: "post",
+        headers: {
+          "Content-Type": "application/json; charset=UTF-8",
+        },
+        data: newData,
+      })
+        .then(function (res) {
+          console.log("dataUpdate - response : ", res.data);
+        })
+        .catch(function (error) {
+          console.log("result - error : ", error);
+        });
+    },
+
+    handleButtonAction() {
+      if (this.isEditMode) {
+        this.boardUpdate();
+      } else {
+        this.dataInsert();
+      }
+    },
   },
   watch: {},
-  computed: {},
+  computed: {
+    titleLabel() {
+      return this.isEditMode ? "수정" : "공지 등록";
+    },
+
+    buttonLabel() {
+      return this.isEditMode ? "수정" : "등록";
+    },
+  },
   components: {
     SvgIcon,
   },
Add a comment
List