mainimg.controller.ts 3.29 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
import { TypedRequest } from "../types";
백승민's avatar
백승민 committed
7
8
import { ObjectId } from "mongoose";
import formidable from "formidable";
백승민's avatar
백승민 committed
9

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

백승민's avatar
백승민 committed
14
15
16
17
  const form = formidable({
    uploadDir: "uploads",
    keepExtensions: true,
    multiples: false,
백승민's avatar
백승민 committed
18
19
  });

백승민's avatar
백승민 committed
20
  form.parse(req, async (err, fields, files) => {
백승민's avatar
백승민 committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
    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)) {
백승민's avatar
백승민 committed
36
          const imgRes = await mainimgDb.createMainimg(
백승민's avatar
백승민 committed
37
38
39
40
            { city, title, theme },
            {
              originalfilename,
              newfilename,
Yoon, Daeki's avatar
Yoon, Daeki committed
41
              picturepath: files.mainimg.filepath,
백승민's avatar
백승민 committed
42
43
            }
          );
Yoon, Daeki's avatar
Yoon, Daeki committed
44
          console.log(imgRes);
백승민's avatar
백승민 committed
45
          return res.json(imgRes);
Yoon, Daeki's avatar
Yoon, Daeki committed
46
47
        } else {
          return res.send("에러");
백승민's avatar
백승민 committed
48
49
50
51
        }
      }
    }
  });
백승민's avatar
백승민 committed
52
53
54
});

export const getMainimg = asyncWrap(async (req, res) => {
백승민's avatar
백승민 committed
55
56
57
58
59
60
61
  const mainimgs = await mainimgDb.getMainimg();
  return res.json(mainimgs);
});

export const deleteMainimg = asyncWrap(async (req, res) => {
  const { imgId } = req.params;
  const deleteCount = await mainimgDb.deleteOneMainimg(imgId);
백승민's avatar
백승민 committed
62
  console.log(deleteCount);
백승민's avatar
백승민 committed
63
64
65

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

백승민's avatar
백승민 committed
67
68
69
70
71
72
73
74
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
75

백승민's avatar
백승민 committed
76
77
78
79
  form.parse(req, (err, fields, files) => {
    if (!Array.isArray(files.updatemainimg)) {
      //파일 좁히기 중
      if (
80
81
        !(
          Array.isArray(fields.id) ||
백승민's avatar
백승민 committed
82
83
84
85
86
87
88
89
90
          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;
91
92
93
94
95
        console.log("error");
        if (!(files.updatemainimg === undefined)) {
          const originalfilename = files.updatemainimg?.originalFilename;
          const newfilename = files.updatemainimg.newFilename;
          if (!(originalfilename === null || newfilename === undefined)) {
백승민's avatar
백승민 committed
96
            const imgRes = mainimgDb.updateOneMainimg(
97
98
99
100
101
102
103
              id,
              theme,
              city,
              title,
              originalfilename,
              newfilename
            );
백승민's avatar
백승민 committed
104
            return res.json(imgRes);
105
106
          }
        } else {
백승민's avatar
백승민 committed
107
108
          const imgRes = mainimgDb.updateOneMainimg(id, theme, city, title);
          return res.json(imgRes);
백승민's avatar
백승민 committed
109
110
111
112
        }
      }
    }
  });
백승민's avatar
백승민 committed
113
});