Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
qin-guan committed Oct 1, 2023
1 parent f505e77 commit 89eaf4d
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 3 deletions.
2 changes: 1 addition & 1 deletion server/api/event/[id].post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default defineProtectedEventHandler(async (event) => {
.where(eq(events.id, eventId))
.returning()

if (updatedEvent.length > 1) {
if (updatedEvent.length !== 1) {
throw createError({
statusCode: 500,
statusMessage: 'Internal server error',
Expand Down
2 changes: 1 addition & 1 deletion server/api/event/index.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default defineProtectedEventHandler(async (event) => {
})
.returning()

if (createdEvent.length > 1) {
if (createdEvent.length !== 1) {
throw createError({
statusCode: 500,
statusMessage: 'Internal server error',
Expand Down
2 changes: 1 addition & 1 deletion server/api/user/[id].post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default defineProtectedEventHandler(async (event) => {
)
.returning()

if (updatedUsers.length > 1) {
if (updatedUsers.length !== 1) {
throw createError({
statusCode: 500,
statusMessage: 'Internal server error',
Expand Down
53 changes: 53 additions & 0 deletions server/api/user/bulk.post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { z } from 'zod'
import { users } from '~/server/db/schema'

const bulkCreateUserRequestBody = z.array(
z.object({
memberId: z.string().nonempty(),
name: z.string().nonempty(),
email: z.string().email(),
graduationYear: z.number().int().min(2011),
memberType: z.enum([
'exco',
'associate',
'affiliate',
'ordinary',
'revoked',
]),
}),
)

export default defineProtectedEventHandler(async (event) => {
const result = await bulkCreateUserRequestBody.safeParseAsync(await readBody(event))
if (!result.success) {
throw createError({
statusCode: 400,
statusMessage: 'Bad request',
})
}

const { data } = result

const createdUsers = await event.context.database.insert(users)
.values(
data.map(user => ({
memberId: user.memberId,
name: user.name,
email: user.email,
graduationYear: user.graduationYear,
memberType: user.memberType,
})),
)
.returning()

if (createdUsers.length !== data.length) {
throw createError({
statusCode: 500,
statusMessage: 'Internal server error',
})
}

return createdUsers
}, {
restrictTo: ['exco'],
})
8 changes: 8 additions & 0 deletions server/api/user/index.get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default defineProtectedEventHandler((event) => {
return event.context.database.query.users.findMany()
}, {
cache: {
maxAge: 60,
},
restrictTo: ['exco'],
})
49 changes: 49 additions & 0 deletions server/api/user/index.post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { z } from 'zod'
import { users } from '~/server/db/schema'

const createUserRequestBody = z.object({
memberId: z.string().nonempty(),
name: z.string().nonempty(),
email: z.string().email(),
graduationYear: z.number().int().min(2011),
memberType: z.enum([
'exco',
'associate',
'affiliate',
'ordinary',
'revoked',
]),
})

export default defineProtectedEventHandler(async (event) => {
const result = await createUserRequestBody.safeParseAsync(await readBody(event))
if (!result.success) {
throw createError({
statusCode: 400,
statusMessage: 'Bad request',
})
}

const { data } = result

const createdUser = await event.context.database.insert(users)
.values({
memberId: data.memberId,
name: data.name,
email: data.email,
graduationYear: data.graduationYear,
memberType: data.memberType,
})
.returning()

if (createdUser.length !== 1) {
throw createError({
statusCode: 500,
statusMessage: 'Internal server error',
})
}

return createdUser[0]
}, {
restrictTo: ['exco'],
})

0 comments on commit 89eaf4d

Please sign in to comment.