Skip to content

Commit

Permalink
Merge pull request #44 from jphacks/feat/hikahana/add-exp-schema
Browse files Browse the repository at this point in the history
EXPスキーマを作成&ユーザサインアップ、サインイン時にテーブル作成。
  • Loading branch information
hikahana authored Nov 13, 2024
2 parents 8137e7f + a58ff9d commit 35a1d08
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 37 deletions.
14 changes: 14 additions & 0 deletions app/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ model User {
email String
photoUrl String
scores Score[]
experiencePoint ExperiencePoint? @relation()
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
Expand Down Expand Up @@ -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])
}
33 changes: 33 additions & 0 deletions app/src/app/api/experiencePoint/new/route.ts
Original file line number Diff line number Diff line change
@@ -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" },
});
}
76 changes: 42 additions & 34 deletions app/src/app/api/user/new/route.ts
Original file line number Diff line number Diff line change
@@ -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" },
});
}
24 changes: 24 additions & 0 deletions app/src/lib/signInAndUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 = "/";
};
18 changes: 15 additions & 3 deletions app/src/types/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

0 comments on commit 35a1d08

Please sign in to comment.