-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update API to fetch data from actual DB instead of mock data
Co-authored-by: Evan Suhyeong Lee <[email protected]>
- Loading branch information
Showing
6 changed files
with
29 additions
and
64 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...ntrollers/sqlControllers/deleteProfile.ts → src/controllers/deleteProfile.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
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,26 @@ | ||
import type { Context } from 'hono'; | ||
import profiles from '../data/profiles.json'; | ||
import { StatusCodes } from '../constants/status-codes.ts'; | ||
|
||
export const getAllProfiles = (context: Context) => context.json(profiles, StatusCodes.OKAY); | ||
|
||
export const getProfileById = (context: Context) => { | ||
const id = context.req.param('id'); | ||
const profile = profiles.find((currentProfile) => currentProfile.id === id); | ||
|
||
if (!profile) { | ||
return context.json({ error: 'Profile not found' }, StatusCodes.NOT_FOUND); | ||
export const getProfileById = async (context: Context<EnvironmentBindings>) => { | ||
const userId = context.req.param('id'); | ||
try { | ||
const { results } = await context.env.database | ||
.prepare('SELECT * FROM profile WHERE id = ?') | ||
.bind(userId) | ||
.run(); | ||
return context.json(results); | ||
} catch (e) { | ||
return context.json({ err: e.message }, StatusCodes.NOT_FOUND); | ||
} | ||
}; | ||
|
||
return context.json(profile, StatusCodes.OKAY); | ||
export const getAllProfiles = async (context: Context) => { | ||
try { | ||
const { results } = await context.env.database | ||
.prepare('SELECT * FROM profile') | ||
.all(); | ||
return context.json(results); | ||
} catch (e) { | ||
return context.json({ err: e.message }, StatusCodes.NOT_FOUND); | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
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,14 +1,16 @@ | ||
import { Hono } from 'hono'; | ||
import { getAllProfiles, getProfileById } from '../../controllers/getProfiles'; | ||
import { deleteProfileById } from '../../controllers/deleteProfile'; | ||
import { | ||
getAllProfiles, | ||
getProfileById | ||
} from '../../controllers/getProfiles'; | ||
|
||
const profilesRoutes = new Hono(); | ||
|
||
// Route to get all profiles | ||
profilesRoutes.get('/', getAllProfiles); | ||
|
||
// Route to get a profile by ID | ||
profilesRoutes.get('/:id', getProfileById); | ||
|
||
|
||
profilesRoutes.get('/:id', getProfileById).delete('/:id', deleteProfileById); | ||
|
||
export default profilesRoutes; |
This file was deleted.
Oops, something went wrong.