Skip to content

Commit

Permalink
refactor: post put patch and delete method
Browse files Browse the repository at this point in the history
  • Loading branch information
ditu10 committed Apr 4, 2024
1 parent 7f4eb52 commit 0dc96ba
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 44 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
node_modules
# ide
.idea
.vscode
6 changes: 5 additions & 1 deletion packages/backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ yarn-error.log*
**/*.tgz
**/*.log
package-lock.json
**/*.bun
**/*.bun

# ide
.idea
.vscode
107 changes: 64 additions & 43 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ const ID = TODOS.length + 1

const app = new Elysia()
.state('id', ID)
.model({
paramCheck: t.Object({
id: t.Numeric()
})
})
.get('/todos', () => TODOS)
.get(
'/todos/:id',
Expand All @@ -29,92 +34,108 @@ const app = new Elysia()
return todo
},
{
params: t.Object({
id: t.Numeric()
})
}
)
.delete(
'/todos/:id',
({ params, error }) => {
let hasTodo = false
const newTodos = TODOS.filter((todo) => {
if (todo.id === params.id) {
hasTodo = true
} else {
return todo
}
})
if (!hasTodo) {
return error(404, 'Todo not found')
}
TODOS = [...newTodos]
},
{
params: t.Object({
id: t.Numeric()
})
params: 'paramCheck'
}
)
.post(
'/todos',
({ body, store }) => {
const todo = { ...body, id: store.id++, starred: false, completed: false }
const todo = {
id: store.id++,
starred: false,
completed: false,
desc: ''
}
Object.assign(todo, body)
TODOS.push(todo)
return todo
},
{
body: t.Object({
desc: t.String()
desc: t.String({
minLength: 1,
error: 'description cannot be empty'
}),
starred: t.Optional(t.Boolean()),
completed: t.Optional(t.Boolean())
})
}
)
.put(
'/todos/:id',
({ body, params }) => {
({ body, params, error }) => {
let hasTodo = false
TODOS.forEach((todo, index) => {
if (todo.id === params.id) {
hasTodo = true
TODOS[index] = { id: params.id, ...body }
}
})
if (!hasTodo) {
return error(204)
}
return body
},
{
params: t.Object({
id: t.Numeric()
}),
params: 'paramCheck',
body: t.Object({
desc: t.String(),
desc: t.String({
minLength: 1,
error: 'description cannot be empty'
}),
starred: t.Boolean(),
completed: t.Boolean()
})
}
)
.patch(
'/todos/:id',
({ body, params }) => {
TODOS.forEach((todo, index) => {
({ body, params, error }) => {
let hasTodo = false
TODOS.forEach((todo) => {
if (todo.id === params.id) {
if (body.completed !== undefined) {
TODOS[index].completed = body.completed
}
if (body.starred !== undefined) {
TODOS[index].starred = body.starred
}
hasTodo = true
Object.assign(todo, body)
}
})
if (!hasTodo) {
return error(204)
}
return body
},
{
params: t.Object({
id: t.Numeric()
}),
params: 'paramCheck',
body: t.Object({
desc: t.Optional(
t.String({
minLength: 1,
error: 'description cannot be empty'
})
),
completed: t.Optional(t.Boolean()),
starred: t.Optional(t.Boolean())
})
}
)
.delete(
'/todos/:id',
({ params, error }) => {
let hasTodo = false
TODOS = TODOS.filter((todo) => {
if (todo.id === params.id) {
hasTodo = true
} else {
return true
}
})
if (!hasTodo) {
return error(204)
}
},
{
params: 'paramCheck'
}
)
.listen(3000)

console.log(
Expand Down

0 comments on commit 0dc96ba

Please sign in to comment.