Skip to content

Commit

Permalink
Change id field to be a UUIDv4 string
Browse files Browse the repository at this point in the history
  • Loading branch information
simonteaching authored and simonplend committed Aug 16, 2022
1 parent 9343281 commit 606589f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
5 changes: 3 additions & 2 deletions migrations/20220710124758_init.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const up = async (knex) => {
return knex.schema.createTable("recipes", function (table) {
table.increments("id").primary();
return knex.schema.createTable("recipes", function (table) {
table.uuid('id').primary().notNullable();
// .defaultTo(knex.raw('gen_random_uuid()'))
table.text("name");
table.text("ingredients");
table.integer("time");
Expand Down
13 changes: 11 additions & 2 deletions src/routes/recipes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { randomUUID } from "node:crypto";
import S from "fluent-json-schema";

const paramsSchema = S.object().prop("id", S.number().required());
const paramsSchema = S.object().prop(
"id",
S.string().format("uuid").required()
);

const filterSchema = S.object().prop(
"filter",
Expand Down Expand Up @@ -68,9 +72,14 @@ export default async function recipesRoutes(app) {
{ schema: { body: recipeSchema } },
async function (request, reply) {
try {
const recipe = {
id: randomUUID(),
...request.body,
};

const recipes = await app
.knex("recipes")
.insert(request.body, ["id", "name", "ingredients", "time", "steps"]);
.insert(recipe, ["id", "name", "ingredients", "time", "steps"]);

reply.status(201).send(recipes[0]);
} catch (error) {
Expand Down
26 changes: 13 additions & 13 deletions test.http
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@ GET http://localhost:3000/recipes?filter=beef

###

GET http://localhost:3000/recipes/3
GET http://localhost:3000/recipes/1a086ba5-c779-4b0c-baa0-48b785ae600

###

POST http://localhost:3000/recipes
Content-Type: application/json

# {
# "name": "Caesar salad",
# "ingredients": "lettuce, croutons",
# "time": 52,
# "steps": "Put everything in a bowl"
# }

{
"name": "Beef brisket",
"ingredients": "beef, onion, garlic",
"name": "Caesar salad",
"ingredients": "lettuce, croutons",
"time": 52,
"steps": "Cook the meat"
"steps": "Put everything in a bowl"
}

# {
# "name": "Beef brisket",
# "ingredients": "beef, onion, garlic",
# "time": 52,
# "steps": "Cook the meat"
# }

###

PUT http://localhost:3000/recipes/3
PUT http://localhost:3000/recipes/5676a35f-8155-4f37-911e-977acc9ade5a
Content-Type: application/json

{
Expand All @@ -41,4 +41,4 @@ Content-Type: application/json

###

DELETE http://localhost:3000/recipes/5
DELETE http://localhost:3000/recipes/5676a35f-8155-4f37-911e-977acc9ade5a

0 comments on commit 606589f

Please sign in to comment.