-
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.
Co-authored-by: Qin Guan <[email protected]>
- Loading branch information
1 parent
58343a3
commit 1271638
Showing
9 changed files
with
194 additions
and
70 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 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,18 +1,13 @@ | ||
import dayjs from 'dayjs' | ||
import { eq } from 'drizzle-orm' | ||
import { events } from '~/server/db/schema' | ||
|
||
export default defineProtectedEventHandler(event => ({ | ||
id: event.context.params!.id, | ||
name: 'SST Homecoming 2024', | ||
description: 'SST Homecoming 2024', | ||
location: 'SST', | ||
badgeImage: 'https://www.sst.edu.sg/images/default-source/album/2019-2020/2020-01-24-homecoming/20200124_182000.jpg?sfvrsn=2', | ||
startDateTime: dayjs(Date.now()).valueOf(), | ||
endDateTime: dayjs(Date.now()).valueOf(), | ||
attendees: [ | ||
{ | ||
id: '123', | ||
name: 'Qin Guan', | ||
admissionKey: '123', | ||
}, | ||
], | ||
})) | ||
export default defineProtectedEventHandler(async (event) => { | ||
const eventId = event.context.params!.id | ||
|
||
await event.context.database.delete(events) | ||
.where(eq(events.id, eventId)) | ||
|
||
return sendNoContent(event) | ||
}, { | ||
restrictTo: ['exco'], | ||
}) |
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,18 +1,39 @@ | ||
import dayjs from 'dayjs' | ||
export default defineProtectedEventHandler(async (event) => { | ||
const eventId = event.context.params!.id | ||
|
||
export default defineProtectedEventHandler(event => ({ | ||
id: event.context.params!.id, | ||
name: 'SST Homecoming 2024', | ||
description: 'SST Homecoming 2024', | ||
location: 'SST', | ||
badgeImage: 'https://www.sst.edu.sg/images/default-source/album/2019-2020/2020-01-24-homecoming/20200124_182000.jpg?sfvrsn=2', | ||
startDateTime: dayjs(Date.now()).valueOf(), | ||
endDateTime: dayjs(Date.now()).valueOf(), | ||
attendees: [ | ||
{ | ||
id: '123', | ||
name: 'Qin Guan', | ||
admissionKey: '123', | ||
const result = await event.context.database.query.events.findFirst({ | ||
where: (events, { eq }) => eq(events.id, eventId), | ||
with: { | ||
usersToEvents: { | ||
with: { | ||
user: { | ||
columns: { | ||
id: true, | ||
name: true, | ||
}, | ||
}, | ||
}, | ||
columns: { | ||
admissionKey: true, | ||
}, | ||
}, | ||
}, | ||
], | ||
})) | ||
}) | ||
|
||
if (!result) { | ||
throw createError({ | ||
status: 400, | ||
statusMessage: 'Bad request', | ||
}) | ||
} | ||
|
||
const { usersToEvents, ...data } = result | ||
|
||
return { | ||
...data, | ||
attendees: usersToEvents.map(({ admissionKey, user }) => ({ | ||
...user, | ||
admissionKey, | ||
})), | ||
} | ||
}) |
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,18 +1,49 @@ | ||
import dayjs from 'dayjs' | ||
import { eq } from 'drizzle-orm' | ||
import { z } from 'zod' | ||
import { events } from '~/server/db/schema' | ||
|
||
export default defineProtectedEventHandler(event => ({ | ||
id: event.context.params!.id, | ||
name: 'SST Homecoming 2024', | ||
description: 'SST Homecoming 2024', | ||
location: 'SST', | ||
badgeImage: 'https://www.sst.edu.sg/images/default-source/album/2019-2020/2020-01-24-homecoming/20200124_182000.jpg?sfvrsn=2', | ||
startDateTime: dayjs(Date.now()).valueOf(), | ||
endDateTime: dayjs(Date.now()).valueOf(), | ||
attendees: [ | ||
{ | ||
id: '123', | ||
name: 'Qin Guan', | ||
admissionKey: '123', | ||
}, | ||
], | ||
})) | ||
const updateEventRequestBody = z.object({ | ||
name: z.string(), | ||
description: z.string(), | ||
location: z.string(), | ||
badgeImage: z.string().url(), | ||
startDateTime: z.string().datetime(), | ||
endDateTime: z.string().datetime(), | ||
}) | ||
|
||
export default defineProtectedEventHandler(async (event) => { | ||
const eventId = event.context.params!.id | ||
|
||
const result = await updateEventRequestBody.safeParseAsync(await readBody(event)) | ||
if (!result.success) { | ||
throw createError({ | ||
status: 400, | ||
statusMessage: 'Bad request', | ||
}) | ||
} | ||
|
||
const { data } = result | ||
|
||
const updatedEvent = await event.context.database.update(events) | ||
.set({ | ||
name: data.name, | ||
description: data.description, | ||
location: data.location, | ||
badgeImage: data.badgeImage, | ||
startDateTime: data.startDateTime, | ||
endDateTime: data.endDateTime, | ||
}) | ||
.where(eq(events.id, eventId)) | ||
.returning() | ||
|
||
if (updatedEvent.length > 1) { | ||
throw createError({ | ||
status: 500, | ||
statusMessage: 'Internal server error', | ||
}) | ||
} | ||
|
||
return updatedEvent[0] | ||
}, { | ||
restrictTo: ['exco'], | ||
}) |
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,18 +1,40 @@ | ||
import dayjs from 'dayjs' | ||
|
||
export default defineProtectedEventHandler(event => [{ | ||
id: event.context.params!.id, | ||
name: 'SST Homecoming 2024', | ||
description: 'SST Homecoming 2024', | ||
location: 'SST', | ||
badgeImage: 'https://www.sst.edu.sg/images/default-source/album/2019-2020/2020-01-24-homecoming/20200124_182000.jpg?sfvrsn=2', | ||
startDateTime: dayjs(Date.now()).valueOf(), | ||
endDateTime: dayjs(Date.now()).valueOf(), | ||
attendees: [ | ||
{ | ||
id: '123', | ||
name: 'Qin Guan', | ||
admissionKey: '123', | ||
export default defineProtectedEventHandler(async (event) => { | ||
const result = await event.context.database.query.events.findMany({ | ||
with: { | ||
usersToEvents: { | ||
with: { | ||
user: { | ||
columns: { | ||
id: true, | ||
name: true, | ||
}, | ||
}, | ||
}, | ||
columns: { | ||
admissionKey: true, | ||
}, | ||
}, | ||
}, | ||
], | ||
}]) | ||
}) | ||
|
||
if (!result) { | ||
throw createError({ | ||
status: 400, | ||
statusMessage: 'Bad request', | ||
}) | ||
} | ||
|
||
return result.map((item) => { | ||
const { usersToEvents, ...data } = item | ||
|
||
return { | ||
...data, | ||
attendees: usersToEvents.map(({ admissionKey, user }) => ({ | ||
...user, | ||
admissionKey, | ||
})), | ||
} | ||
}) | ||
}, { | ||
restrictTo: ['exco'], | ||
}) |
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,45 @@ | ||
import { z } from 'zod' | ||
import { events } from '~/server/db/schema' | ||
|
||
const createEventRequestBody = z.object({ | ||
name: z.string(), | ||
description: z.string(), | ||
location: z.string(), | ||
badgeImage: z.string().url(), | ||
startDateTime: z.string().datetime(), | ||
endDateTime: z.string().datetime(), | ||
}) | ||
|
||
export default defineProtectedEventHandler(async (event) => { | ||
const result = await createEventRequestBody.safeParseAsync(await readBody(event)) | ||
if (!result.success) { | ||
throw createError({ | ||
status: 400, | ||
statusMessage: 'Bad request', | ||
}) | ||
} | ||
|
||
const { data } = result | ||
|
||
const createdEvent = await event.context.database.insert(events) | ||
.values({ | ||
name: data.name, | ||
description: data.description, | ||
location: data.location, | ||
badgeImage: data.badgeImage, | ||
startDateTime: data.startDateTime, | ||
endDateTime: data.endDateTime, | ||
}) | ||
.returning() | ||
|
||
if (createdEvent.length > 1) { | ||
throw createError({ | ||
status: 500, | ||
statusMessage: 'Internal server error', | ||
}) | ||
} | ||
|
||
return createdEvent[0] | ||
}, { | ||
restrictTo: ['exco'], | ||
}) |
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