-
Notifications
You must be signed in to change notification settings - Fork 0
/
plantController.go
105 lines (89 loc) · 2.66 KB
/
plantController.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
package rest
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"time"
"github.com/go-chi/chi"
"github.com/kdefombelle/go-sample/logger"
"github.com/kdefombelle/go-sample/nursery/plant"
)
// Plant contains the base info for plant to be created
type Plant struct {
ID string `json:"id"`
Name string `json:"name" validate:"required,max=50"`
PlantedDate string `json:"planted_date" validate:"required,datetime=2006-01-02"`
Price string `json:"price" validate:"required,max=20"`
Reserved string `json:"reserved" validate:"required"`
}
// AddPlantRequest the parameter to be passed to add a Plant.
type AddPlantRequest struct {
Plant
}
// AddPlantResponse the response to an addition of a Plant.
type AddPlantResponse struct {
ID string `json:"id"`
}
// PlantController HTTP requests controller.
type PlantController struct {
PlantService *plant.Service
}
const shortForm = "2006-01-02"
// GetPlant HTTP endpoint.
func (pc *PlantController) GetPlant(w http.ResponseWriter, r *http.Request) {
logger.Logger.Info("Getting plant information")
ID := chi.URLParam(r, "id")
p, err := pc.PlantService.GetPlant(ID)
if err != nil {
logger.Logger.Fatalf("Cannot get plant [%v]", err)
internalServer(w, fmt.Errorf("cannot fetch plant id %q", ID))
return
}
if p == nil {
notFound(w, fmt.Errorf("%q not found", ID))
return
}
plant := Plant{
ID: strconv.FormatInt(p.ID, 10),
Name: p.Name,
PlantedDate: p.PlantedDate.Format(shortForm),
Price: strconv.FormatFloat(p.Price, 'f', 2, 64),
Reserved: strconv.FormatBool(p.Reserved),
}
response := plant
json, _ := json.Marshal(response)
_, _ = w.Write(json)
}
// AddPlant HTTP endpoint.
func (pc *PlantController) AddPlant(w http.ResponseWriter, r *http.Request) {
logger.Logger.Infof("Creating new plant")
request := &AddPlantRequest{}
err := validateRequest(r.Body, request)
if err != nil {
logger.Logger.Errorf("Invalid add plant request [%v]", err)
invalidRequest(w, errors.New("invalid request"))
return
}
// insert
plantedDate, _ := time.Parse(shortForm, request.PlantedDate)
price, _ := strconv.ParseFloat(request.Price, 64)
reserved, _ := strconv.ParseBool(request.Reserved)
p := plant.Plant{
Name: request.Name,
PlantedDate: plantedDate,
Price: price,
Reserved: reserved,
}
ID, err := pc.PlantService.CreatePlant(&p)
if err != nil {
logger.Logger.Fatalf("Cannot create plant [%v]", err)
internalServer(w, errors.New("cannot create plant"))
return
}
logger.Logger.Infof("New plant [%s] created", ID)
response := AddPlantResponse{ID: strconv.FormatInt(ID, 10)}
json, _ := json.Marshal(response)
_, _ = w.Write(json)
}