forked from LS6-Events/astra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
62 lines (48 loc) · 1.08 KB
/
handlers.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
package petstore
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/Mrzrb/astra/tests/petstore"
)
func getAllPets(c *gin.Context) {
allPets := petstore.Pets
c.JSON(http.StatusOK, allPets)
}
func getPetByID(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
pet, err := petstore.PetByID(int64(id))
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, pet)
}
func createPet(c *gin.Context) {
var pet petstore.PetDTO
err := c.BindJSON(&pet)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
petstore.AddPet(petstore.Pet{
Name: pet.Name,
PhotoURLs: pet.PhotoURLs,
Status: pet.Status,
Tags: pet.Tags,
})
c.JSON(http.StatusOK, pet)
}
func deletePet(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
petstore.RemovePet(int64(id))
c.Status(http.StatusOK)
}