mainimg.controller.ts 1.66 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
9
10

export const createMainimg = asyncWrap(async (reqExp, res, next) => {
  const req = reqExp as TypedRequestAuth<{ userId: string }>;

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

  console.log("body", req.body);

백승민's avatar
백승민 committed
20
21
22
23
  if (!isLength(url ?? "", { min: 1 })) {
    return res.status(422).send("이미지 url을 입력해주세요");
  }

백승민's avatar
백승민 committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  if (!isLength(title ?? "", { min: 1 })) {
    return res.status(422).send("이미지 제목을 입력해주세요");
  }

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

  return res.json(newMainimg);
});

export const getMainimg = asyncWrap(async (req, res) => {
백승민's avatar
백승민 committed
39
40
41
42
43
44
45
46
47
48
49
50
  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
51

백승민's avatar
백승민 committed
52
53
54
55
56
57
58
59
export const updateMainimg = asyncWrap(async (req, res) => {
  const { title, theme, city, url } = req.body as {
    title: string;
    url: string;
    theme: string;
    city: string;
  };
  const { imgId } = req.params;
백승민's avatar
백승민 committed
60

백승민's avatar
백승민 committed
61
62
63
64
65
66
67
68
69
70
71
72
  const updateImg = await mainimgDb.updateOnePost(
    {
      title,
      theme,
      city,
      url,
    },
    imgId
  );

  return res.json(updateImg);
});
백승민's avatar
백승민 committed
73