Skip to content

Commit 37b778e

Browse files
committed
feat: additional validation on createBook
1 parent d15245f commit 37b778e

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

api/api.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"net/http"
5+
"strings"
56

67
"errors"
78

@@ -38,7 +39,7 @@ func getBookById(id string) (*book, error) {
3839
return &books[i], nil
3940
}
4041
}
41-
return nil, errors.New("book now found")
42+
return nil, errors.New("book not found")
4243
}
4344

4445
// bookById returns a book with an id
@@ -59,6 +60,12 @@ func createBook(c *gin.Context) {
5960
var newBook book
6061

6162
if err := c.BindJSON(&newBook); err != nil {
63+
c.IndentedJSON(http.StatusBadRequest, gin.H{ "error": err })
64+
return
65+
}
66+
67+
if strings.Count(newBook.ID, "") == 1 {
68+
c.IndentedJSON(http.StatusBadRequest, gin.H{ "error": "id cannot be empty" })
6269
return
6370
}
6471

0 commit comments

Comments
 (0)