homeman-deno/routes/api/user.ts

125 lines
3.8 KiB
TypeScript

import { Handlers } from '$fresh/server.ts'
import { db, User, UserModel } from '@homeman/models.ts'
import { ulid } from 'https://deno.land/x/ulid@v0.3.0/mod.ts'
import { z } from 'https://deno.land/x/zod@v3.21.4/mod.ts'
const UserCreate = UserModel.omit({ id: true, createdAt: true })
type UserCreate = z.infer<typeof UserCreate>
export const handler: Handlers<User | null> = {
async POST(req, _ctx) {
// handle json or form posts
let user: UserCreate
let redirectInstead = false
const newId = ulid()
if (req.headers.get('content-type')?.includes('json')) {
user = UserCreate.parse(req.json())
} else {
redirectInstead = true
const form = await req.formData()
const avatarFile = form.get('avatar') as File
if (!avatarFile) {
throw new Error('invalid avatar file')
}
// validate png/jpg/webp?
console.log(avatarFile.type)
await Deno.mkdir('./static/uploads', { recursive: true })
const name = `${newId}-${avatarFile.name.replaceAll('/', '')}`
const localAvatarFile = await Deno.open(`./static/uploads/${name}`, {
create: true,
write: true,
})
await avatarFile.stream().pipeTo(localAvatarFile.writable)
user = UserCreate.parse({
name: form.get('name')?.toString(),
avatarUrl: `/uploads/${name}`,
color: form.get('color')?.toString(),
})
}
// post processing
if (user.color && user.color[0] == '#') {
user.color = user.color.substring(1)
}
const newUser: User = { ...user, id: newId, createdAt: new Date() }
const result = await db.users.create({ data: newUser })
if (redirectInstead) {
const url = new URL(req.url)
url.pathname = '/admin'
return Response.redirect(url, 303)
} else {
return new Response(JSON.stringify(result))
}
},
async PUT(req, _ctx) {
// TODO: form or json
let model = UserModel.omit({ createdAt: true }).partial({ avatarUrl: true })
let user: z.infer<typeof model>
let redirectInstead = false
if (req.headers.get('content-type')?.includes('json')) {
// TODO: ensure missing fields don't get set to null?
user = model.parse(req.json())
} else {
redirectInstead = true
const form = await req.formData()
user = model.parse({
id: form.get('id')?.toString(),
name: form.get('name')?.toString(),
avatarUrl: null,
color: form.get('color')?.toString(),
})
const avatarFile = form.get('avatar') as (File | null)
if (!avatarFile) {
user.avatarUrl =
(await db.users.findFirst({ where: { id: user.id } })).avatarUrl
} else {
// validate png/jpg/webp?
console.log(avatarFile.type)
await Deno.mkdir('./static/uploads', { recursive: true })
const name = `${user.id}-${avatarFile.name.replaceAll('/', '')}`
const localAvatarFile = await Deno.open(`./static/uploads/${name}`, {
create: true,
write: true,
})
await avatarFile.stream().pipeTo(localAvatarFile.writable)
}
}
const result = await db.users.update({ data: user })
if (redirectInstead) {
const url = new URL(req.url)
url.pathname = '/admin'
return Response.redirect(url, 303)
} else {
return new Response(JSON.stringify(result))
}
},
async DELETE(req, _ctx) {
// TODO: form or query params or json
let data
if (req.headers.get('content-type')?.includes('json')) {
data = await req.json()
} else {
data = { id: new URL(req.url).searchParams.get('id') }
}
const userData = UserModel.pick({ id: true }).parse(data)
const result = await db.users.delete({ where: userData })
return new Response(JSON.stringify(result))
},
async GET(req, _ctx) {
// TODO: json or query params
const data = await req.json().catch(() => {})
const userData = UserModel.pick({ id: true }).safeParse(data)
if (userData.success) {
return new Response(
JSON.stringify(await db.users.findFirst({ where: userData.data })),
)
} else {
return new Response(JSON.stringify(await db.users.findMany({})))
}
},
}