This repository has been archived by the owner on May 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 94
/
bookmark.go
132 lines (118 loc) · 3.56 KB
/
bookmark.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package handler
import (
"encoding/json"
"log"
"net/http"
"github.com/codegangsta/negroni"
"github.com/eminetto/clean-architecture-go/pkg/bookmark"
"github.com/eminetto/clean-architecture-go/pkg/entity"
"github.com/gorilla/mux"
)
func bookmarkIndex(service bookmark.UseCase) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
errorMessage := "Error reading bookmarks"
var data []*entity.Bookmark
var err error
name := r.URL.Query().Get("name")
switch {
case name == "":
data, err = service.FindAll()
default:
data, err = service.Search(name)
}
w.Header().Set("Content-Type", "application/json")
if err != nil && err != entity.ErrNotFound {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(errorMessage))
return
}
if data == nil {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(errorMessage))
return
}
if err := json.NewEncoder(w).Encode(data); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(errorMessage))
}
})
}
func bookmarkAdd(service bookmark.UseCase) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
errorMessage := "Error adding bookmark"
var b *entity.Bookmark
err := json.NewDecoder(r.Body).Decode(&b)
if err != nil {
log.Println(err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(errorMessage))
return
}
b.ID, err = service.Store(b)
if err != nil {
log.Println(err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(errorMessage))
return
}
w.WriteHeader(http.StatusCreated)
if err := json.NewEncoder(w).Encode(b); err != nil {
log.Println(err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(errorMessage))
return
}
})
}
func bookmarkFind(service bookmark.UseCase) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
errorMessage := "Error reading bookmark"
vars := mux.Vars(r)
id := vars["id"]
data, err := service.Find(entity.StringToID(id))
w.Header().Set("Content-Type", "application/json")
if err != nil && err != entity.ErrNotFound {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(errorMessage))
return
}
if data == nil {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(errorMessage))
return
}
if err := json.NewEncoder(w).Encode(data); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(errorMessage))
}
})
}
func bookmarkDelete(service bookmark.UseCase) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
errorMessage := "Error removing bookmark"
vars := mux.Vars(r)
id := vars["id"]
err := service.Delete(entity.StringToID(id))
w.Header().Set("Content-Type", "application/json")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(errorMessage))
return
}
})
}
//MakeBookmarkHandlers make url handlers
func MakeBookmarkHandlers(r *mux.Router, n negroni.Negroni, service bookmark.UseCase) {
r.Handle("/v1/bookmark", n.With(
negroni.Wrap(bookmarkIndex(service)),
)).Methods("GET", "OPTIONS").Name("bookmarkIndex")
r.Handle("/v1/bookmark", n.With(
negroni.Wrap(bookmarkAdd(service)),
)).Methods("POST", "OPTIONS").Name("bookmarkAdd")
r.Handle("/v1/bookmark/{id}", n.With(
negroni.Wrap(bookmarkFind(service)),
)).Methods("GET", "OPTIONS").Name("bookmarkFind")
r.Handle("/v1/bookmark/{id}", n.With(
negroni.Wrap(bookmarkDelete(service)),
)).Methods("DELETE", "OPTIONS").Name("bookmarkDelete")
}