From d270f14dd2f716612633894fa23b9447fb78fafb Mon Sep 17 00:00:00 2001 From: hikahana <22.h.hanada.nutfes@gmail.com> Date: Wed, 13 Nov 2024 08:20:55 +0000 Subject: [PATCH 1/7] add experiencePoint schema --- app/prisma/schema.prisma | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/app/prisma/schema.prisma b/app/prisma/schema.prisma index 73a96f0..fc079d0 100755 --- a/app/prisma/schema.prisma +++ b/app/prisma/schema.prisma @@ -20,6 +20,7 @@ model User { email String photoUrl String scores Score[] + experiencePoint ExperiencePoint? @relation() createdAt DateTime @default(now()) updatedAt DateTime @default(now()) @updatedAt @@ -62,3 +63,16 @@ model Word { assignment Assignment[] } + +model ExperiencePoint { + id Int @id @default(autoincrement()) + speedPoint Int + similarityPoint Int + totalPoint Int + continuationDay Int + userId Int @unique + createdAt DateTime @default(now()) + updatedAt DateTime @default(now()) + + user User @relation(fields: [userId], references: [id]) +} From b5bc8fc88485bd42234df698913015a0a137003a Mon Sep 17 00:00:00 2001 From: hikahana <22.h.hanada.nutfes@gmail.com> Date: Wed, 13 Nov 2024 08:21:12 +0000 Subject: [PATCH 2/7] add create experiencePoint API --- app/src/app/api/experiencePoint/new/route.ts | 33 ++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 app/src/app/api/experiencePoint/new/route.ts diff --git a/app/src/app/api/experiencePoint/new/route.ts b/app/src/app/api/experiencePoint/new/route.ts new file mode 100644 index 0000000..2a6b651 --- /dev/null +++ b/app/src/app/api/experiencePoint/new/route.ts @@ -0,0 +1,33 @@ +import type { experiencePoint } from "@/types"; +import { prisma } from "@lib/prisma"; +import type { NextRequest } from "next/server"; +import { NextResponse } from "next/server"; + +// experiencePointの新規作成 +export async function POST(req: NextRequest) { + const reader = req.body?.getReader(); + if (!reader) { + return NextResponse.json( + { error: "No request body found" }, + { status: 400 }, + ); + } + const { value } = await reader.read(); + const userId = new TextDecoder().decode(value); + + const id = JSON.parse(userId); + const createExp: experiencePoint = await prisma.experiencePoint.create({ + data: { + speedPoint: 0, + similarityPoint: 0, + totalPoint: 0, + continuationDay: 0, + userId: id, + }, + }); + + return new Response(JSON.stringify({ createExp }), { + status: 200, + headers: { "Content-Type": "application/json" }, + }); +} From e793d46bdebe0bb9fd013d0bf1b2452ce4058a10 Mon Sep 17 00:00:00 2001 From: hikahana <22.h.hanada.nutfes@gmail.com> Date: Wed, 13 Nov 2024 08:22:13 +0000 Subject: [PATCH 3/7] fix addition create experiencePoint --- app/src/app/api/user/new/route.ts | 76 +++++++++++++++++-------------- 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/app/src/app/api/user/new/route.ts b/app/src/app/api/user/new/route.ts index 2384756..78c2402 100644 --- a/app/src/app/api/user/new/route.ts +++ b/app/src/app/api/user/new/route.ts @@ -1,49 +1,57 @@ -import type { NextRequest } from "next/server"; -import { prisma } from "@lib/prisma"; import type { DBUser as User } from "@/types"; +import { prisma } from "@lib/prisma"; +import type { NextRequest } from "next/server"; import { NextResponse } from "next/server"; type ResponseData = { - message: string; + message: string; }; // GETメソッドのハンドラ関数 export function GET() { - // 疎通確認 - return new Response(JSON.stringify({ message: "Hello from Next.js!" }), { - status: 200, - headers: { "Content-Type": "application/json" }, - }); + // 疎通確認 + return new Response(JSON.stringify({ message: "Hello from Next.js!" }), { + status: 200, + headers: { "Content-Type": "application/json" }, + }); } // POSTメソッドをサポートしたい場合(例) export async function POST(req: NextRequest) { - const reader = req.body?.getReader(); - if (!reader) { - return NextResponse.json( - { error: "No request body found" }, - { status: 400 } - ); - } - const { value } = await reader.read(); - const userData = new TextDecoder().decode(value); + const reader = req.body?.getReader(); + if (!reader) { + return NextResponse.json( + { error: "No request body found" }, + { status: 400 }, + ); + } + const { value } = await reader.read(); + const userData = new TextDecoder().decode(value); - // 任意の処理をここで行います(例: データのパース) const userData = new TextDecoder().decode(value); - // TODO ここバリデーション欲しい - const user = JSON.parse(userData); - // プリズマでユーザを登録 - const registerUser: User = await prisma.user.create({ - data: { - uid: user.uid, - name: user.displayName, - email: user.email, - photoUrl: user.photoURL, - }, - }); + // 任意の処理をここで行います(例: データのパース) const userData = new TextDecoder().decode(value); + // TODO ここバリデーション欲しい + const user = JSON.parse(userData); + // プリズマでユーザ,経験値を登録 + const registerUser: User = await prisma.user.create({ + data: { + uid: user.uid, + name: user.displayName, + email: user.email, + photoUrl: user.photoURL, + experiencePoint: { + create: { + speedPoint: 0, + similarityPoint: 0, + totalPoint: 0, + continuationDay: 0, + }, + }, + }, + }); - // 疎通確認 - return new Response(JSON.stringify({ registerUser }), { - status: 200, - headers: { "Content-Type": "application/json" }, - }); + // 疎通確認 + return new Response(JSON.stringify({ registerUser }), { + status: 200, + headers: { "Content-Type": "application/json" }, + }); } From 02dd908064b673ce6fc0c647e79ef830d21be46f Mon Sep 17 00:00:00 2001 From: hikahana <22.h.hanada.nutfes@gmail.com> Date: Wed, 13 Nov 2024 08:22:35 +0000 Subject: [PATCH 4/7] add type experiencePoint --- app/src/types/index.tsx | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/app/src/types/index.tsx b/app/src/types/index.tsx index 09b3229..f7d9788 100644 --- a/app/src/types/index.tsx +++ b/app/src/types/index.tsx @@ -103,7 +103,19 @@ export type todayAssignment = { }; export type latestAssignment = { - assignmentId: number; - english: string; + assignmentId: number; + english: string; +}; + +export type experiencePoint = { + id: number; + speedPoint: number; + similarityPoint: number; + totalPoint: number; + continuationDay: number; + userId: number; + createdAt: Date; + updatedAt: Date; -} + user?: User; +}; From 926c295d7cb61a1f230584e724b62afe03d94898 Mon Sep 17 00:00:00 2001 From: hikahana <22.h.hanada.nutfes@gmail.com> Date: Wed, 13 Nov 2024 08:23:26 +0000 Subject: [PATCH 5/7] =?UTF-8?q?fix=20=E3=83=A6=E3=83=BC=E3=82=B6=E3=81=AE?= =?UTF-8?q?=E3=82=B5=E3=82=A4=E3=83=B3=E3=82=A4=E3=83=B3=E3=80=81=E3=82=A2?= =?UTF-8?q?=E3=83=83=E3=83=97=E6=99=82=E3=81=ABexperience=E3=82=92?= =?UTF-8?q?=E4=BD=9C=E6=88=90=E3=81=95=E3=81=9B=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/lib/signInAndUp.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/app/src/lib/signInAndUp.ts b/app/src/lib/signInAndUp.ts index 89ec934..5663015 100644 --- a/app/src/lib/signInAndUp.ts +++ b/app/src/lib/signInAndUp.ts @@ -23,6 +23,10 @@ export const signInOrUp = async (firebaseUser: FirebaseUser) => { if (res.status === 200) { storeStorageUser(user); + const userData = await res.json(); + if (!userData.experiencePoint) { + await createExp(userData.id); + } toRoot(); } else { await signUp(user); @@ -53,6 +57,26 @@ const signUp = async (user: User) => { } }; +const createExp = async (userId: number) => { + try { + const res = await fetch("/api/experiencePoint/new", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(userId), + }); + + if (res.status === 200) { + toRoot(); + } else { + throw new Error("作成に失敗しました"); + } + } catch (error) { + console.error("エラーが発生しました:", error); + } +}; + const toRoot = () => { window.location.href = "/"; }; From c549c238686ce65176d3494a97e1f71c9d1df53e Mon Sep 17 00:00:00 2001 From: hikahana <22.h.hanada.nutfes@gmail.com> Date: Wed, 13 Nov 2024 08:41:03 +0000 Subject: [PATCH 6/7] fix build error --- app/src/app/api/image/route.ts | 110 +++++++++++++++------------------ 1 file changed, 51 insertions(+), 59 deletions(-) diff --git a/app/src/app/api/image/route.ts b/app/src/app/api/image/route.ts index ead6808..1b359d8 100644 --- a/app/src/app/api/image/route.ts +++ b/app/src/app/api/image/route.ts @@ -1,77 +1,69 @@ -import type { NextApiResponse } from "next"; import type { NextRequest } from "next/server"; import { OpenAI } from "openai"; -type ResponseData = { - message: string; -}; - const openai = new OpenAI({ - apiKey: process.env.OPENAI_API_KEY, + apiKey: process.env.OPENAI_API_KEY, }); const BUCKET_NAME = "kz2404"; -export async function GET( - req: NextRequest, - res: NextApiResponse -) { - // クエリパラメータを取得 +export async function GET(req: NextRequest) { + // クエリパラメータを取得 const { searchParams } = new URL(req.url || ""); const imageName = searchParams.get("imageName") || "default-image-name"; - const imageURL = `${process.env.NEXT_PUBLIC_MINIO_ENDPOINT}${BUCKET_NAME}/${imageName}`; + const imageURL = `${process.env.NEXT_PUBLIC_MINIO_ENDPOINT}${BUCKET_NAME}/${imageName}`; - return await generateCaption(imageURL) - .then((caption) => { - console.log(caption); - return new Response(JSON.stringify({ caption }), { - status: 200, - headers: { "Content-Type": "application/json" }, - }); - }) - .catch((error) => { - console.error("Error generating caption:", error); - return new Response(JSON.stringify({ message: "エラーが発生しました" }), { - status: 500, - headers: { "Content-Type": "application/json" }, - }); - }); + return await generateCaption(imageURL) + .then((caption) => { + console.log(caption); + return new Response(JSON.stringify({ caption }), { + status: 200, + headers: { "Content-Type": "application/json" }, + }); + }) + .catch((error) => { + console.error("Error generating caption:", error); + return new Response(JSON.stringify({ message: "エラーが発生しました" }), { + status: 500, + headers: { "Content-Type": "application/json" }, + }); + }); } // 画像URLからキャプションを生成する関数 export const generateCaption = async (imageUrl: string) => { - try { - const completion = await openai.chat.completions.create({ - model: "gpt-4o", - messages: [ - { - role: "system", - content: [ - { - type: "text", - text: "A robot that can generate very short captions for images.", - }, - ], - }, - { - role: "user", - content: [ - { - type: "image_url", - image_url: { - url: imageUrl, - detail: "low", - }, - }, - ], - }, - ], - }); + try { + const completion = await openai.chat.completions.create({ + model: "gpt-4o", + messages: [ + { + role: "system", + content: [ + { + type: "text", + text: "A robot that can generate very short captions for images.", + }, + ], + }, + { + role: "user", + content: [ + { + type: "image_url", + image_url: { + url: imageUrl, + detail: "low", + }, + }, + ], + }, + ], + }); - const caption = await completion.choices[0].message.content; - return caption; - } catch (error) { - console.error("Error generating caption:", error); - } + const caption = await completion.choices[0].message.content; + return caption; + } catch (error) { + console.error("Error generating caption:", error); + } }; From a58ff9d0de6b5c710ebaf6ae619acd9636f6d3f9 Mon Sep 17 00:00:00 2001 From: hikahana <22.h.hanada.nutfes@gmail.com> Date: Wed, 13 Nov 2024 11:30:16 +0000 Subject: [PATCH 7/7] Revert "fix build error" This reverts commit c549c238686ce65176d3494a97e1f71c9d1df53e. --- app/src/app/api/image/route.ts | 110 ++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 51 deletions(-) diff --git a/app/src/app/api/image/route.ts b/app/src/app/api/image/route.ts index 1b359d8..ead6808 100644 --- a/app/src/app/api/image/route.ts +++ b/app/src/app/api/image/route.ts @@ -1,69 +1,77 @@ +import type { NextApiResponse } from "next"; import type { NextRequest } from "next/server"; import { OpenAI } from "openai"; +type ResponseData = { + message: string; +}; + const openai = new OpenAI({ - apiKey: process.env.OPENAI_API_KEY, + apiKey: process.env.OPENAI_API_KEY, }); const BUCKET_NAME = "kz2404"; -export async function GET(req: NextRequest) { - // クエリパラメータを取得 +export async function GET( + req: NextRequest, + res: NextApiResponse +) { + // クエリパラメータを取得 const { searchParams } = new URL(req.url || ""); const imageName = searchParams.get("imageName") || "default-image-name"; - const imageURL = `${process.env.NEXT_PUBLIC_MINIO_ENDPOINT}${BUCKET_NAME}/${imageName}`; + const imageURL = `${process.env.NEXT_PUBLIC_MINIO_ENDPOINT}${BUCKET_NAME}/${imageName}`; - return await generateCaption(imageURL) - .then((caption) => { - console.log(caption); - return new Response(JSON.stringify({ caption }), { - status: 200, - headers: { "Content-Type": "application/json" }, - }); - }) - .catch((error) => { - console.error("Error generating caption:", error); - return new Response(JSON.stringify({ message: "エラーが発生しました" }), { - status: 500, - headers: { "Content-Type": "application/json" }, - }); - }); + return await generateCaption(imageURL) + .then((caption) => { + console.log(caption); + return new Response(JSON.stringify({ caption }), { + status: 200, + headers: { "Content-Type": "application/json" }, + }); + }) + .catch((error) => { + console.error("Error generating caption:", error); + return new Response(JSON.stringify({ message: "エラーが発生しました" }), { + status: 500, + headers: { "Content-Type": "application/json" }, + }); + }); } // 画像URLからキャプションを生成する関数 export const generateCaption = async (imageUrl: string) => { - try { - const completion = await openai.chat.completions.create({ - model: "gpt-4o", - messages: [ - { - role: "system", - content: [ - { - type: "text", - text: "A robot that can generate very short captions for images.", - }, - ], - }, - { - role: "user", - content: [ - { - type: "image_url", - image_url: { - url: imageUrl, - detail: "low", - }, - }, - ], - }, - ], - }); + try { + const completion = await openai.chat.completions.create({ + model: "gpt-4o", + messages: [ + { + role: "system", + content: [ + { + type: "text", + text: "A robot that can generate very short captions for images.", + }, + ], + }, + { + role: "user", + content: [ + { + type: "image_url", + image_url: { + url: imageUrl, + detail: "low", + }, + }, + ], + }, + ], + }); - const caption = await completion.choices[0].message.content; - return caption; - } catch (error) { - console.error("Error generating caption:", error); - } + const caption = await completion.choices[0].message.content; + return caption; + } catch (error) { + console.error("Error generating caption:", error); + } };