-
Notifications
You must be signed in to change notification settings - Fork 0
/
recordsets_endpoints.go
123 lines (111 loc) · 3.8 KB
/
recordsets_endpoints.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
package endpoints
import (
"context"
"encoding/json"
"fmt"
"github.com/datatug/datatug/packages/api"
"github.com/datatug/datatug/packages/dto"
"github.com/sneat-co/sneat-go-core/apicore"
"github.com/strongo/validation"
"log"
"net/http"
"strconv"
)
func getRecordsetRequestParams(r *http.Request) (params api.RecordsetRequestParams, err error) {
query := r.URL.Query()
if params.Project = query.Get(urlParamProjectID); params.Project == "" {
err = validation.NewErrRequestIsMissingRequiredField(urlParamProjectID)
return
}
if params.Recordset = query.Get(urlParamRecordsetID); params.Recordset == "" {
err = validation.NewErrRequestIsMissingRequiredField(urlParamRecordsetID)
return
}
return
}
func getRecordsetDataParams(r *http.Request) (params api.RecordsetDataRequestParams, err error) {
query := r.URL.Query()
params.RecordsetRequestParams, err = getRecordsetRequestParams(r)
if params.Data = query.Get(urlParamDataID); params.Data == "" {
err = validation.NewErrRequestIsMissingRequiredField(urlParamDataID)
return
}
return
}
// getRecordsetsSummary returns list of dataset definitions
func getRecordsetsSummary(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
ref := newProjectRef(query)
ctx, err := getContextFromRequest(r)
if err != nil {
handleError(err, w, r)
}
datasets, err := api.GetRecordsetsSummary(ctx, ref)
returnJSON(w, r, http.StatusOK, err, datasets)
}
// getRecordsetDefinition returns list of dataset definitions
func getRecordsetDefinition(w http.ResponseWriter, r *http.Request) {
var ref dto.ProjectItemRef
getProjectItem(w, r, &ref, func(ctx context.Context) (responseDTO apicore.ResponseDTO, err error) {
return api.GetDatasetDefinition(ctx, ref)
})
}
// getRecordsetData returns data
func getRecordsetData(w http.ResponseWriter, r *http.Request) {
var ref dto.ProjectItemRef
getProjectItem(w, r, &ref, func(ctx context.Context) (responseDTO apicore.ResponseDTO, err error) {
return api.GetRecordset(ctx, ref)
})
}
// addRowsToRecordset adds rows to a recordset
func addRowsToRecordset(w http.ResponseWriter, r *http.Request) {
var err error
params, err := getRecordsetDataParams(r)
if err != nil {
handleError(err, w, r)
return
}
count, err := strconv.Atoi(r.URL.Query().Get("count"))
if err != nil {
log.Println(fmt.Errorf("WARNING: count parameter is not supplied or invalid: %w", err))
}
rows := make([]api.RowValues, 0, count)
decoder := json.NewDecoder(r.Body)
if err = decoder.Decode(&rows); err != nil {
err = validation.NewErrBadRequestFieldValue("body", err.Error())
handleError(err, w, r)
return
}
numberOfRecords, err := api.AddRowsToRecordset(params, nil)
returnJSON(w, r, http.StatusCreated, err, numberOfRecords)
}
// deleteRowsFromRecordset deletes rows from a recordset
func deleteRowsFromRecordset(w http.ResponseWriter, r *http.Request) {
params, err := getRecordsetDataParams(r)
if err != nil {
handleError(err, w, r)
return
}
count, err := strconv.Atoi(r.URL.Query().Get("count"))
if err != nil {
log.Println(fmt.Errorf("WARNING: count parameter is not supplied or invalid: %w", err))
}
rows := make([]api.RowWithIndex, 0, count)
numberOfRecords, err := api.RemoveRowsFromRecordset(params, rows)
returnJSON(w, r, http.StatusCreated, err, numberOfRecords)
}
// updateRowsInRecordset updates rows in a recordset
func updateRowsInRecordset(w http.ResponseWriter, r *http.Request) {
params, err := getRecordsetDataParams(r)
if err != nil {
handleError(err, w, r)
return
}
count, err := strconv.Atoi(r.URL.Query().Get("count"))
if err != nil {
log.Println(fmt.Errorf("WARNING: count parameter is not supplied or invalid: %w", err))
}
rows := make([]api.RowWithIndexAndNewValues, 0, count)
numberOfRecords, err := api.UpdateRowsInRecordset(params, rows)
returnJSON(w, r, http.StatusCreated, err, numberOfRecords)
}