-
Notifications
You must be signed in to change notification settings - Fork 0
/
post-controller.go
115 lines (97 loc) · 3.34 KB
/
post-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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package controller
import (
"encoding/json"
"net/http"
"strconv"
"github.com/go-chi/chi"
"github.com/kuma-coffee/go-crash-course/using-redis-as-a-cache-for-rest-api/cache"
"github.com/kuma-coffee/go-crash-course/using-redis-as-a-cache-for-rest-api/entity"
"github.com/kuma-coffee/go-crash-course/using-redis-as-a-cache-for-rest-api/errors"
"github.com/kuma-coffee/go-crash-course/using-redis-as-a-cache-for-rest-api/service"
)
var (
postService service.PostService
PostCache cache.PostCache
)
type controller struct{}
type PostController interface {
GetPosts(w http.ResponseWriter, r *http.Request)
GetPostByID(w http.ResponseWriter, r *http.Request)
AddPost(w http.ResponseWriter, r *http.Request)
DeletePost(w http.ResponseWriter, r *http.Request)
}
func NewPostController(service service.PostService, cache cache.PostCache) PostController {
postService = service
PostCache = cache
return &controller{}
}
func (*controller) GetPosts(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-type", "application/json")
posts, err := postService.FindAll()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(errors.ServiceError{Message: "Error getting the posts"})
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(posts)
}
func (*controller) GetPostByID(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-type", "application/json")
ID, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(errors.ServiceError{Message: err.Error()})
}
// add cache using redis
stringID := strconv.Itoa(ID)
var post *entity.Post = PostCache.Get(stringID)
if post == nil {
posts, err := postService.FindByID(ID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(errors.ServiceError{Message: err.Error()})
return
}
PostCache.Set(stringID, posts)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(posts)
} else {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(post)
}
}
func (*controller) AddPost(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-type", "application/json")
var post entity.Post
err := json.NewDecoder(r.Body).Decode(&post)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(errors.ServiceError{Message: "Error unmarshalling the request"})
}
err = postService.Validate(&post)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(errors.ServiceError{Message: err.Error()})
}
result, err := postService.Create(&post)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(errors.ServiceError{Message: "Error saving the post"})
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(result)
}
func (*controller) DeletePost(w http.ResponseWriter, r *http.Request) {
ID, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(errors.ServiceError{Message: err.Error()})
}
err = postService.Delete(ID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(errors.ServiceError{Message: err.Error()})
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode("success delete post")
}