mainimg.db.ts 2.46 KB
Newer Older
백승민's avatar
백승민 committed
1
import { 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

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

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

export const getMainimg = async () => {
백승민's avatar
백승민 committed
23
24
25
  console.log("Haha")
  const img = await Mainimg.find({}).populate("fileInfo");
  console.log("adf",img)
백승민's avatar
백승민 committed
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 });
백승민's avatar
백승민 committed
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
47
export const updateOneMainimg = async (
  _Id: string,
  theme: string,
  city: string,
  title: string,
  originalfilename?: string | null,
48
  newfilename?: string
백승민's avatar
백승민 committed
49
50
) => {
  const newMainimg = await Mainimg.findById(_Id);
51
  console.log("error2", _Id);
Lee Soobeom's avatar
Lee Soobeom committed
52
  if (!(newMainimg?.fileInfo === undefined)) {
백승민's avatar
백승민 committed
53
54
55
56
57
    if (originalfilename === undefined) {
      await Mainimg.findByIdAndUpdate(newMainimg._id, {
        theme: theme,
        city: city,
        title: title,
58
59
60
61
62
63
64
65
      });
      console.log("errrror4");
    } else if (
      !(originalfilename === undefined) &&
      (!(theme === undefined) ||
        !(city === undefined) ||
        !(title === undefined))
    ) {
백승민's avatar
백승민 committed
66
67
68
69
      await Mainimg.findByIdAndUpdate(newMainimg._id, {
        theme: theme,
        city: city,
        title: title,
70
      });
Lee Soobeom's avatar
Lee Soobeom committed
71
      await FileInfo.findByIdAndUpdate(newMainimg.fileInfo._id, {
백승민's avatar
백승민 committed
72
73
        originalfilename: originalfilename,
        newfilename: newfilename,
74
75
76
      });
      console.log("error6");
    } else {
Lee Soobeom's avatar
Lee Soobeom committed
77
      await FileInfo.findByIdAndUpdate(newMainimg.fileInfo._id, {
백승민's avatar
백승민 committed
78
79
        originalfilename: originalfilename,
        newfilename: newfilename,
80
81
82
      });
      console.log("error5", newfilename, originalfilename, theme, city, title);
    }
백승민's avatar
백승민 committed
83
  
84
85
  } else console.log("error3", newMainimg);
};