mainimg.db.ts 3.68 KB
Newer Older
백승민's avatar
백승민 committed
1
import { HydratedDocument, ObjectId } from "mongoose";
Kim, MinGyu's avatar
Kim, MinGyu committed
2
import { FileInfo, IFileInfo, Mainimg, MainimgType } from "../models";
백승민's avatar
백승민 committed
3
4
import fs from "fs/promises";
import { fileInfoCtrl } from "../controllers";
백승민's avatar
백승민 committed
5
import formidable from "formidable";
백승민's avatar
백승민 committed
6

Kim, MinGyu's avatar
Kim, MinGyu committed
7
8
export const createMainimg = async (mainimg: MainimgType, pic: IFileInfo) => {
  const newPic = await FileInfo.create({
백승민's avatar
백승민 committed
9
10
    originalfilename: pic.originalfilename,
    newfilename: pic.newfilename,
백승민's avatar
백승민 committed
11
    picturepath: pic.picturepath,
백승민's avatar
백승민 committed
12
  });
백승민's avatar
백승민 committed
13
14
15
16

  const newMainimg = await Mainimg.create({
    theme: mainimg.theme,
    city: mainimg.city,
백승민's avatar
백승민 committed
17
    fileInfo: newPic._id,
백승민's avatar
백승민 committed
18
19
    title: mainimg.title,
  });
백승민's avatar
백승민 committed
20
  return newMainimg.populate("fileInfo");
백승민's avatar
백승민 committed
21
22
23
};

export const getMainimg = async () => {
Kim, MinGyu's avatar
Kim, MinGyu committed
24
  const img = await Mainimg.find({}).populate("fileInfo");
백승민's avatar
백승민 committed
25
26
27

  return img;
};
백승민's avatar
백승민 committed
28
29

export const deleteOneMainimg = async (_id: string) => {
백승민's avatar
백승민 committed
30
31
32
33
34
35
36
  const main = await Mainimg.findById(_id);
  if (!(main?.fileInfo === undefined)) {
    const ref = await FileInfo.findById(main.fileInfo._id);
    if (!(ref?.newfilename === undefined)) {
      await fs.unlink("../travel/uploads/" + ref?.newfilename);
    }
    await FileInfo.deleteOne({ _id: main.fileInfo._id });
37
38
    const res = await Mainimg.deleteOne({ _id: _id });
    return res;
백승민's avatar
백승민 committed
39
  }
백승민's avatar
백승민 committed
40
};
백승민's avatar
백승민 committed
41

백승민's avatar
백승민 committed
42
43
44
45
46
export const updateOneMainimg = async (
  _Id: string,
  theme: string,
  city: string,
  title: string,
백승민's avatar
백승민 committed
47
48
49
  fileInfo: formidable.File
  // originalfilename?: string | null,
  // newfilename?: string
백승민's avatar
백승민 committed
50
) => {
백승민's avatar
백승민 committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  // const newMainimg = await Mainimg.findById(_Id)
  const newMainimg = await Mainimg.findById(_Id).populate<{ fileInfo: IFileInfo }>(
    "fileInfo"
  );

  console.log("error2", newMainimg);
  if (!newMainimg) {
    throw new Error("mainimg가 존재하지 않습니다")
  }
  if (
    fileInfo.originalFilename &&
    newMainimg?.fileInfo.originalfilename !== fileInfo.originalFilename
  ) {
    // 같지 않으면 기존의 파일을 디스크에서 삭제한 후
    try {
      console.log("picturepath", newMainimg.fileInfo.picturepath)
      await fs.unlink(newMainimg.fileInfo.picturepath);
    } catch (error) {
      console.log("error", error);
70
    }
백승민's avatar
백승민 committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
    const mainimgAvatar = newMainimg.fileInfo as HydratedDocument<IFileInfo>;
    // 기존 fileinfo의 파일이름과 경로 변경 설정
    mainimgAvatar.originalfilename = fileInfo.originalFilename;
    mainimgAvatar.newfilename = fileInfo.newFilename;
    mainimgAvatar.picturepath = fileInfo.filepath;
    await mainimgAvatar.save();
  }


  newMainimg.theme = theme;
  newMainimg.city = city;
  newMainimg.title = title;
  await newMainimg.save();
  console.log("mainimg updated", newMainimg);
  return newMainimg;
}
//   if (!(newMainimg?.fileInfo === undefined)) {
//     if (originalfilename === undefined) {
//       await Mainimg.findByIdAndUpdate(newMainimg._id, {
//         theme: theme,
//         city: city,
//         title: title,
//       });
//       console.log("errrror4");
//     } else if (
//       !(originalfilename === undefined) &&
//       (!(theme === undefined) ||
//         !(city === undefined) ||
//         !(title === undefined))
//     ) {
//       await Mainimg.findByIdAndUpdate(newMainimg._id, {
//         theme: theme,
//         city: city,
//         title: title,
//       });
//       await FileInfo.findByIdAndUpdate(newMainimg.fileInfo._id, {
//         originalfilename: originalfilename,
//         newfilename: newfilename,
//       });
//       console.log("error6");
//     } else {
//       await FileInfo.findByIdAndUpdate(newMainimg.fileInfo._id, {
//         originalfilename: originalfilename,
//         newfilename: newfilename,
//       });
//       console.log("error5", newfilename, originalfilename, theme, city, title);
//     }
//   } else console.log("error3", newMainimg);
// };