Skip to content

Commit

Permalink
Add: Bought column (#3)
Browse files Browse the repository at this point in the history
* chore: add column

* feat: apply bought
  • Loading branch information
Simirall authored Aug 21, 2024
1 parent 0dac03c commit 769d155
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 23 deletions.
1 change: 1 addition & 0 deletions migrations/0003_noisy_raider.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `Madamis` ADD `bought` integer NOT NULL;
206 changes: 206 additions & 0 deletions migrations/meta/0003_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
{
"version": "6",
"dialect": "sqlite",
"id": "bb029370-8595-4b6b-b547-cf137e8f8105",
"prevId": "f9cf0395-0599-44ce-9bf4-cf72af4fc42c",
"tables": {
"GameUsers": {
"name": "GameUsers",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": true
},
"gm": {
"name": "gm",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"gameId": {
"name": "gameId",
"type": "integer",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"userId": {
"name": "userId",
"type": "integer",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {
"GameUsers_gameId_Games_id_fk": {
"name": "GameUsers_gameId_Games_id_fk",
"tableFrom": "GameUsers",
"tableTo": "Games",
"columnsFrom": [
"gameId"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
},
"GameUsers_userId_Users_id_fk": {
"name": "GameUsers_userId_Users_id_fk",
"tableFrom": "GameUsers",
"tableTo": "Users",
"columnsFrom": [
"userId"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"Games": {
"name": "Games",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": true
},
"date": {
"name": "date",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"madamisId": {
"name": "madamisId",
"type": "integer",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {
"Games_madamisId_Madamis_id_fk": {
"name": "Games_madamisId_Madamis_id_fk",
"tableFrom": "Games",
"tableTo": "Madamis",
"columnsFrom": [
"madamisId"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"Madamis": {
"name": "Madamis",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": true
},
"title": {
"name": "title",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"link": {
"name": "link",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"player": {
"name": "player",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"gmRequired": {
"name": "gmRequired",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"bought": {
"name": "bought",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"Users": {
"name": "Users",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": true
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"color": {
"name": "color",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
},
"internal": {
"indexes": {}
}
}
7 changes: 7 additions & 0 deletions migrations/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
"when": 1724078667553,
"tag": "0002_bouncy_legion",
"breakpoints": true
},
{
"idx": 3,
"version": "6",
"when": 1724248328072,
"tag": "0003_noisy_raider",
"breakpoints": true
}
]
}
1 change: 1 addition & 0 deletions schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const madamis = sqliteTable("Madamis", {
link: text("link").notNull(),
player: integer("player").notNull(),
gmRequired: integer("gmRequired").notNull(),
bought: integer("bought").notNull(),
});

export const madamisRelations = relations(madamis, ({ many }) => ({
Expand Down
13 changes: 5 additions & 8 deletions src/apis/madamis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const madamisPostSchema = z.object({
link: z.string().url(),
player: z.number().int().min(1).max(6),
gmRequired: z.boolean().transform((b) => Number(b)),
bought: z.boolean().transform((b) => Number(b)),
});

const madamisPutSchema = madamisPostSchema.extend({
Expand Down Expand Up @@ -60,19 +61,15 @@ export const madamisApp = madamisApi
const db = drizzle(c.env.DB);
const body = c.req.valid("json");

const [result] = await db.insert(madamis).values(body).returning();
return c.json(result);
await db.insert(madamis).values(body);
return new Response(null, { status: 204 });
})
.put("/", zValidator("json", madamisPutSchema), async (c) => {
const db = drizzle(c.env.DB);
const body = c.req.valid("json");

const [result] = await db
.update(madamis)
.set(body)
.where(eq(madamis.id, body.id))
.returning();
return c.json(result);
await db.update(madamis).set(body).where(eq(madamis.id, body.id));
return new Response(null, { status: 204 });
})
.delete("/:id", async (c) => {
const db = drizzle(c.env.DB);
Expand Down
28 changes: 13 additions & 15 deletions src/pages/components/MadamisList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,22 @@ export const MadamisList = () => {
key={d.id}
w="20rem"
shadow="sm"
padding="lg"
p="md"
radius="md"
withBorder
>
<Stack>
<Group>
<NavLink
href={d.link}
target="_blank"
label={d.title}
variant="subtle"
active
leftSection={<Link fontSize="1.4rem" />}
style={{
borderRadius: "4px",
}}
/>
</Group>
<NavLink
href={d.link}
target="_blank"
label={d.title}
variant="subtle"
active
leftSection={<Link fontSize="1.4rem" />}
style={{
borderRadius: "4px",
}}
/>
<Group>
<Badge size="xl" color={d.gmRequired ? "orange" : "cyan"}>
GM: {d.gmRequired ? "要" : "レス可"}
Expand All @@ -62,7 +60,7 @@ export const MadamisList = () => {
))}
</Stack>
)}
<AddGameButton madamisId={d.id} />
{Boolean(d.bought) && <AddGameButton madamisId={d.id} />}
</Stack>
</Card>
))}
Expand Down
7 changes: 7 additions & 0 deletions src/pages/components/MadamisModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const formSchema = z.object({
link: z.string().url(),
player: z.coerce.number().int().min(1).max(6),
gmRequired: z.boolean(),
bought: z.boolean(),
});

type FormSchema = z.infer<typeof formSchema>;
Expand Down Expand Up @@ -109,6 +110,12 @@ export const MadamisModal = () => {
{...register("gmRequired")}
error={errors.gmRequired?.message}
/>
<Checkbox
defaultChecked={Boolean(editData?.bought)}
label="購入済み"
{...register("bought")}
error={errors.bought?.message}
/>
<Button mt="md" type="submit" loading={loading}>
{editData ? "更新" : "追加"}
</Button>
Expand Down

0 comments on commit 769d155

Please sign in to comment.