-
Notifications
You must be signed in to change notification settings - Fork 402
/
coupon.go
149 lines (128 loc) · 3.52 KB
/
coupon.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package admin
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"github.com/gorilla/mux"
"storj.io/common/uuid"
"storj.io/storj/satellite/payments"
)
func (server *Server) addCoupon(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
httpJSONError(w, "failed to read body",
err.Error(), http.StatusInternalServerError)
return
}
var input struct {
UserID uuid.UUID `json:"userId"`
Duration int `json:"duration"`
Amount int64 `json:"amount"`
Description string `json:"description"`
}
err = json.Unmarshal(body, &input)
if err != nil {
httpJSONError(w, "failed to unmarshal request",
err.Error(), http.StatusBadRequest)
return
}
switch {
case input.Duration == 0:
httpJSONError(w, "Duration is not set",
"", http.StatusBadRequest)
return
case input.Amount == 0:
httpJSONError(w, "Amount is not set",
"", http.StatusBadRequest)
return
case input.Description == "":
httpJSONError(w, "Description is not set",
"", http.StatusBadRequest)
return
case input.UserID.IsZero():
httpJSONError(w, "UserID is not set",
"", http.StatusBadRequest)
return
}
coupon, err := server.db.StripeCoinPayments().Coupons().Insert(ctx, payments.Coupon{
UserID: input.UserID,
Amount: input.Amount,
Duration: input.Duration,
Description: input.Description,
})
if err != nil {
httpJSONError(w, "failed to insert coupon",
err.Error(), http.StatusInternalServerError)
return
}
data, err := json.Marshal(coupon.ID)
if err != nil {
httpJSONError(w, "json encoding failed",
err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(data) // nothing to do with the error response, probably the client requesting disappeared
}
func (server *Server) couponInfo(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r)
id, ok := vars["couponid"]
if !ok {
httpJSONError(w, "couponId missing",
"", http.StatusBadRequest)
return
}
couponID, err := uuid.FromString(id)
if err != nil {
httpJSONError(w, "invalid couponId",
"", http.StatusBadRequest)
}
coupon, err := server.db.StripeCoinPayments().Coupons().Get(ctx, couponID)
if errors.Is(err, sql.ErrNoRows) {
httpJSONError(w, fmt.Sprintf("coupon with id %q not found", couponID),
"", http.StatusNotFound)
return
}
if err != nil {
httpJSONError(w, "failed to get coupon",
err.Error(), http.StatusInternalServerError)
return
}
data, err := json.Marshal(coupon)
if err != nil {
httpJSONError(w, "json encoding failed",
err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(data) // nothing to do with the error response, probably the client requesting disappeared
}
func (server *Server) deleteCoupon(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r)
UUIDString, ok := vars["couponid"]
if !ok {
httpJSONError(w, "couponid missing",
"", http.StatusBadRequest)
return
}
couponID, err := uuid.FromString(UUIDString)
if err != nil {
httpJSONError(w, "invalid couponid",
err.Error(), http.StatusBadRequest)
return
}
err = server.db.StripeCoinPayments().Coupons().Delete(ctx, couponID)
if err != nil {
httpJSONError(w, "unable to delete coupon",
err.Error(), http.StatusInternalServerError)
return
}
}