fix constraint on update
This commit is contained in:
parent
dd752e665d
commit
bc7331ee64
84
src/index.ts
84
src/index.ts
@ -1,7 +1,87 @@
|
|||||||
import { Elysia } from "elysia";
|
import { Elysia, t } from "elysia";
|
||||||
|
|
||||||
|
// dummy data
|
||||||
|
const data = {
|
||||||
|
foods: ["Banana", "Apple", "Watermelon"],
|
||||||
|
};
|
||||||
|
|
||||||
const app = new Elysia().get("/", () => "Hello Elysia").listen(3000);
|
const app = new Elysia().get("/", () => "Hello Elysia").listen(3000);
|
||||||
|
|
||||||
|
app.group("/api", (app) => {
|
||||||
|
return app
|
||||||
|
.post(
|
||||||
|
"/create",
|
||||||
|
({ body: { food } }) => {
|
||||||
|
data.foods.push(food);
|
||||||
|
|
||||||
|
const id = data.foods.length - 1;
|
||||||
|
|
||||||
|
return {
|
||||||
|
status: "success",
|
||||||
|
message: `${food} is added to the list.`,
|
||||||
|
data: {
|
||||||
|
id,
|
||||||
|
food: data.foods[id],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
{
|
||||||
|
body: t.Object({
|
||||||
|
food: t.String(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
.get("/read", () => ({
|
||||||
|
status: "success",
|
||||||
|
message: "Reading foods.",
|
||||||
|
data: data.foods,
|
||||||
|
}))
|
||||||
|
|
||||||
|
.put(
|
||||||
|
"/update",
|
||||||
|
({ body: { food, id } }) => {
|
||||||
|
if (data.foods[id] === undefined) {
|
||||||
|
return {
|
||||||
|
status: "errror",
|
||||||
|
message: `${id} does not exist.`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
data.foods[id] = food;
|
||||||
|
|
||||||
|
return {
|
||||||
|
status: "success",
|
||||||
|
message: `${food} is updated.`,
|
||||||
|
data: {
|
||||||
|
food: data.foods[id],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
{
|
||||||
|
body: t.Object({
|
||||||
|
id: t.Number(),
|
||||||
|
food: t.String(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
.delete(
|
||||||
|
"/delete",
|
||||||
|
({ body: { id } }) => {
|
||||||
|
const deleted = data.foods.splice(id, 1);
|
||||||
|
|
||||||
|
return {
|
||||||
|
status: "success",
|
||||||
|
message: `${deleted[0]} is deleted.`,
|
||||||
|
data: {
|
||||||
|
food: deleted[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
{ body: t.Object({ id: t.Number() }) },
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
|
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`,
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user