Skip to content

Commit e2c13f0

Browse files
authored
Merge pull request #13 from sinProject-Inc/9-register-task
close #9
2 parents 422bd80 + aabb8d9 commit e2c13f0

9 files changed

Lines changed: 319 additions & 6 deletions

File tree

.env.example

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Environment variables declared in this file are automatically made available to Prisma.
2+
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema
3+
4+
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
5+
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
6+
7+
DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public"

.vite/deps_temp/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"type":"module"}

package-lock.json

Lines changed: 97 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@
2323
"eslint-plugin-svelte3": "^4.0.0",
2424
"prettier": "^2.6.2",
2525
"prettier-plugin-svelte": "^2.7.0",
26+
"prisma": "^4.6.1",
2627
"svelte": "^3.44.0",
2728
"svelte-check": "^2.7.1",
2829
"svelte-preprocess": "^4.10.6",
2930
"tslib": "^2.3.1",
3031
"typescript": "^4.7.4",
3132
"vite": "^3.1.0"
3233
},
33-
"type": "module"
34+
"type": "module",
35+
"dependencies": {
36+
"@prisma/client": "^4.6.1",
37+
"svelte-dialogs": "^1.2.2"
38+
}
3439
}

prisma/schema.prisma

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// This is your Prisma schema file,
2+
// learn more about it in the docs: https://pris.ly/d/prisma-schema
3+
4+
generator client {
5+
provider = "prisma-client-js"
6+
}
7+
8+
datasource db {
9+
provider = "mysql"
10+
url = env("DATABASE_URL")
11+
}
12+
13+
model Task {
14+
id Int @id @default(autoincrement())
15+
name String? @db.VarChar(255)
16+
description String @db.VarChar(255)
17+
price Int @db.Int
18+
updateDate DateTime @db.DateTime
19+
createDate DateTime @default(now())
20+
}

src/lib/database/database.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import prisma from '@prisma/client'
2+
3+
export const db = new prisma.PrismaClient()
4+
5+
export class Database {
6+
public static async getAppSettingInt(key: string): Promise<number> {
7+
const appSetting = await db.appSetting.findUnique({ where: { key } })
8+
const number_value = Number(appSetting?.value)
9+
const number_value_not_nan = Number.isNaN(number_value) ? 0 : number_value
10+
11+
return number_value_not_nan
12+
}
13+
14+
public static async getAppSettingString(key: string): Promise<string> {
15+
const appSetting = await db.appSetting.findUnique({ where: { key } })
16+
17+
const string_value = appSetting?.value.toString() ?? ''
18+
19+
return string_value
20+
}
21+
}

src/routes/+page.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
<h1>Welcome to SvelteKit</h1>
2-
<p>Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation</p>
1+
<h1>Mission</h1>
2+
<p><a href="./register_task">タスクを登録する</a></p>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { db } from '$lib/database/database'
2+
import type { Actions } from '@sveltejs/kit'
3+
4+
export const actions: Actions = {
5+
default: async ({ request, locals }) => {
6+
const data = await request.formData()
7+
const task = data.get('task') as string
8+
const description = data.get('description') as string
9+
const price = data.get('price') as string
10+
const priceIntvalue = Number(price)
11+
await db.task.create({ data: { name: task, description: description, price: priceIntvalue, createDate: new Date(), updateDate: new Date() } })
12+
return { success: true, message: 'タスクを登録しました' }
13+
},
14+
}

0 commit comments

Comments
 (0)