post.controller.ts 4.16 KB
Newer Older
Lee Soobeom's avatar
Lee Soobeom committed
1
import { NextFunction, Request, Response } from "express";
Lee Soobeom's avatar
Lee Soobeom committed
2
import formidable, { Fields, Files } from "formidable";
Lee Soobeom's avatar
Lee Soobeom committed
3
import { TypedRequestAuth } from "./auth.controller";
Lee Soobeom's avatar
Lee Soobeom committed
4
import { asyncWrap } from "../helpers";
Lee Soobeom's avatar
Lee Soobeom committed
5
6
import { postDb, userDb } from "../db";
import { TypedRequest } from "../types";
Lee Soobeom's avatar
Lee Soobeom committed
7

Lee Soobeom's avatar
Lee Soobeom committed
8
export const createFileAndPost = asyncWrap(async (reqExp, res, next) => {
Lee Soobeom's avatar
Lee Soobeom committed
9
  const req = reqExp as TypedRequestAuth<{ userId: string }>;
Lee Soobeom's avatar
Lee Soobeom committed
10

Lee Soobeom's avatar
Lee Soobeom committed
11
  const { userId } = req.auth;
Lee Soobeom's avatar
Lee Soobeom committed
12

Lee Soobeom's avatar
Lee Soobeom committed
13
14
15
16
  const form = formidable({
    uploadDir: "uploads",
    keepExtensions: true,
    multiples: true,
Lee Soobeom's avatar
Lee Soobeom committed
17
  });
Lee Soobeom's avatar
Lee Soobeom committed
18

Lee Soobeom's avatar
Lee Soobeom committed
19
  const fileIdArr = new Array();
Lee Soobeom's avatar
Lee Soobeom committed
20

Lee Soobeom's avatar
Lee Soobeom committed
21
  form.parse(req, async (err, fields, files) => {
Lee Soobeom's avatar
Lee Soobeom committed
22
23
24
25
26
27
28
29
30
    if (!Array.isArray(fields.title)) {
      const title = fields.title;
      if (!Array.isArray(fields.text)) {
        const text = fields.text;
        if (!Array.isArray(fields.theme)) {
          const theme = fields.theme;
          if (!Array.isArray(fields.city)) {
            const city = fields.city;

Lee Soobeom's avatar
Lee Soobeom committed
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
56
57
58
59
            if (Array.isArray(files.picture)) {
              for (var i = 0; i < files.picture.length; i++) {
                const originalfilename = files.picture?.[i].originalFilename;
                const newfilename = files.picture?.[i].newFilename;
                const filepath = files.picture?.[i].filepath;

                const filesRes = await postDb.createFilesRow(
                  originalfilename,
                  newfilename,
                  filepath
                );

                fileIdArr.push(filesRes);
              }
            } else if (!Array.isArray(files.picture)) {
              const originalfilename = files.picture.originalFilename;
              const newfilename = files.picture.newFilename;
              const filepath = files.picture.filepath;

              const filesRes = await postDb.createFilesRow(
                originalfilename,
                newfilename,
                filepath
              );

              fileIdArr.push(filesRes);
            } // file or one or more

            const postRes = await postDb.createPostRow({
Lee Soobeom's avatar
Lee Soobeom committed
60
61
62
63
64
65
66
              title,
              text,
              theme,
              city,
              date: Date.now(),
              counts: 0,
              user: userId,
Lee Soobeom's avatar
Lee Soobeom committed
67
68
              file: fileIdArr,
            });
Lee Soobeom's avatar
Lee Soobeom committed
69
          }
Lee Soobeom's avatar
Lee Soobeom committed
70
71
72
73
        }
      }
    }
  });
74
});
Lee Soobeom's avatar
Lee Soobeom committed
75
76
77

export const getAllPost = asyncWrap(async (req, res) => {
  const posts = await postDb.getPosts();
Lee Soobeom's avatar
Lee Soobeom committed
78
  // console.log(posts);
Lee Soobeom's avatar
Lee Soobeom committed
79
80
81
82

  return res.json(posts);
});

Lee Soobeom's avatar
Lee Soobeom committed
83
84
85
86
87
88
89
90
export const getFiles = asyncWrap(async (req, res) => {
  const { postId } = req.params;
  // console.log("나는 말하는 고구마.", postId);
  const files = await postDb.getFilesByPostId(postId);

  return res.json(files);
});

Lee Soobeom's avatar
Lee Soobeom committed
91
92
93
94
95
export const addCounts = asyncWrap(async (req, res) => {
  const { postId } = req.params;
  const { counts } = req.body as {
    counts: number;
  };
Lee Soobeom's avatar
Lee Soobeom committed
96
  // console.log(postId, counts);
Lee Soobeom's avatar
Lee Soobeom committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

  const updateCounts = await postDb.addOneCount(postId, counts);

  return res.json(updateCounts);
});

export const userByPostId = (
  reqExp: Request,
  res: Response,
  next: NextFunction,
  postId: string
) => {
  const req = reqExp as TypedRequest;
  req.user = userDb.findUserByPostId(postId);
  next();
};

export const getOnePost = asyncWrap(async (req, res) => {
  const { postId } = req.params;
  const post = await postDb.getPost(postId);

  return res.json(post);
});
Lee Soobeom's avatar
Lee Soobeom committed
120
121
122

export const deleteOnePost = asyncWrap(async (req, res) => {
  const { postId } = req.params;
Lee Soobeom's avatar
Lee Soobeom committed
123
  // console.log(postId);
Lee Soobeom's avatar
Lee Soobeom committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  const deleteCount = await postDb.deletePost(postId);

  return res.json(deleteCount);
});

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

  const { title, text, theme, city, date } = req.body as {
    title: string;
    text: string;
    theme: string;
    city: string;
    date: Date;
    counts: number;
  };

  const userId = req.auth.userId;
  const { postId } = req.params;

  const updatePost = await postDb.updateOnePost(
    {
      title,
      text,
      theme,
      city,
      date: Date.now(),
      counts: req.body.counts,
      user: userId,
    },
    postId
  );

Lee Soobeom's avatar
Lee Soobeom committed
157
  // console.log("게시글 수정 후", updatePost);
Lee Soobeom's avatar
Lee Soobeom committed
158
159
160

  return res.json(updatePost);
});