📚

Express & Prisma【12User Controller】

2022/09/29に公開

Express & Prisma【12User Controller】

YouTube: https://youtu.be/eS8HapBiMyM

postman
{
    "email": "jim@mail.com", 
    "name": "Jim", 
    "password": "jim", 
    "confirm_password": "jim"
}

{
    "email": "jimupdate@mail.com", 
    "name": "Jim update"
}
userControllers.ts
import {Request, Response} from 'express'
import { prisma } from '../utils/prismaClient'

export const getAllUsers = async (_req: Request, res: Response) => {
  try {
    const users = await prisma.user.findMany({
      select: {
        id: true,
        email: true,
        name: true,
      }}
    )
    res.status(200).json(users)
  } catch (error) {
    res.status(500).json({"error": error})
  }
}

export const getUserById = async (req: Request, res: Response) => {
  const id = Number(req.params.id)
  try {
  const user = await prisma.user.findUnique({
    where: {
      id: id,
    },
    select: {
      id: true,
      email: true,
      name: true,
    }
  })
  if (user === null) {
    res.status(404).json({"message": "User do not exist"})
    return
  }
  res.status(200).json(user)
  } catch (error) {
    res.status(500).json({"error": error})
  }
}

export const createUser = async (req: Request, res: Response) => {
  const { email, name, password, confirm_password } = req.body

  if (password !== confirm_password ) {
    res.status(400).json({
      'message': "Password do not match confirm password."
    })
    return
  }

  try {
    const user = await prisma.user.create({
      data: {
        email: email,
        name: name,
        password: password
      },
      select: {
        id: true,
        email: true,
        name: true,
      }
    })
    res.status(200).json(user)
  } catch (error) {
    res.status(500).json({"error": error})
  }
}

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

export const updateUser = async (req: Request, res: Response) => {
  const id = Number(req.params.id)
  const { email, name } = req.body
  try {
    const updateUser = await prisma.user.update({
      where: {
        id: id,
      },
      data: {
        name: name,
        email: email,
      },
      select: {
        id: true,
        email: true,
        name: true,
      }
    })
    res.status(200).json({"message": "user updated success", "user": updateUser})
  } catch (error) {
    res.status(500).json({"error": error})
  }
}
user.ts
import { Router } from "express";
import { createUser, deleteUser, getAllUsers, getUserById, updateUser } from "../controllers/userControllers";

const router = Router()

router.get('/', getAllUsers)
router.get('/:id', getUserById)
router.post('/', createUser)
router.delete('/:id', deleteUser)
router.put('/:id', updateUser)

export default router

Discussion