Skip to content

Commit cd49cc8

Browse files
committed
盤面と共にコードを保存/読込するよう変更
1 parent 1e94f82 commit cd49cc8

File tree

8 files changed

+88
-18
lines changed

8 files changed

+88
-18
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
Warnings:
3+
4+
- Added the required column `codeData` to the `Board` table without a default value. This is not possible if the table is not empty.
5+
- Added the required column `isEdited` to the `Board` table without a default value. This is not possible if the table is not empty.
6+
7+
*/
8+
-- AlterTable
9+
ALTER TABLE "Board" ADD COLUMN "codeData" JSONB NOT NULL,
10+
ADD COLUMN "isEdited" BOOLEAN NOT NULL;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
Warnings:
3+
4+
- You are about to drop the column `isEdited` on the `Board` table. All the data in the column will be lost.
5+
6+
*/
7+
-- AlterTable
8+
ALTER TABLE "Board" DROP COLUMN "isEdited";
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
Warnings:
3+
4+
- You are about to drop the column `codeData` on the `Board` table. All the data in the column will be lost.
5+
- You are about to drop the column `data` on the `Board` table. All the data in the column will be lost.
6+
- Added the required column `board` to the `Board` table without a default value. This is not possible if the table is not empty.
7+
- Added the required column `code` to the `Board` table without a default value. This is not possible if the table is not empty.
8+
9+
*/
10+
-- AlterTable
11+
ALTER TABLE "Board" DROP COLUMN "codeData",
12+
DROP COLUMN "data",
13+
ADD COLUMN "board" JSONB NOT NULL,
14+
ADD COLUMN "code" JSONB NOT NULL;

prisma/schema.prisma

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ datasource db {
1818
model Board {
1919
id Int @id @default(autoincrement())
2020
createdAt DateTime @default(now())
21-
data Json
21+
board Json
2222
name String
2323
preview Json
24+
code Json
2425
}
2526

2627
model Code {

src/lib/api/board.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { toast } from "$lib/models/ToastStore.svelte";
22

3-
export async function saveBoard(data: { board: number[][]; name: string }, isJapanese: boolean) {
3+
export async function saveBoard(
4+
data: { board: number[][]; name: string; code: string },
5+
isJapanese: boolean,
6+
) {
47
try {
58
const response = await fetch("/api/board", {
69
method: "POST",
@@ -61,10 +64,15 @@ export async function fetchBoardList(isJapanese: boolean): Promise<BoardListItem
6164
}
6265
}
6366

67+
export type LoadedBoardData = {
68+
board: number[][];
69+
code: string;
70+
};
71+
6472
export async function loadBoardById(
6573
id: number,
6674
isJapanese: boolean,
67-
): Promise<number[][] | undefined> {
75+
): Promise<LoadedBoardData | undefined> {
6876
try {
6977
const response = await fetch(`/api/board?id=${id}`);
7078

@@ -78,7 +86,7 @@ export async function loadBoardById(
7886

7987
const loadedBoard = await response.json();
8088

81-
return loadedBoard as number[][];
89+
return loadedBoard as LoadedBoardData;
8290
} catch (err) {
8391
console.error("Load error", err);
8492
if (isJapanese) {

src/lib/models/BoardManager.svelte.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
import { createBoardPreview } from "$lib/board-preview";
2-
import { saveBoard, fetchBoardList, loadBoardById, type BoardListItem } from "$lib/api/board";
2+
import {
3+
saveBoard,
4+
fetchBoardList,
5+
loadBoardById,
6+
type BoardListItem,
7+
type LoadedBoardData,
8+
} from "$lib/api/board";
39

410
type SaveState =
511
| { saving: false }
6-
| { saving: true; data: number[][]; name: string; preview: number[][] };
12+
| {
13+
saving: true;
14+
board: number[][];
15+
name: string;
16+
preview: number[][];
17+
code: string;
18+
};
719

820
type LoadState =
921
| { state: "closed" }
@@ -16,9 +28,15 @@ export class BoardManager {
1628

1729
constructor() {}
1830

19-
openSaveModal(board: number[][]) {
31+
openSaveModal(board: number[][], code: string) {
2032
const preview = createBoardPreview(board);
21-
this.saveState = { saving: true, data: board, name: "", preview: preview };
33+
this.saveState = {
34+
saving: true,
35+
board: board,
36+
name: "",
37+
preview: preview,
38+
code: code,
39+
};
2240
}
2341

2442
closeSaveModal() {
@@ -28,7 +46,14 @@ export class BoardManager {
2846
async save(isJapanese: boolean) {
2947
if (!this.saveState.saving) return;
3048
const name = this.saveState.name.trim() === "" ? "Unnamed Board" : this.saveState.name.trim();
31-
await saveBoard({ board: this.saveState.data, name }, isJapanese);
49+
await saveBoard(
50+
{
51+
board: this.saveState.board,
52+
name: name,
53+
code: this.saveState.code,
54+
},
55+
isJapanese,
56+
);
3257
this.closeSaveModal();
3358
}
3459

@@ -46,7 +71,7 @@ export class BoardManager {
4671
this.loadState = { state: "closed" };
4772
}
4873

49-
async load(id: number, isJapanese: boolean): Promise<number[][] | undefined> {
74+
async load(id: number, isJapanese: boolean): Promise<LoadedBoardData | undefined> {
5075
this.closeLoadModal();
5176
return await loadBoardById(id, isJapanese);
5277
}

src/routes/+page.svelte

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
break;
8989
}
9090
case "save_board": {
91-
boardManager.openSaveModal(event.data.data as number[][]);
91+
boardManager.openSaveModal(event.data.data as number[][], appliedCode as string);
9292
break;
9393
}
9494
default: {
@@ -110,9 +110,11 @@
110110
}
111111
112112
async function onBoardSelect(id: number) {
113-
const board = await boardManager.load(id, isJapanese);
114-
if (board) {
115-
sendEvent("apply_board", board);
113+
const data = await boardManager.load(id, isJapanese);
114+
if (data) {
115+
editingCode = data.code;
116+
appliedCode = data.code;
117+
sendEvent("apply_board", data.board);
116118
}
117119
}
118120

src/routes/api/board/+server.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as v from "valibot";
66
const BoardSchema = v.object({
77
board: v.array(v.array(v.number())),
88
name: v.pipe(v.string(), v.minLength(1, "盤面名は必須です。")),
9+
code: v.string(),
910
});
1011

1112
export async function POST({ request }) {
@@ -23,14 +24,15 @@ export async function POST({ request }) {
2324
return json({ message: "無効なリクエストデータです。" }, { status: 400 });
2425
}
2526

26-
const { board, name } = result.output;
27+
const { board, name, code } = result.output;
2728
const preview = createBoardPreview(board);
2829

2930
const newState = await prisma.board.create({
3031
data: {
31-
data: board,
32+
board: board,
3233
name: name,
3334
preview: preview,
35+
code: code,
3436
},
3537
});
3638

@@ -49,14 +51,14 @@ export async function GET({ url }) {
4951

5052
const state = await prisma.board.findUnique({
5153
where: { id: id },
52-
select: { data: true },
54+
select: { board: true, code: true },
5355
});
5456

5557
if (!state) {
5658
return json({ message: `ID: ${id} の盤面は見つかりません。` }, { status: 404 });
5759
}
5860

59-
return json(state.data);
61+
return json(state);
6062
} else {
6163
//IDが指定されなかった場合、全ての盤面のリストを返す
6264
const allStates = await prisma.board.findMany({

0 commit comments

Comments
 (0)