Skip to content

Commit f397390

Browse files
committed
feat: implement more CRUD operations for character management with API endpoints
1 parent 485e420 commit f397390

16 files changed

Lines changed: 380 additions & 52 deletions

File tree

api/bruno/character create.bru

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
meta {
2+
name: character create
3+
type: http
4+
seq: 5
5+
}
6+
7+
post {
8+
url: {{BASE_URL}}/characters
9+
body: json
10+
auth: inherit
11+
}
12+
13+
params:query {
14+
~limit: 10
15+
~skip: 0
16+
}
17+
18+
body:json {
19+
{
20+
"name": "{{NAME}}",
21+
"description": "{{DESCR}}",
22+
"debut": {{DEBUT}}
23+
}
24+
}
25+
26+
vars:pre-request {
27+
NAME: Cristoph Rot
28+
DESCR: lorem ipsum
29+
DEBUT: 1
30+
}

api/bruno/character delete.bru

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
meta {
2+
name: character delete
3+
type: http
4+
seq: 7
5+
}
6+
7+
delete {
8+
url: {{BASE_URL}}/characters/:id
9+
body: none
10+
auth: inherit
11+
}
12+
13+
params:query {
14+
~limit: 10
15+
~skip: 0
16+
}
17+
18+
params:path {
19+
id: f3496428e9e86b9e5df510
20+
}

api/bruno/character update.bru

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
meta {
2+
name: character update
3+
type: http
4+
seq: 6
5+
}
6+
7+
put {
8+
url: {{BASE_URL}}/characters/:id
9+
body: json
10+
auth: inherit
11+
}
12+
13+
params:query {
14+
~limit: 10
15+
~skip: 0
16+
}
17+
18+
params:path {
19+
id: f3496428e9e86b9e5df510
20+
}
21+
22+
23+
body:json {
24+
{
25+
"name": "{{NAME}}",
26+
"description": "{{DESCR}}",
27+
"debut": {{DEBUT}}
28+
}
29+
}
30+
31+
vars:pre-request {
32+
NAME: Cristoph Rot
33+
DESCR: lorem ipsum
34+
DEBUT: 1
35+
}

api/bruno/character.bru

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ meta {
55
}
66

77
get {
8-
url: {{BASE_URL}}/character/qyeufszVydeucS2CRGfKjC
8+
url: {{BASE_URL}}/characters/:id
99
body: none
1010
auth: inherit
1111
}
@@ -15,6 +15,10 @@ params:query {
1515
~skip: 0
1616
}
1717

