-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
145 lines (137 loc) · 4.66 KB
/
handler.go
File metadata and controls
145 lines (137 loc) · 4.66 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Package handler provides with handler functions for handling
// the various HTTP Requests
package main
import (
"encoding/json"
"net/http"
"strconv"
"time"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"github.com/springwiz/articlemaster/model"
"github.com/springwiz/articlemaster/repository"
)
// GET /articles/{id}
// implements and returns the GET Article Handler function
func GetArticleByIdHandler() func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
log.Println("Article Id: ", vars["id"])
var err1 error
id, err1 := strconv.ParseUint(vars["id"], 10, 64)
if err1 != nil {
errRes, _ := json.Marshal(model.NewException("PE00001", "Parse Error"))
log.Printf("Error while parsing %s", err1)
w.WriteHeader(500)
json.NewEncoder(w).Encode(string(errRes))
return
}
article, err1 := repository.GetInstance().GetArticle(id)
if err1 != nil {
errRes, _ := json.Marshal(model.NewException("PE00002", err1.Error()))
log.Printf("Error while parsing %s", err1)
w.WriteHeader(404)
json.NewEncoder(w).Encode(string(errRes))
return
}
articleBytes, err := json.Marshal(article)
if err != nil {
errRes, _ := json.Marshal(model.NewException("PE00001", "Parse Error"))
log.Printf("Error while parsing %s", err1)
w.WriteHeader(500)
json.NewEncoder(w).Encode(string(errRes))
return
}
log.Println("Response published: ", string(articleBytes))
w.WriteHeader(200)
json.NewEncoder(w).Encode(string(articleBytes))
}
}
// GET /tags/{tagName}/{date}
// implements and returns the GET Tags Handler function
func GetArticlesByTagDateHandler() func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
log.Println("Tag Name: ", vars["tagName"])
log.Println("Tag Date: ", vars["date"])
tag, err1 := repository.GetInstance().GetArticlesByTagDate(vars["tagName"], vars["date"])
if err1 != nil {
errRes, _ := json.Marshal(model.NewException("PE00002", err1.Error()))
log.Printf("Error while parsing %s", err1)
w.WriteHeader(500)
json.NewEncoder(w).Encode(string(errRes))
return
}
tagBytes, err := json.Marshal(tag)
if err != nil {
errRes, _ := json.Marshal(model.NewException("PE00001", "Parse Error"))
log.Printf("Error while parsing %s", err1)
w.WriteHeader(500)
json.NewEncoder(w).Encode(string(errRes))
return
}
log.Println("Response published: ", string(tagBytes))
json.NewEncoder(w).Encode(string(tagBytes))
}
}
// POST /articles
// implements and returns the POST Article Handler function
func PostArticleHandler() func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
article := model.NewBlankArticle()
err1 := json.NewDecoder(r.Body).Decode(&article)
if err1 != nil {
errRes, _ := json.Marshal(model.NewException("PE00002", err1.Error()))
log.Printf("Error while parsing %s", err1)
w.WriteHeader(500)
json.NewEncoder(w).Encode(string(errRes))
return
}
article.Date, _ = time.Parse("yyyy-MM-dd", article.DateString)
err := repository.GetInstance().SaveArticle(article)
if err != nil {
errRes, _ := json.Marshal(model.NewException("PE00002", err.Error()))
log.Printf("Error while parsing %s", err)
w.WriteHeader(500)
json.NewEncoder(w).Encode(string(errRes))
return
}
w.WriteHeader(201)
json.NewEncoder(w).Encode(model.NewException("", "Success"))
}
}
// PUT /articles
// implements and returns the PUT Article Handler function
func PutArticleHandler() func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
log.Println("Article Id: ", vars["id"])
article := &model.Article{}
err1 := json.NewDecoder(r.Body).Decode(article)
if err1 != nil {
errRes, _ := json.Marshal(model.NewException("PE00002", err1.Error()))
log.Printf("Error while parsing %s", err1)
w.WriteHeader(500)
json.NewEncoder(w).Encode(string(errRes))
return
}
article, err1 = model.NewArticle(vars["id"], article.Title, article.Body, article.Tags, article.DateString)
if err1 != nil {
errRes, _ := json.Marshal(model.NewException("PE00002", err1.Error()))
log.Printf("Error while parsing %s", err1)
w.WriteHeader(500)
json.NewEncoder(w).Encode(string(errRes))
return
}
err2 := repository.GetInstance().SaveArticle(article)
if err2 != nil {
errRes, _ := json.Marshal(model.NewException("PE00002", err2.Error()))
log.Printf("Error while parsing %s", err2)
w.WriteHeader(500)
json.NewEncoder(w).Encode(string(errRes))
return
}
w.WriteHeader(200)
json.NewEncoder(w).Encode(model.NewException("", "Success"))
}
}