Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
# For more details, please see https://www.gitpod.io/docs/references/gitpod-yml
image: gitpod/workspace-postgres

Expand All @@ -6,7 +7,8 @@ tasks:
command: |
export HMR_HOST=`gp url 3000`
npm run dev

env:
DATABASE_URL: postgres://cherryTec@localhost/todos
ports:
- port: 3000
onOpen: open-browser
Expand All @@ -17,8 +19,10 @@ vscode:
extensions:
- svelte.svelte-vscode

- prisma.prisma

github:
prebuilds:
master: true
branches: true
pullRequests: true
pullRequests: true
81 changes: 81 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
"devDependencies": {
"@sveltejs/adapter-auto": "next",
"@sveltejs/kit": "next",
"prisma": "^3.6.0",
"svelte": "^3.44.0",
"svelte-check": "^2.2.6",
"svelte-preprocess": "^4.9.4",
"tslib": "^2.3.1",
"typescript": "^4.4.3"
},
"type": "module"
}
"type": "module",
"dependencies": {
"@prisma/client": "^3.6.0"
}
}
9 changes: 9 additions & 0 deletions prisma/migrations/20211219124240_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- CreateTable
CREATE TABLE "Todo" (
"uid" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL,
"text" TEXT NOT NULL,
"done" BOOLEAN NOT NULL,

CONSTRAINT "Todo_pkey" PRIMARY KEY ("uid")
);
3 changes: 3 additions & 0 deletions prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "postgresql"
17 changes: 17 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// lear more about this file in the docs: https://pris.ly/d/prism-schema
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

generator client {
provider = "prisma-client-js"
binaryTargets = ["native"]
}

model Todo {
uid String @id @default(cuid())
created_at DateTime
text String
done Boolean
}
5 changes: 5 additions & 0 deletions src/lib/prisma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Prisma, * as PrismaAll from "@prisma/client";

const PrismaClient = Prisma?.PrismaClient || PrismaAll?.PrismaClient;

export default PrismaClient;
48 changes: 35 additions & 13 deletions src/routes/todos/_api.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,58 @@
import type { Request } from "@sveltejs/kit";
import PrismaClient from "$lib/prisma";

// TODO: Persist in database
let todos: Todo[] = [];
const prisma = new PrismaClient();

export const api = (request: Request, data?: Record<string, unknown>) => {
export const api = async (request: Request, data?: Record<string, unknown>) => {
let body = {};
let status = 500;

switch (request.method.toUpperCase()) {
case "GET":
body = todos;
body = await prisma.todo.findMany();
status = 200;
break;
case "POST":
todos.push(data as Todo);
body = await prisma.todo.create({
data: {
created_at: data.creat_at as Date,
done: data.done as boolean,
text: data.text as string

}
})
//todos.push(data as Todo);
body = data;
status = 201;
break;
case "DELETE":
todos = todos.filter(todo => todo.uid !== request.params.uid)
body = await prisma.todo.delete({
where: {
uid:request.params.uid
}
})
//todos = todos.filter(todo => todo.uid !== request.params.uid)
status = 200;
break;
case "PATCH":
todos = todos.map(todo => {
if (todo.uid === request.params.uid) {
if (data.text) todo.text = data.text as string;
else todo.done = data.done as boolean;
body = await prisma.todo.update({
where:{
uid:request.params.uid
},
data: {
done: data.done,
text: data.text
}
return todo;
});
})
// todos = todos.map(todo => {
// if (todo.uid === request.params.uid) {
// if (data.text) todo.text = data.text as string;
// else todo.done = data.done as boolean;
// }
// return todo;
// });
status = 200;
body = todos.find(todo => todo.uid === request.params.uid);
//body = todos.find(todo => todo.uid === request.params.uid);
break;

default:
Expand Down
2 changes: 1 addition & 1 deletion src/routes/todos/index.json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const get: RequestHandler = (request) => {

export const post: RequestHandler<{}, FormData> = (request) => {
return api(request, {
uid: `${Date.now()}`, // TODO: Replace with the UID from the datbase
//uid: `${Date.now()}`, // TODO: Replace with the UID from the datbase
created_at: new Date(),
text: request.body.get("text"),
done: false
Expand Down