mainimg.controller.ts 3.53 KB
Newer Older
백승민's avatar
백승민 committed
1
import { NextFunction, Request, Response } from "express";
백승민's avatar
백승민 committed
2
3
4
5
import isLength from "validator/lib/isLength";
import { TypedRequestAuth } from "./auth.controller";
import { asyncWrap } from "../helpers";
import { mainimgDb } from "../db";
백승민's avatar
백승민 committed
6
7
import { ObjectId } from "mongoose";
import formidable from "formidable";
백승민's avatar
백승민 committed
8

백승민's avatar
백승민 committed
9
10
11
export const createMainimg = asyncWrap(async (reqExp, res) => {
  const req = reqExp as TypedRequestAuth<{ userId: ObjectId }>;
  const { userId } = req.auth
백승민's avatar
백승민 committed
12

백승민's avatar
백승민 committed
13
14
15
16
17
18
  // const { theme, city, url, title } = req.body as {
  //   theme: string;
  //   city: string;
  //   url: string;
  //   title: string;
  // };
백승민's avatar
백승민 committed
19

백승민's avatar
백승민 committed
20
  // console.log("body", req.body);
백승민's avatar
백승민 committed
21

백승민's avatar
백승민 committed
22
23
24
25
  const form = formidable({
    uploadDir: "uploads",
    keepExtensions: true,
    multiples: false,
백승민's avatar
백승민 committed
26
27
  });

백승민's avatar
백승민 committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  form.parse(req, (err, fields, files) => {
    if (!Array.isArray(files.mainimg)) {
      //파일 좁히기 중
      if (
        !(
          Array.isArray(fields.city) ||
          Array.isArray(fields.title) ||
          Array.isArray(fields.theme)
        )
      ) {
        const city = fields.city;
        const title = fields.title;
        const theme = fields.theme;
        const originalfilename = files.mainimg?.originalFilename;
        const newfilename = files.mainimg.newFilename;
        if (!(originalfilename === null || newfilename === undefined)) {
          mainimgDb.createMainimg(
            { city, title, theme },
            {
              originalfilename,
              newfilename,
            }
          );
        }
      }
    }
  });
  res.json();
백승민's avatar
백승민 committed
56
57
});

백승민's avatar
백승민 committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

// if (!isLength(url ?? "", { min: 1 })) {
//   return res.status(422).send("이미지 url을 입력해주세요");
// }

// if (!isLength(title ?? "", { min: 1 })) {
//   return res.status(422).send("이미지 제목을 입력해주세요");
// }

//   const newMainimg = await mainimgDb.createMainimg({
//     theme,
//     city,
//     url,
//     title,
//   });

//   return res.json(newMainimg);
// });

백승민's avatar
백승민 committed
77
export const getMainimg = asyncWrap(async (req, res) => {
백승민's avatar
백승민 committed
78
79
80
81
82
83
84
85
86
87
88
89
  const mainimgs = await mainimgDb.getMainimg();
  return res.json(mainimgs);
});


export const deleteMainimg = asyncWrap(async (req, res) => {
  const { imgId } = req.params;
  console.log(imgId);
  const deleteCount = await mainimgDb.deleteOneMainimg(imgId);

  return res.json(deleteCount);
});
백승민's avatar
백승민 committed
90

백승민's avatar
백승민 committed
91
92
93
94
95
96
97
98
99
export const updateMainimg = asyncWrap(async (reqExp, res) => {
  const req = reqExp as TypedRequestAuth<{ userId: ObjectId }>;
  

  const form = formidable({
    uploadDir: "uploads",
    keepExtensions: true,
    multiples: false,
  });
백승민's avatar
백승민 committed
100

백승민's avatar
백승민 committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  form.parse(req, (err, fields, files) => {
    if (!Array.isArray(files.updatemainimg)) {
      //파일 좁히기 중
      if (
        !(Array.isArray(fields.id) ||
          Array.isArray(fields.city) ||
          Array.isArray(fields.title) ||
          Array.isArray(fields.theme)
        )
      ) {
        const id = fields.id;
        const city = fields.city;
        const title = fields.title;
        const theme = fields.theme;
        console.log("error")
        if (!(files.updatemainimg ===undefined)){
        const originalfilename = files.updatemainimg?.originalFilename;
        const newfilename = files.updatemainimg.newFilename;
        if (!(originalfilename === null || newfilename === undefined)) {
          mainimgDb.updateOneMainimg(
            id,
            theme,
            city,
            title,
            originalfilename,
            newfilename
          );
        }
        }

        else {
          mainimgDb.updateOneMainimg(id, theme, city, title)}
      }
    }
  });
  res.json();
백승민's avatar
백승민 committed
137
});
백승민's avatar
백승민 committed
138

백승민's avatar
백승민 committed
139