-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
72 lines (57 loc) · 1.55 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
)
type cat struct {
Id int `json:"id"`
Birthday string `json:"birthday"`
Name string `json:"name"`
Breed string `json:"breed"`
IsHungry bool `json:"isHungry"`
}
var cats = []cat{
{Id: 1, Birthday: "12/17", Name: "Alex", Breed: "Maine Coon", IsHungry: false},
{Id: 2, Birthday: "4/26", Name: "Andrew", Breed: "Siamese", IsHungry: true},
{Id: 3, Birthday: "5/23", Name: "Laurie", Breed: "Scottish Fold", IsHungry: false},
}
func getAllCats(c *gin.Context) {
c.IndentedJSON(http.StatusOK, cats)
}
func getCatById(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "invalid input for param \"id\""})
return
}
for _, a := range cats {
if a.Id == id {
c.IndentedJSON(http.StatusOK, a)
return
}
}
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "cat not found"})
}
func postCats(c *gin.Context) {
var newCat cat
if err := c.BindJSON(&newCat); err != nil {
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "invalid input for body \"cat\""})
return
}
for _, a := range cats {
if a.Name == newCat.Name {
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "a cat with that name already exists"})
return
}
}
cats = append(cats, newCat)
c.IndentedJSON(http.StatusCreated, newCat)
}
func main() {
router := gin.Default()
router.GET("/cats", getAllCats)
router.GET("/cat/:id", getCatById)
router.POST("/cats", postCats)
router.Run("localhost:3000")
}