Skip to content

Commit f73a063

Browse files
committed
refactoring some methods
1 parent 6ed5c3e commit f73a063

File tree

1 file changed

+26
-62
lines changed

1 file changed

+26
-62
lines changed

internal/powerplatform/services/data_record/api_data_record.go

Lines changed: 26 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ type RelationApiBody struct {
5050
OdataID string `json:"@odata.id"`
5151
}
5252

53+
func getEntityDefinition(ctx context.Context, client *DataRecordClient, environmentUrl string, entityLogicalName string) *EntityDefinitionsDto {
54+
entityDefinitionApiUrl := &url.URL{
55+
Scheme: "https",
56+
Host: strings.TrimPrefix(environmentUrl, "https://"),
57+
Path: fmt.Sprintf("/api/data/v9.2/EntityDefinitions(LogicalName='%s')", entityLogicalName),
58+
Fragment: "$select=PrimaryIdAttribute,LogicalCollectionName",
59+
}
60+
entityDefinition := EntityDefinitionsDto{}
61+
_, err := client.Api.Execute(ctx, "GET", entityDefinitionApiUrl.String(), nil, nil, []int{http.StatusOK}, &entityDefinition)
62+
if err != nil {
63+
return nil
64+
}
65+
66+
return &entityDefinition
67+
}
68+
5369
func (client *DataRecordClient) GetEnvironmentUrlById(ctx context.Context, environmentId string) (string, error) {
5470
env, err := client.getEnvironment(ctx, environmentId)
5571
if err != nil {
@@ -92,19 +108,9 @@ func (client *DataRecordClient) ApplyDataRecord(ctx context.Context, recordId st
92108
if nestedMap, ok := value.(map[string]interface{}); ok {
93109
delete(columns, key)
94110
if len(nestedMap) > 0 {
95-
entityLogicalName := nestedMap["entity_logical_name"]
111+
entityLogicalName := nestedMap["entity_logical_name"].(string)
96112

97-
entityDefinitionApiUrl := &url.URL{
98-
Scheme: "https",
99-
Host: strings.TrimPrefix(environmentUrl, "https://"),
100-
Path: fmt.Sprintf("/api/data/v9.2/EntityDefinitions(LogicalName='%s')", entityLogicalName),
101-
Fragment: "$select=PrimaryIdAttribute,LogicalCollectionName",
102-
}
103-
entityDefinition := EntityDefinitionsDto{}
104-
_, err = client.Api.Execute(ctx, "GET", entityDefinitionApiUrl.String(), nil, nil, []int{http.StatusOK}, &entityDefinition)
105-
if err != nil {
106-
return &result, err
107-
}
113+
entityDefinition := getEntityDefinition(ctx, client, environmentUrl, entityLogicalName)
108114

109115
columns[fmt.Sprintf("%[email protected]", key)] = fmt.Sprintf("%s/api/data/v9.2/%s(%s)", environmentUrl, entityDefinition.LogicalCollectionName, nestedMap["data_record_id"])
110116
}
@@ -115,18 +121,7 @@ func (client *DataRecordClient) ApplyDataRecord(ctx context.Context, recordId st
115121
}
116122
}
117123

118-
apiUrl := &url.URL{
119-
Scheme: "https",
120-
Host: strings.TrimPrefix(environmentUrl, "https://"),
121-
Path: fmt.Sprintf("/api/data/v9.2/EntityDefinitions(LogicalName='%s')", tableName),
122-
Fragment: "$select=PrimaryIdAttribute,LogicalCollectionName",
123-
}
124-
125-
entityDefinition := EntityDefinitionsDto{}
126-
_, err = client.Api.Execute(ctx, "GET", apiUrl.String(), nil, nil, []int{http.StatusOK}, &entityDefinition)
127-
if err != nil {
128-
return &result, err
129-
}
124+
entityDefinition := getEntityDefinition(ctx, client, environmentUrl, tableName)
130125

131126
method := "POST"
132127
path := fmt.Sprintf("/api/data/v9.2/%s", entityDefinition.LogicalCollectionName)
@@ -139,7 +134,7 @@ func (client *DataRecordClient) ApplyDataRecord(ctx context.Context, recordId st
139134
path = fmt.Sprintf("%s(%s)", path, recordId)
140135
}
141136

142-
apiUrl = &url.URL{
137+
apiUrl := &url.URL{
143138
Scheme: "https",
144139
Host: strings.TrimPrefix(environmentUrl, "https://"),
145140
Path: path,
@@ -181,19 +176,9 @@ func (client *DataRecordClient) ApplyDataRecord(ctx context.Context, recordId st
181176
for _, nestedItem := range nestedMapList {
182177
nestedMap := nestedItem.(map[string]interface{})
183178

184-
entityLogicalName := nestedMap["entity_logical_name"]
179+
entityLogicalName := nestedMap["entity_logical_name"].(string)
185180

186-
entityDefinitionApiUrl := &url.URL{
187-
Scheme: "https",
188-
Host: strings.TrimPrefix(environmentUrl, "https://"),
189-
Path: fmt.Sprintf("/api/data/v9.2/EntityDefinitions(LogicalName='%s')", entityLogicalName),
190-
Fragment: "$select=PrimaryIdAttribute,LogicalCollectionName",
191-
}
192-
entityDefinition := EntityDefinitionsDto{}
193-
_, err = client.Api.Execute(ctx, "GET", entityDefinitionApiUrl.String(), nil, nil, []int{http.StatusOK}, &entityDefinition)
194-
if err != nil {
195-
return &result, err
196-
}
181+
entityDefinition := getEntityDefinition(ctx, client, environmentUrl, entityLogicalName)
197182

198183
relation := RelationApiBody{
199184
OdataID: fmt.Sprintf("%s/api/data/v9.2/%s(%s)", environmentUrl, entityDefinition.LogicalCollectionName, nestedMap["data_record_id"]),
@@ -227,20 +212,9 @@ func (client *DataRecordClient) DeleteDataRecord(ctx context.Context, recordId s
227212
}
228213
}
229214

230-
apiUrl := &url.URL{
231-
Scheme: "https",
232-
Host: strings.TrimPrefix(environmentUrl, "https://"),
233-
Path: fmt.Sprintf("/api/data/v9.2/EntityDefinitions(LogicalName='%s')", tableName),
234-
Fragment: "$select=PrimaryIdAttribute,LogicalCollectionName",
235-
}
236-
237-
tableEntityDefinition := EntityDefinitionsDto{}
238-
_, err = client.Api.Execute(ctx, "GET", apiUrl.String(), nil, nil, []int{http.StatusOK}, &tableEntityDefinition)
239-
if err != nil {
240-
return err
241-
}
215+
tableEntityDefinition := getEntityDefinition(ctx, client, environmentUrl, tableName)
242216

243-
apiUrl = &url.URL{
217+
apiUrl := &url.URL{
244218
Scheme: "https",
245219
Host: strings.TrimPrefix(environmentUrl, "https://"),
246220
Path: fmt.Sprintf("/api/data/v9.2/%s(%s)", tableEntityDefinition.LogicalCollectionName, recordId),
@@ -262,19 +236,9 @@ func (client *DataRecordClient) DeleteDataRecord(ctx context.Context, recordId s
262236
for _, nestedItem := range nestedMapList {
263237
nestedMap := nestedItem.(map[string]interface{})
264238

265-
entityLogicalName := nestedMap["entity_logical_name"]
239+
entityLogicalName := nestedMap["entity_logical_name"].(string)
266240

267-
entityDefinitionApiUrl := &url.URL{
268-
Scheme: "https",
269-
Host: strings.TrimPrefix(environmentUrl, "https://"),
270-
Path: fmt.Sprintf("/api/data/v9.2/EntityDefinitions(LogicalName='%s')", entityLogicalName),
271-
Fragment: "$select=PrimaryIdAttribute,LogicalCollectionName",
272-
}
273-
columnEntityDefinition := EntityDefinitionsDto{}
274-
_, err = client.Api.Execute(ctx, "GET", entityDefinitionApiUrl.String(), nil, nil, []int{http.StatusOK}, &columnEntityDefinition)
275-
if err != nil {
276-
return err
277-
}
241+
columnEntityDefinition := getEntityDefinition(ctx, client, environmentUrl, entityLogicalName)
278242

279243
apiUrl = &url.URL{
280244
Scheme: "https",

0 commit comments

Comments
 (0)