-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
30 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,39 @@ | ||
import { Handlers } from "$fresh/server.ts"; | ||
import { kv } from "#src/services/database.ts"; | ||
import type { User } from "#src/shared/api.ts"; | ||
import { getUserKey, handleUserNotFound, statusCheck } from "#src/utils/api.ts"; | ||
|
||
export const handler: Handlers<User | null> = { | ||
async GET(_req, ctx) { | ||
const id = ctx.params.id; | ||
const key = ["user", id]; | ||
const user = (await kv.get<User>(key)).value!; | ||
return new Response(JSON.stringify(user)); | ||
const user = (await kv.get<User>(getUserKey(id))).value; | ||
return user ? new Response(JSON.stringify(user)) : handleUserNotFound(id); | ||
}, | ||
|
||
async PUT(req, ctx) { | ||
const id = ctx.params.id; | ||
const user = (await req.json()) as User; | ||
const userKey = ["user", id]; | ||
const userKey = getUserKey(id); | ||
|
||
const userRes = await kv.get(userKey); | ||
if (!userRes.value) return new Response(`no user with id ${id} found`); | ||
if (!userRes.value) return handleUserNotFound(id); | ||
|
||
const ok = await kv.atomic().check(userRes).set(userKey, user).commit(); | ||
if (!ok) throw new Error("Something went wrong."); | ||
statusCheck(ok); | ||
|
||
return new Response(JSON.stringify(user)); | ||
}, | ||
|
||
async DELETE(_req, ctx) { | ||
const id = ctx.params.id; | ||
const userKey = ["user", id]; | ||
const userKey = getUserKey(id); | ||
|
||
const userRes = await kv.get(userKey); | ||
if (!userRes.value) return new Response(`no user with id ${id} found`); | ||
if (!userRes.value) return handleUserNotFound(id); | ||
|
||
const ok = await kv.atomic().check(userRes).delete(userKey).commit(); | ||
if (!ok) throw new Error("Something went wrong."); | ||
statusCheck(ok); | ||
|
||
return new Response(`user ${id} deleted`); | ||
return new Response(`User ${id} deleted`); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export const statusCheck = ( | ||
status: Deno.KvCommitResult | Deno.KvCommitError, | ||
) => { | ||
if (!status) throw new Error("Something went wrong."); | ||
}; | ||
|
||
export const getUserKey = (id: string) => ["user", id]; | ||
|
||
export const handleUserNotFound = (id: string) => | ||
new Response(`No user with id ${id} found`); |