mainimg.db.ts 2.48 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,
Lee Soobeom's avatar
Lee Soobeom committed
47
  fileInfo?: formidable.File
백승민's avatar
백승민 committed
48
) => {
Lee Soobeom's avatar
Lee Soobeom committed
49
50
51
52
  const newMainimg = await Mainimg.findById(_Id).populate<{
    fileInfo: IFileInfo;
  }>("fileInfo");
  // console.log("error2", newMainimg);
백승민's avatar
백승민 committed
53
  if (!newMainimg) {
Lee Soobeom's avatar
Lee Soobeom committed
54
    throw new Error("mainimg가 존재하지 않습니다");
백승민's avatar
백승민 committed
55
56
  }
  if (
Lee Soobeom's avatar
Lee Soobeom committed
57
58
    fileInfo?.originalFilename &&
    newMainimg?.fileInfo.originalfilename !== fileInfo?.originalFilename
백승민's avatar
백승민 committed
59
60
61
  ) {
    // 같지 않으면 기존의 파일을 디스크에서 삭제한 후
    try {
Lee Soobeom's avatar
Lee Soobeom committed
62
      // console.log("picturepath", newMainimg.fileInfo.picturepath)
백승민's avatar
백승민 committed
63
64
65
      await fs.unlink(newMainimg.fileInfo.picturepath);
    } catch (error) {
      console.log("error", error);
66
    }
백승민's avatar
백승민 committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    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;
Lee Soobeom's avatar
Lee Soobeom committed
81
};