Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mytodo
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Martin Schmollinger
mytodo
Commits
e1283220
Commit
e1283220
authored
1 year ago
by
Martin Schmollinger
Browse files
Options
Downloads
Patches
Plain Diff
API Extension: JSON tags and DELETE
parent
72cdba60
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
code/mytodo/data/db.go
+31
-1
31 additions, 1 deletion
code/mytodo/data/db.go
code/mytodo/handler/todo.go
+29
-7
29 additions, 7 deletions
code/mytodo/handler/todo.go
code/mytodo/main.go
+2
-0
2 additions, 0 deletions
code/mytodo/main.go
code/mytodo/model/todo.go
+1
-1
1 addition, 1 deletion
code/mytodo/model/todo.go
with
63 additions
and
9 deletions
code/mytodo/data/db.go
+
31
−
1
View file @
e1283220
package
data
import
"gitlab.reutlingen-university.de/schmolli/todo/model"
import
(
"fmt"
"gitlab.reutlingen-university.de/schmolli/todo/model"
)
var
Todos
=
[]
model
.
Todo
{}
var
NextId
=
0
func
DeleteToDoWithId
(
id
int
)
(
todo
*
model
.
Todo
,
err
error
)
{
delIndex
:=
-
1
for
pos
,
t
:=
range
Todos
{
if
t
.
ID
==
id
{
delIndex
=
pos
break
}
}
if
delIndex
==
-
1
{
return
nil
,
fmt
.
Errorf
(
"todo with ID %d not found"
,
id
)
}
else
{
todo
:=
Todos
[
delIndex
]
deleteIndex
(
delIndex
)
return
&
todo
,
nil
}
}
func
DeleteAll
()
{
//Create empty slice
Todos
=
make
([]
model
.
Todo
,
0
)
}
func
deleteIndex
(
pos
int
)
{
Todos
=
append
(
Todos
[
:
pos
],
Todos
[
pos
+
1
:
]
...
)
}
This diff is collapsed.
Click to expand it.
code/mytodo/handler/todo.go
+
29
−
7
View file @
e1283220
...
...
@@ -13,12 +13,13 @@ import (
func
AddTodo
(
c
*
gin
.
Context
)
{
var
todo
model
.
Todo
if
err
:=
c
.
BindJSON
(
&
todo
);
err
!=
nil
{
c
.
IndentedJSON
(
http
.
StatusBadRequest
,
fmt
.
Sprintf
(
"{
\"
error message
\"
:
\"
%s
\"
}"
,
err
.
Error
()))
c
.
AbortWithError
(
http
.
StatusBadRequest
,
err
)
}
else
{
todo
.
ID
=
data
.
NextId
data
.
NextId
++
data
.
Todos
=
append
(
data
.
Todos
,
todo
)
c
.
IndentedJSON
(
http
.
StatusCreated
,
todo
)
}
todo
.
ID
=
data
.
NextId
data
.
NextId
++
data
.
Todos
=
append
(
data
.
Todos
,
todo
)
c
.
IndentedJSON
(
http
.
StatusCreated
,
todo
)
}
func
GetTodos
(
c
*
gin
.
Context
)
{
...
...
@@ -28,7 +29,7 @@ func GetTodos(c *gin.Context) {
func
GetToDoByID
(
c
*
gin
.
Context
)
{
id
,
err
:=
strconv
.
Atoi
(
c
.
Param
(
"id"
))
if
err
!=
nil
{
c
.
IndentedJSON
(
http
.
StatusBadRequest
,
gin
.
H
{
"message"
:
"illegal id "
}
)
c
.
AbortWithError
(
http
.
StatusBadRequest
,
err
)
return
}
...
...
@@ -38,5 +39,26 @@ func GetToDoByID(c *gin.Context) {
return
}
}
c
.
IndentedJSON
(
http
.
StatusNotFound
,
gin
.
H
{
"message"
:
"Todo not found"
})
c
.
AbortWithError
(
http
.
StatusNotFound
,
fmt
.
Errorf
(
"todo with ID %d not found"
,
id
))
}
func
DeleteToDoByID
(
c
*
gin
.
Context
)
{
id
,
err
:=
strconv
.
Atoi
(
c
.
Param
(
"id"
))
if
err
!=
nil
{
c
.
AbortWithError
(
http
.
StatusBadRequest
,
err
)
return
}
var
todo
*
model
.
Todo
=
nil
if
todo
,
err
=
data
.
DeleteToDoWithId
(
id
);
err
!=
nil
{
c
.
AbortWithError
(
http
.
StatusNotFound
,
err
)
}
c
.
IndentedJSON
(
http
.
StatusOK
,
*
todo
)
}
func
DeleteAllTodos
(
c
*
gin
.
Context
)
{
len
:=
len
(
data
.
Todos
)
data
.
DeleteAll
()
c
.
IndentedJSON
(
http
.
StatusOK
,
fmt
.
Sprintf
(
"Deleted %d todo items"
,
len
))
}
This diff is collapsed.
Click to expand it.
code/mytodo/main.go
+
2
−
0
View file @
e1283220
...
...
@@ -11,5 +11,7 @@ func main() {
r
.
GET
(
"/mytodo/todos"
,
handler
.
GetTodos
)
r
.
GET
(
"/mytodo/todos/:id"
,
handler
.
GetToDoByID
)
r
.
POST
(
"/mytodo/todos"
,
handler
.
AddTodo
)
r
.
DELETE
(
"/mytodo/todos/:id"
,
handler
.
DeleteToDoByID
)
r
.
DELETE
(
"/mytodo/todos/clear"
,
handler
.
DeleteAllTodos
)
r
.
Run
(
":10000"
)
}
This diff is collapsed.
Click to expand it.
code/mytodo/model/todo.go
+
1
−
1
View file @
e1283220
...
...
@@ -2,5 +2,5 @@ package model
type
Todo
struct
{
ID
int
`json:"id"`
Description
string
`json:"description"`
Description
string
`json:"description"
binding:"required"
`
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment