Skip to content

Commit

Permalink
postgreSQLからデータを取得する
Browse files Browse the repository at this point in the history
  • Loading branch information
okyosi0000 committed Mar 25, 2024
1 parent de1aa66 commit 4bd2f9d
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 60 deletions.
51 changes: 51 additions & 0 deletions app/api/db/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { PrismaClient } from '@prisma/client'
import { NextApiRequest, NextApiResponse } from 'next'
import { NextRequest, NextResponse } from 'next/server'

export async function GET(_request: NextRequest, _response: NextResponse) {
try {
const prisma = new PrismaClient()
const data = await prisma.skills.findMany()
return NextResponse.json({ data }, { status: 200 })
} catch (error) {
NextResponse.json({ error: 'Internal server error' }, { status: 500 })
}
}

async function CREATE(request: NextApiRequest, response: NextApiResponse) {
const { skillType, skillName } = JSON.parse(request.body)
try {
const prisma = new PrismaClient()
// TODO 生成する値は仮埋め
const data = await prisma.skills.create({
select: {
skilltype: true,
skillname: true
},
data: {
skilltype: '',
skillname: ''
}
})
return response.status(200).json({ data })
} catch (error) {
response.status(500).json({ error })
}
}

async function DELETE(request: NextApiRequest, response: NextApiResponse) {
const { id } = JSON.parse(request.body)

try {
const prisma = new PrismaClient()
const data = await prisma.skills.delete({
where: {
id: parseInt(id, 10)
}
})

return response.status(200).json({ data })
} catch (error) {
return response.status(500).json({ error })
}
}
17 changes: 17 additions & 0 deletions app/skills/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
Radar,
RadarChart
} from 'recharts'
import { useEffect, useState } from 'react'

const profile = {
name: 'Yoshi Oka'
Expand Down Expand Up @@ -119,6 +120,22 @@ const GridRechartItem = styled(Grid)({
})

const Home = () => {
const [frontend, setFrontend] = useState(null)
console.log(process.env.NEXT_PUBLIC_API_URL)
useEffect(() => {
;(async () => {
const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/db`)
if (!response.ok) {
throw new Error('Network response was not ok')
}
console.log(response)
const data = await response.json()
setFrontend(data)
})()
}, [])

console.log(frontend)

return (
<Box maxWidth={'80ch'} mr="auto" ml="auto">
<ResponsiveAppBar />
Expand Down
24 changes: 0 additions & 24 deletions lib/create.ts

This file was deleted.

20 changes: 0 additions & 20 deletions lib/delete.ts

This file was deleted.

13 changes: 0 additions & 13 deletions lib/get.ts

This file was deleted.

3 changes: 0 additions & 3 deletions lib/prisma.ts

This file was deleted.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"recharts": "^2.12.0",
"rss": "^1.2.2"
},
"prisma": {
"schema": "prisma/schema.prisma"
},
"prettier": {
"arrowParens": "always",
"singleQuote": true,
Expand Down

0 comments on commit 4bd2f9d

Please sign in to comment.