post.db.ts 1.76 KB
Newer Older
Lee Soobeom's avatar
Lee Soobeom committed
1
import { Post, PostType } from "../models";
Lee Soobeom's avatar
Lee Soobeom committed
2
import { FileInfo, IFileInfo } from "../models";
Lee Soobeom's avatar
Lee Soobeom committed
3

Lee Soobeom's avatar
Lee Soobeom committed
4
5
export const createPostRow = async (post: PostType) => {
  const newPostRow = await Post.create({
Lee Soobeom's avatar
Lee Soobeom committed
6
7
8
9
10
11
    title: post.title,
    text: post.text,
    theme: post.theme,
    city: post.city,
    user: post.user,
    date: post.date,
Lee Soobeom's avatar
Lee Soobeom committed
12
    counts: 0,
Lee Soobeom's avatar
Lee Soobeom committed
13
  });
Lee Soobeom's avatar
Lee Soobeom committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  console.log("check", newPostRow);
  return newPostRow;
};

export const createFilesRow = async (
  originalfilename?: string | null,
  newfilename?: string,
  picturepath?: string
) => {
  const newFileRow = await FileInfo.create({
    originalfilename: originalfilename,
    newfilename: newfilename,
    picturepath: picturepath,
  });
  return newFileRow;
};

export const findFileByPostInfo = async (title: string, usreId: string) => {
  const posts = await Post.find({ title: title, userId: usreId });
  return posts;
Lee Soobeom's avatar
Lee Soobeom committed
34
};
Lee Soobeom's avatar
Lee Soobeom committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

export const getPosts = async () => {
  const posts = await Post.find({});
  return posts;
};

export const addOneCount = async (_id: string, counts: number) => {
  const newCounts = await Post.findOneAndUpdate(
    { _id: _id },
    { counts: counts },
    { new: true }
  );
  // console.log(newCounts);

  return newCounts;
};

export const getPost = async (_id: string) => {
  const post = await Post.findOne({ _id: _id });
  return post;
};
Lee Soobeom's avatar
Lee Soobeom committed
56
57
58
59
60
61
62
63
64

export const deletePost = async (_id: string) => {
  const res = await Post.deleteOne({ _id: _id });
  return res;
};

export const updateOnePost = async (post: PostType, _id: string) => {
  const newPost = await Post.findOneAndUpdate(
    { _id: _id },
Lee Soobeom's avatar
Lee Soobeom committed
65
66
67
68
69
70
71
72
73
    {
      title: post.title,
      text: post.text,
      theme: post.theme,
      city: post.city,
      date: post.date,
      counts: post.counts,
      user: post.user,
    },
Lee Soobeom's avatar
Lee Soobeom committed
74
75
76
77
    { new: true }
  );
  return newPost;
};