/
joke_controller.go
55 lines (48 loc) · 1.17 KB
/
joke_controller.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
package controller
import (
"net/http"
"strconv"
"github.com/anish-krishnan/Tidepod/tidepod-server/entity"
"github.com/gin-gonic/gin"
)
// GetJokesHandler gets a list of all jokes stored in the database
func GetJokesHandler(c *gin.Context) {
jokes, err := MyStore.GetJokes()
if err == nil {
c.Header("Content-Type", "application/json")
c.JSON(http.StatusOK, jokes)
} else {
panic(err)
}
}
// CreateJokeHandler create a joke
func CreateJokeHandler(c *gin.Context) {
err := MyStore.CreateJoke(c.Param("joke"))
if err == nil {
c.JSON(http.StatusOK, []*entity.Joke{})
} else {
panic(err)
}
}
// DeleteJokeHandler deletes specific joke with provided ID
func DeleteJokeHandler(c *gin.Context) {
if jokeid, err := strconv.Atoi(c.Param("jokeID")); err == nil {
err := MyStore.DeleteJoke(jokeid)
if err == nil {
c.JSON(http.StatusOK, []*entity.Joke{})
} else {
panic(err)
}
}
}
// LikeJoke increments the likes of a particular joke Item
func LikeJoke(c *gin.Context) {
if jokeid, err := strconv.Atoi(c.Param("jokeID")); err == nil {
err := MyStore.LikeJoke(jokeid)
if err == nil {
c.JSON(http.StatusOK, []*entity.Joke{})
} else {
panic(err)
}
}
}