Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
nicobytes committed Aug 19, 2024
1 parent 0fbffb8 commit 654bde1
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 1 deletion.
1 change: 1 addition & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"devDependencies": {
"@cloudflare/workers-types": "4.20240320.1",
"@faker-js/faker": "^8.4.1",
"@types/bcryptjs": "2.4.6",
"@types/better-sqlite3": "7.6.9",
"drizzle-kit": "0.20.14",
Expand Down
23 changes: 23 additions & 0 deletions apps/api/src/dtos/location.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { z } from '@hono/zod-openapi';

export const LocationSchema = z
.object({
id: z.number().openapi({
example: 1,
}),
name: z.string().openapi({
example: 'John Doe',
}),
description: z.string().openapi({
example: 'Description',
}),
latitude: z.number().openapi({
example: 1.234,
}),
longitude: z.number().openapi({
example: 1.234,
}),
})
.openapi('Location');

export const LocationsSchema = z.array(LocationSchema);
4 changes: 3 additions & 1 deletion apps/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import getUsers from '@src/routes/users/getUsers';
import getUser from '@src/routes/users/getUser';
import updateUser from '@src/routes/users/updateUser';
import deleteUser from '@src/routes/users/deleteUser';
import getLocations from '@src/routes/locations/getLocations';
import seed from '@src/routes/seed/seed';

import login from '@src/routes/auth/login';
Expand Down Expand Up @@ -50,6 +51,8 @@ app.route('/api/v1/users', getUser);
app.route('/api/v1/users', createUser);
app.route('/api/v1/seed', seed);

app.route('/api/v1/locations', getLocations);

// Private paths

app.use('/api/v1/*', jwtMiddleware);
Expand All @@ -64,7 +67,6 @@ app.route('/api/v1/products', createProduct);
app.route('/api/v1/products', updateProduct);
app.route('/api/v1/products', deleteProduct);


app.route('/api/v1/users', updateUser);
app.route('/api/v1/users', deleteUser);

Expand Down
29 changes: 29 additions & 0 deletions apps/api/src/routes/locations/getLocations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { OpenAPIHono, createRoute } from "@hono/zod-openapi";
import { LocationsSchema } from '@src/dtos/location.dto';
import { getAllLocations } from '@src/services/location.service';
import { App } from "@src/types";

const app = new OpenAPIHono<App>();

const route = createRoute({
tags: ['location'],
method: 'get',
path: '/',
responses: {
200: {
content: {
'application/json': {
schema: LocationsSchema,
},
},
description: 'Retrieve all locations',
},
},
});

app.openapi(route, (c) => {
const results = getAllLocations();
return c.json(results);
});

export default app;
21 changes: 21 additions & 0 deletions apps/api/src/services/location.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { faker } from '@faker-js/faker';

function generateLocation() {
const [latitude, longitude] = faker.location.nearbyGPSCoordinate({
origin: [4.60971, -74.08175],
});

return {
id: faker.number.int(),
name: faker.location.streetAddress(),
description: faker.lorem.sentence(),
latitude,
longitude,
}
}

export const getAllLocations = () => {
return faker.helpers.multiple(generateLocation, {
count: 5,
});
}

0 comments on commit 654bde1

Please sign in to comment.