💡

Express & Prisma【13Post Controller】

2022/09/30に公開約3,600字

Express & Prisma【13Post Controller】

YouTube: https://youtu.be/0VcJklMVpKs

postman
{
    "author_id": 1, 
    "title": "alice title1 update", 
    "content": "alice content1 update"
}

{
    "published": true, 
    "title": "alice title1 update", 
    "content": "alice content1 update"
}
postControllers.ts
import {Request, Response} from 'express'
import { prisma } from '../utils/prismaClient'

export const getAllPosts = async (_req: Request, res: Response) => {
  try {
    const posts = await prisma.post.findMany({
      select: {
        id: true,
        title: true,
        content: true,
        published: true,
        createdAt: true,
        updatedAt: true,
        author: {
          select: {
            id: true,
            name: true,
          }
        }
      }
    })
    res.status(200).json(posts)
  } catch (error) {
    res.status(500).json({"error": error})
  }
}

export const getPostById = async (req: Request, res: Response) => {
  const id = Number(req.params.id)
  try {
    const post = await prisma.post.findUnique({
      where: {
        id: id,
      },
      select: {
        id: true,
        title: true,
        content: true,
        published: true,
        createdAt: true,
        updatedAt: true,
        author: {
          select: {
            id: true,
            name: true,
          }
        }
      }
    })
  if (post === null) {
    res.status(404).json({"message": "Post do not exist"})
    return
  }
  res.status(200).json(post)
  } catch (error) {
    res.status(500).json({"error": error})
  }
}

export const createPost = async (req: Request, res: Response) => {
  const {author_id, title, content} = req.body

  try {
    const post = await prisma.post.create({
      data: {
        title: title,
        content: content,
        authorId: author_id
      },
      select: {
        id: true,
        title: true,
        content: true,
        published: true,
        createdAt: true,
        updatedAt: true,
        author: {
          select: {
            id: true,
            name: true,
          }
        }
      }
    })
    res.status(200).json(post)
  } catch (error) {
    res.status(500).json({"error": error})
  }
}

export const deletePost = async (req: Request, res: Response) => {
  const id = Number(req.params.id)
  try {
    await prisma.post.delete({
      where: {
        id: id,
      },
    })
    res.status(200).json({"message": "post deleted success"})
  } catch (error) {
    res.status(500).json({"error": error})
  }
}

export const updatePost = async (req: Request, res: Response) => {
  const id = Number(req.params.id)
  const { title, content, published } = req.body
  try {
    const updatePost = await prisma.post.update({
      where: {
        id: id,
      },
      data: {
        title: title,
        content: content,
        published: published
      },
      select: {
        id: true,
        title: true,
        content: true,
        published: true,
        createdAt: true,
        updatedAt: true,
        author: {
          select: {
            id: true,
            name: true,
          }
        }
      }
    })
    res.status(200).json({"message": "post updated success", "post": updatePost})
  } catch (error) {
    res.status(500).json({"error": error})
  }
}
post.ts
import { Router } from "express";
import { createPost, deletePost, getAllPosts, getPostById, updatePost } from "../controllers/postControllers";

const router = Router()

router.get('/', getAllPosts)
router.get('/:id', getPostById)
router.post('/', createPost)
router.delete('/:id', deletePost)
router.put('/:id', updatePost)

export default router

Discussion

ログインするとコメントできます