Skip to content

Commit

Permalink
waiter: Add team schema and GET /team/:namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
april83c committed May 24, 2024
1 parent 56246f5 commit 0956f46
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
Binary file modified bun.lockb
Binary file not shown.
48 changes: 45 additions & 3 deletions service/waiter/src/controller/team.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,57 @@
import { env } from '~/util/env';
import { Elysia } from 'elysia';
import { Elysia, NotFoundError, t } from 'elysia';
import { NotImplementedError } from '~/types';
import db from '~/instance/database';
import { teams, teamMembers } from '~/instance/database/schema';
import { InferSelectModel, like } from 'drizzle-orm';

class APITeam {
id: string;
namespace: string;

displayName: string;
description: string;

members: string[];

createdAt: Date;

constructor(team: InferSelectModel<typeof teams>, members: InferSelectModel<typeof teamMembers>[]) {
this.id = team.id;
this.namespace = team.namespace;
this.displayName = team.displayName;
this.description = team.description;
this.createdAt = team.createdAt;

this.members = members.map(m => m.userId);
}
}

export default new Elysia()
.post('/team',
() => { throw new NotImplementedError() },
{ detail: { description: 'Create a new team' } }
)
.get('/team/:namespace',
() => { throw new NotImplementedError() },
{ detail: { description: 'Get team details' } }
async (context) => {
const team = await db.query.teams.findFirst({
where: like(teams.namespace, context.params.namespace)
});
if (team == undefined) throw new NotFoundError();

const members = await db.query.teamMembers.findMany({
where: like(teamMembers.teamId, team.id)
});

// TODO: Remove this workaround? see user.ts
return Response.json(new APITeam(team, members));
},
{
detail: { description: 'Get team details' },
params: t.Object({
namespace: t.String()
})
}
)
.put('/team/:namespace',
() => { throw new NotImplementedError() },
Expand Down
19 changes: 18 additions & 1 deletion service/waiter/src/instance/database/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,21 @@ export const users = table('user', {
// Now for our stuff!

createdAt: integer('createdAt', { mode: 'timestamp_ms' }).notNull().default(sql`(CURRENT_TIMESTAMP)`)
});
});

export const teams = table('team', {
id: text('id').notNull().primaryKey(),
namespace: text('namespace').notNull().unique(),

displayName: text('displayName').notNull(),
description: text('description').notNull(),

createdAt: integer('createdAt', { mode: 'timestamp_ms' }).notNull().default(sql`(CURRENT_TIMESTAMP)`)
});

export const teamMembers = table('teamMember', {
teamId: text('teamId').notNull().references(() => teams.id, { onDelete: 'cascade' }),
userId: text('userId').notNull().references(() => users.id, { onDelete: 'cascade' })
}, (tm) => ({
compoundKey: primaryKey({ columns: [ tm.teamId, tm.userId ] })
}));

0 comments on commit 0956f46

Please sign in to comment.