18+
params:path {
19+
id: 80a7b6a6765d7dffc1b199
20+
}
21+
1822
body:json {
1923
{
2024
"limit": 10,

cmd/api/charactersapi/character.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ func HandleGetCharacter(res http.ResponseWriter, req *http.Request) {
2020
return
2121
}
2222
var char Character = Character{
23-
Id: character.Id,
24-
Name: character.Name,
25-
Description: character.Description,
26-
Debut: character.Debut,
23+
Id: character.Id,
24+
CharacterInfo: CharacterInfo{
25+
Name: character.Name,
26+
Description: character.Description,
27+
Debut: character.Debut,
28+
},
2729
}
2830
core.RespondOk(res, char)
2931
}

cmd/api/charactersapi/characters.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ func HandleGetCharacters(res http.ResponseWriter, req *http.Request) {
2828
// NOTE: eh should i be mapping at all? golang type integrity does mean i dont need to worry so much about leakage
2929
for i, char := range characters {
3030
charactersMapped[i] = Character{
31-
Id: char.Id,
32-
Name: char.Name,
33-
Description: char.Description,
34-
Debut: char.Debut,
31+
Id: char.Id,
32+
CharacterInfo: CharacterInfo{
33+
Name: char.Name,
34+
Description: char.Description,
35+
Debut: char.Debut,
36+
},
3537
}
3638
}
3739
core.RespondOk(
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package charactersapi
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/UltimateForm/gopen-api/internal/core"
7+
"github.com/UltimateForm/gopen-api/internal/repository/charrep"
8+
"github.com/UltimateForm/gopen-api/internal/util"
9+
)
10+
11+
func HandleCreateCharacter(res http.ResponseWriter, req *http.Request) {
12+
var bodyChar CharacterInfo
13+
bodyErr := core.ParseBody(req, &bodyChar)
14+
if bodyErr != nil {
15+
core.RespondError(res, req, bodyErr)
16+
return
17+
}
18+
id, idError := util.RandomHex(11)
19+
if idError != nil {
20+
core.RespondError(res, req, idError)
21+
return
22+
}
23+
createChar, createErr := charrep.WriteOneCharacter(req.Context(), charrep.Character{
24+
Id: id,
25+
Name: bodyChar.Name,
26+
Description: bodyChar.Description,
27+
Debut: bodyChar.Debut,
28+
})
29+
if createErr != nil {
30+
core.RespondError(res, req, createErr)
31+
return
32+
}
33+
core.RespondCreated(res, Character{
34+
Id: createChar.Id,
35+
CharacterInfo: CharacterInfo{
36+
Name: createChar.Name,
37+
Description: createChar.Description,
38+
Debut: createChar.Debut,
39+
},
40+
})
41+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package charactersapi
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/UltimateForm/gopen-api/internal/core"
7+
"github.com/UltimateForm/gopen-api/internal/repository/charrep"
8+
"github.com/go-chi/chi/v5"
9+
)
10+
11+
func HandleDeleteCharacter(res http.ResponseWriter, req *http.Request) {
12+
id := chi.URLParam(req, "id")
13+
if id == "" {
14+
core.RespondKnownError(res, core.BadRequest("id required"))
15+
return
16+
}
17+
deleted, deleteError := charrep.DeleteOneCharacter(req.Context(), id)
18+
if deleteError != nil {
19+
core.RespondError(res, req, deleteError)
20+
return
21+
}
22+
if !deleted {
23+
core.RespondKnownError(res, core.NotFound())
24+
return
25+
}
26+
core.RespondNoContent(res)
27+
}

cmd/api/charactersapi/types.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
package charactersapi
22

3-
type Character struct {
4-
Id string `json:"id"`
3+
import "github.com/UltimateForm/gopen-api/internal/core"
4+
5+
type CharacterInfo struct {
56
Name string `json:"name"`
67
Description string `json:"description"`
78
Debut int `json:"debut"`
89
}
910

11+
func (src CharacterInfo) Validate() error {
12+
if src.Name == "" || src.Description == "" || src.Debut == 0 {
13+
return core.BadRequest("missing one or more parameters: name, description, debut")
14+
}
15+
return nil
16+
}
17+
18+
type Character struct {
19+
Id string `json:"id"`
20+
CharacterInfo
21+
}
22+
1023
type Offset struct {
1124
Limit int `json:"limit"`
1225
Skip int `json:"skip"`
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package charactersapi
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/UltimateForm/gopen-api/internal/core"
7+
"github.com/UltimateForm/gopen-api/internal/repository/charrep"
8+
"github.com/go-chi/chi/v5"
9+
)
10+
11+
func HandleUpdateCharacter(res http.ResponseWriter, req *http.Request) {
12+
id := chi.URLParam(req, "id")
13+
if id == "" {
14+
core.RespondError(res, req, core.BadRequest("id required"))
15+
return
16+
}
17+
var bodyChar CharacterInfo
18+
bodyErr := core.ParseBody(req, &bodyChar)
19+
if bodyErr != nil {
20+
core.RespondError(res, req, bodyErr)
21+
return
22+
}
23+
updatedChar, updateErr := charrep.UpdateOneCharacter(req.Context(), charrep.Character{
24+
Id: id,
25+
Name: bodyChar.Name,
26+
Description: bodyChar.Description,
27+
Debut: bodyChar.Debut,
28+
})
29+
if updateErr != nil {
30+
core.RespondError(res, req, updateErr)
31+
return
32+
}
33+
core.RespondOk(res, Character{
34+
Id: updatedChar.Id,
35+
CharacterInfo: CharacterInfo{
36+
Name: updatedChar.Name,
37+
Description: updatedChar.Description,
38+
Debut: updatedChar.Debut,
39+
},
40+
})
41+
}

0 commit comments

Comments
 (0)