-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry_handler.go
58 lines (49 loc) · 1.66 KB
/
entry_handler.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
package handlers
import (
"net/http"
"github.com/Khigashiguchi/khigashiguchi.com/api/domain/entity"
"github.com/Khigashiguchi/khigashiguchi.com/api/infrastructure/repository"
"github.com/Khigashiguchi/khigashiguchi.com/api/interfaces/presenter"
"github.com/Khigashiguchi/khigashiguchi.com/api/usecase"
"github.com/gin-gonic/gin/json"
)
// Handler is interface of handling request.
type Handler interface {
Handler(w http.ResponseWriter, r *http.Request)
}
type getEntriesHandler struct {
UseCase usecase.GetEntriesUseCase
}
// GetEntriesHandler handle request GET /entries.
func (h *getEntriesHandler) Handler(w http.ResponseWriter, r *http.Request) {
entries, err := h.UseCase.Run()
if err != nil {
presenter.RespondErrorJson(w, "Internal Server Error", http.StatusInternalServerError)
return
}
res := presenter.GetEntriesResponse{
Entities: entries,
}
presenter.RespondJson(w, res, http.StatusOK)
}
// NewGetEntriesHandler create new handler of getting entries.
func NewGetEntriesHandler(db repository.DB) Handler {
return &getEntriesHandler{UseCase: usecase.NewGetEntriesUseCase(db)}
}
type postEntriesHandler struct {
UseCase usecase.PostEntriesUseCase
}
func (h *postEntriesHandler) Handler(w http.ResponseWriter, r *http.Request) {
entry := entity.Entry{}
if err := json.NewDecoder(r.Body).Decode(&entry); err != nil {
presenter.RespondErrorJson(w, "invalid parameter", http.StatusBadRequest)
return
}
if err := h.UseCase.Run(entry); err != nil {
presenter.RespondJson(w, err.Error(), http.StatusBadRequest)
return
}
}
func NewPostEntriesHandler(db repository.Beginner) Handler {
return &postEntriesHandler{UseCase: usecase.NewPostEntriesUseCase(db)}
}