mainimg.controller.ts 1.91 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";
Kim, MinGyu's avatar
Kim, MinGyu committed
7
8
import { ObjectId } from "mongoose";
import formidable from "formidable";
백승민's avatar
백승민 committed
9

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

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

Kim, MinGyu's avatar
Kim, MinGyu committed
20
21
22
23
24
25
26
27
28
29
30
31
32
  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;
백승민's avatar
백승민 committed
33

Kim, MinGyu's avatar
Kim, MinGyu committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
        // if (!isLength(title ?? "", { min: 1 })) {
        //   return res.status(422).send("이미지 제목을 입력해주세요");
        // }
        console.log(files);
        const originalfilename = files.mainimg?.originalFilename;
        const newfilename = files.mainimg.newFilename;
        if (!(originalfilename === null || newfilename === undefined)) {
          mainimgDb.createMainimg(
            { city, title, theme },
            {
              originalfilename,
              newfilename,
            }
          );
        }
      }
    }
백승민's avatar
백승민 committed
51
52
  });

Kim, MinGyu's avatar
Kim, MinGyu committed
53
  res.json();
백승민's avatar
백승민 committed
54
55
56
});

export const getMainimg = asyncWrap(async (req, res) => {
백승민's avatar
백승민 committed
57
58
59
60
61
62
63
64
65
66
67
  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);
});