Skip to content

Commit

Permalink
feat: update API to fetch data from actual DB instead of mock data
Browse files Browse the repository at this point in the history
Co-authored-by: Evan Suhyeong Lee <[email protected]>
  • Loading branch information
pmjuu and sounmind committed Jan 10, 2025
1 parent cc61955 commit 686df16
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Context } from 'hono';
import { StatusCodes } from '../../constants/status-codes.ts';
import { StatusCodes } from '../constants/status-codes.ts';

export const deleteProfileById = async (
context: Context<EnvironmentBindings>
Expand Down
30 changes: 20 additions & 10 deletions src/controllers/getProfiles.ts
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);
}
};
26 changes: 0 additions & 26 deletions src/controllers/sqlControllers/getProfiles.ts

This file was deleted.

9 changes: 2 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ import { Hono } from 'hono';
import { cors } from 'hono/cors';
import profilesRoute from './routes/profiles/profiles';
import teamsRoutes from './routes/teams/teams';
import profilesSqlRoute from './routes/profiles/profilesSql';
import teamsSqlRoute from './routes/teams/teamsSql';



const app = new Hono();

app.get('/', (context) => context.text('Welome to volunteer management system!'));
Expand All @@ -23,12 +20,10 @@ app.use(
})
);

// Existing routes
app.route('/profiles', profilesRoute);
app.route('/teams', teamsRoutes);

app.route('/sql/profiles', profilesSqlRoute);
// TODO: should be changed to interact with real DB
app.route('/sql/teams', teamsSqlRoute);

app.route('/teams', teamsRoutes);

export default app;
10 changes: 6 additions & 4 deletions src/routes/profiles/profiles.ts
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;
16 changes: 0 additions & 16 deletions src/routes/profiles/profilesSql.ts

This file was deleted.

0 comments on commit 686df16

Please sign in to comment.