-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update the teams page to recieve updated data from svelte backend and…
… auto update with that data. Also align admin title with other pages. Signed-off-by: Zachary Licastro <[email protected]>
- Loading branch information
1 parent
1a3d24f
commit c7931f0
Showing
4 changed files
with
98 additions
and
50 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
41 changes: 29 additions & 12 deletions
41
modules/odr_frontend/src/routes/admin/teams/api/+server.ts
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,16 +1,33 @@ | ||
import { pgClient } from '$lib/server/pg'; | ||
import type { RequestHandler } from '@sveltejs/kit'; | ||
|
||
export const POST: RequestHandler = async ({ request }) => { | ||
const body = await request.json(); | ||
if (!body.newTeamName) { | ||
return new Response(JSON.stringify({ success: false, error: 'No Team name provided' })); | ||
} | ||
try { | ||
const result = await pgClient.query('INSERT INTO teams (name) VALUES ($1)', [body.newTeamName]); | ||
console.info(`Added team ${body.newTeamName}`); | ||
return new Response(JSON.stringify({ success: true, result: result })); | ||
} catch (e) { | ||
console.error(`Failed to add team ${body.newTeamName}`); | ||
return new Response(JSON.stringify({ success: false, error: e })); | ||
} | ||
const body = await request.json(); | ||
if (!body.newTeamName) { | ||
return new Response(JSON.stringify({ success: false, error: 'No Team name provided' }), { | ||
status: 400, | ||
headers: { 'Content-Type': 'application/json' } | ||
}); | ||
} | ||
|
||
try { | ||
const result = await pgClient.query( | ||
'INSERT INTO teams (name) VALUES ($1) RETURNING id, name, created_at, updated_at', | ||
[body.newTeamName] | ||
); | ||
|
||
const newTeam = result.rows[0]; | ||
console.info(`Added team ${body.newTeamName} with id ${newTeam.id}`); | ||
|
||
return new Response(JSON.stringify({ success: true, team: newTeam }), { | ||
status: 201, | ||
headers: { 'Content-Type': 'application/json' } | ||
}); | ||
} catch (e) { | ||
console.error(`Failed to add team ${body.newTeamName}:`, e); | ||
return new Response(JSON.stringify({ success: false, error: e.message }), { | ||
status: 500, | ||
headers: { 'Content-Type': 'application/json' } | ||
}); | ||
} | ||
}; |
60 changes: 38 additions & 22 deletions
60
modules/odr_frontend/src/routes/admin/teams/api/addUser/+server.ts
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,28 +1,44 @@ | ||
import { PG_API, pgClient } from '$lib/server/pg'; | ||
import type { RequestHandler } from '@sveltejs/kit'; | ||
|
||
export const POST: RequestHandler = async ({ request }) => { | ||
const body = await request.json(); | ||
if (!body.userId) { | ||
return new Response(JSON.stringify({ success: false, error: 'No user ID provided' })); | ||
} | ||
if (!body.teamId) { | ||
return new Response(JSON.stringify({ success: false, error: 'No team ID provided' })); | ||
} | ||
const body = await request.json(); | ||
if (!body.userId) { | ||
return new Response(JSON.stringify({ success: false, error: 'No user ID provided' }), { | ||
status: 400, | ||
headers: { 'Content-Type': 'application/json' } | ||
}); | ||
} | ||
if (!body.teamId) { | ||
return new Response(JSON.stringify({ success: false, error: 'No team ID provided' }), { | ||
status: 400, | ||
headers: { 'Content-Type': 'application/json' } | ||
}); | ||
} | ||
|
||
const team_users = await PG_API.teams.getUsers(body.teamId); | ||
if (team_users.some((user) => user.user_id == body.userId)) { | ||
return new Response(JSON.stringify({ success: false, error: 'User already in team' })); | ||
} | ||
const team_users = await PG_API.teams.getUsers(body.teamId); | ||
if (team_users.some((user) => user.user_id == body.userId)) { | ||
return new Response(JSON.stringify({ success: false, error: 'User already in team' }), { | ||
status: 400, | ||
headers: { 'Content-Type': 'application/json' } | ||
}); | ||
} | ||
|
||
try { | ||
const result = await pgClient.query( | ||
'INSERT INTO user_teams (team_id, user_id, role) VALUES ($1, $2, $3)', | ||
[body.teamId, body.userId, 'member'] | ||
); | ||
console.info(`Added user ${body.userId} to team ${body.teamId}`); | ||
return new Response(JSON.stringify({ success: true, result: result })); | ||
} catch (e) { | ||
console.error(`Failed to add user ${body.userId} to team ${body.teamId}`); | ||
return new Response(JSON.stringify({ success: false, error: e })); | ||
} | ||
try { | ||
const result = await pgClient.query( | ||
'INSERT INTO user_teams (team_id, user_id, role) VALUES ($1, $2, $3) RETURNING *', | ||
[body.teamId, body.userId, 'member'] | ||
); | ||
console.info(`Added user ${body.userId} to team ${body.teamId}`); | ||
return new Response(JSON.stringify({ success: true, team_user: result.rows[0] }), { | ||
status: 201, | ||
headers: { 'Content-Type': 'application/json' } | ||
}); | ||
} catch (e) { | ||
console.error(`Failed to add user ${body.userId} to team ${body.teamId}:`, e); | ||
return new Response(JSON.stringify({ success: false, error: e.message }), { | ||
status: 500, | ||
headers: { 'Content-Type': 'application/json' } | ||
}); | ||
} | ||
}; |