-
Notifications
You must be signed in to change notification settings - Fork 6
/
customer.go
77 lines (67 loc) · 2.1 KB
/
customer.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
package culqi
import (
"encoding/json"
"net/url"
utils "github.com/culqi/culqi-go/utils/validation"
)
const (
customerURL = baseURL + "/customers"
)
// Create método para crear un cliente
func CreateCustomer(body []byte, encryptionData ...byte) (int, string, error) {
var data map[string]interface{}
err := json.Unmarshal(body, &data)
if err != nil {
return 0, "", err
}
// Perform validation
validator := utils.NewCustomerValidation()
err = validator.Create(data)
if err != nil {
return ErrorBadRequest, "", err
}
statusCode, res, err := Create(customerURL, body, encryptionData...)
return statusCode, res, err
}
// GetByID método para obtener un cliente por id
func GetByIDCustomer(id string, body []byte) (int, string, error) {
err := utils.ValidateStringStart(id, "cus")
if err != nil {
return ErrorBadRequest, "", err
}
statusCode, res, err := GetById(customerURL, id, body)
return statusCode, res, err
}
// GetAll método para obtener la lista de clientes
func GetAllCustomer(queryParams url.Values, body []byte) (int, string, error) {
data := make(map[string]interface{})
for key, values := range queryParams {
if len(values) > 0 {
data[key] = values[0] // Taking only the first value for each key.
}
}
err := utils.CustomerListValidation(data)
if err != nil {
return ErrorBadRequest, "", err
}
statusCode, res, err := GetAll(customerURL, queryParams, body)
return statusCode, res, err
}
// Update método para agregar o remplazar información a los valores de la metadata de un cliente
func UpdateCustomer(id string, body []byte, encryptionData ...byte) (int, string, error) {
err := utils.ValidateStringStart(id, "cus")
if err != nil {
return ErrorBadRequest, "", err
}
statusCode, res, err := Update(customerURL, id, body, encryptionData...)
return statusCode, res, err
}
// Delete método para eliminar un cliente por id
func DeleteCustomer(id string, body []byte) (int, string, error) {
err := utils.ValidateStringStart(id, "cus")
if err != nil {
return ErrorBadRequest, "", err
}
statusCode, res, err := Delete(customerURL, id, body)
return statusCode, res, err
}