mirror of
https://github.com/michivonah/themepark-assistant.git
synced 2025-12-22 22:16:29 +01:00
implement themepark endpoints & specifiy new error type
This commit is contained in:
parent
e097d154de
commit
3574559ead
5 changed files with 73 additions and 5 deletions
|
|
@ -4,6 +4,7 @@ import { getUser } from './lib/user-auth'
|
|||
import GitHub from '@auth/core/providers/github'
|
||||
import notification from './routes/notification'
|
||||
import logbook from './routes/logbook'
|
||||
import themepark from './routes/themepark'
|
||||
import cronRouter from './jobs/cron'
|
||||
|
||||
// create app
|
||||
|
|
@ -26,13 +27,14 @@ app.use('/*', verifyAuth())
|
|||
|
||||
// example endpoint
|
||||
app.get('/protected', async (c) => {
|
||||
const user = await getUser(c);
|
||||
return c.json(user);
|
||||
const user = await getUser(c);
|
||||
return c.json(user);
|
||||
})
|
||||
|
||||
// define routes & export app
|
||||
app.route('/notification', notification)
|
||||
app.route('/logbook', logbook)
|
||||
app.route('/themepark', themepark)
|
||||
export default {
|
||||
fetch: app.fetch,
|
||||
scheduled: cronRouter,
|
||||
|
|
|
|||
9
api/src/lib/cache.ts
Normal file
9
api/src/lib/cache.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import { cache } from 'hono/cache'
|
||||
|
||||
/**
|
||||
* Cache unit to use for multiple endpoints as needed
|
||||
*/
|
||||
export const responseCache = cache({
|
||||
cacheName: 'themepark-assistant',
|
||||
cacheControl: 'max-age=86400'
|
||||
});
|
||||
|
|
@ -4,7 +4,7 @@ import { UserSelect } from "../types/user";
|
|||
import { user } from "../db/schema";
|
||||
import { like } from "drizzle-orm";
|
||||
import { DrizzleD1Database } from "drizzle-orm/d1";
|
||||
import { MissingMailError, UserInactiveError } from "../types/error";
|
||||
import { MissingMailError, UserInactiveError, DatabaseError } from "../types/error";
|
||||
|
||||
/**
|
||||
* Returns the details of a user from the given context
|
||||
|
|
@ -27,7 +27,7 @@ export async function getUser(c: Context): Promise<UserSelect>{
|
|||
|
||||
}
|
||||
catch(e){
|
||||
throw new Error(`Database error: ${e}`);
|
||||
throw new DatabaseError();
|
||||
}
|
||||
|
||||
const dbResult = userData[0] ?? await createUser(db, mail);
|
||||
|
|
@ -57,7 +57,7 @@ async function createUser(db: DrizzleD1Database, userMail: string): Promise<User
|
|||
|
||||
}
|
||||
catch(e){
|
||||
throw new Error(`Database error: ${e}`);
|
||||
throw new DatabaseError();
|
||||
}
|
||||
|
||||
return userData[0];
|
||||
|
|
|
|||
51
api/src/routes/themepark.ts
Normal file
51
api/src/routes/themepark.ts
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
import { Hono } from 'hono'
|
||||
import { getDbContext } from '../db/client'
|
||||
import { themepark, attraction } from '../db/schema'
|
||||
import { responseCache } from '../lib/cache'
|
||||
import { eq } from 'drizzle-orm'
|
||||
import { DatabaseError } from '../types/error'
|
||||
|
||||
const app = new Hono()
|
||||
|
||||
/**
|
||||
* Lists all available themeparks with their id, name & countrycode
|
||||
*/
|
||||
app.get('/list', responseCache, async (c) => {
|
||||
const db = getDbContext(c)
|
||||
|
||||
try{
|
||||
const themeparks = await db.select({
|
||||
id: themepark.id,
|
||||
name: themepark.name,
|
||||
countrycode: themepark.countrycode
|
||||
}).from(themepark);
|
||||
|
||||
return c.json(themeparks);
|
||||
}
|
||||
catch{
|
||||
throw new DatabaseError();
|
||||
}
|
||||
})
|
||||
|
||||
/**
|
||||
* Lists all attractions from a themepark with their id & name
|
||||
*/
|
||||
app.get('/list/:id/attraction', responseCache, async (c) => {
|
||||
const parkId = parseInt(c.req.param('id'));
|
||||
const db = getDbContext(c)
|
||||
|
||||
try{
|
||||
const attractions = await db.select({
|
||||
id: attraction.id,
|
||||
name: attraction.name,
|
||||
}).from(attraction)
|
||||
.where(eq(attraction.themeparkId, parkId));
|
||||
|
||||
return c.json(attractions);
|
||||
}
|
||||
catch{
|
||||
throw new DatabaseError();
|
||||
}
|
||||
})
|
||||
|
||||
export default app
|
||||
|
|
@ -11,3 +11,9 @@ export class MissingMailError extends HTTPException{
|
|||
super(400, { message: 'Mail address is missing in authorizaton header.' })
|
||||
}
|
||||
}
|
||||
|
||||
export class DatabaseError extends HTTPException{
|
||||
constructor(){
|
||||
super(500, { message: 'Internal Database Error' })
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue