forked from r3-team/r3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Got base API schema & handler running with simple GET requests. Still open: * Builder UIs * Most HTTP methods (POST, PATCH, PUT, DELETE)
- Loading branch information
1 parent
eba7cb4
commit a4d3fd5
Showing
23 changed files
with
827 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package data_query | ||
|
||
import ( | ||
"r3/types" | ||
"time" | ||
|
||
"github.com/jackc/pgx/v5/pgtype" | ||
) | ||
|
||
func ConvertSubQueryToDataGet(query types.Query, queryAggregator pgtype.Text, | ||
attributeId pgtype.UUID, attributeIndex int, loginId int64, languageCode string) types.DataGet { | ||
|
||
return types.DataGet{ | ||
RelationId: query.RelationId.Bytes, | ||
Joins: ConvertQueryToDataJoins(query.Joins), | ||
Expressions: []types.DataGetExpression{ | ||
types.DataGetExpression{ | ||
Aggregator: queryAggregator, | ||
AttributeId: attributeId, | ||
AttributeIdNm: pgtype.UUID{}, | ||
Index: attributeIndex, | ||
}, | ||
}, | ||
Filters: ConvertQueryToDataFilter(query.Filters, loginId, languageCode), | ||
Orders: ConvertQueryToDataOrders(query.Orders), | ||
Limit: query.FixedLimit, | ||
} | ||
} | ||
|
||
func ConvertQueryToDataFilter(filters []types.QueryFilter, | ||
loginId int64, languageCode string) []types.DataGetFilter { | ||
|
||
filtersOut := make([]types.DataGetFilter, len(filters)) | ||
|
||
var processSide = func(side types.QueryFilterSide) types.DataGetFilterSide { | ||
sideOut := types.DataGetFilterSide{ | ||
AttributeId: side.AttributeId, | ||
AttributeIndex: side.AttributeIndex, | ||
AttributeNested: side.AttributeNested, | ||
Brackets: side.Brackets, | ||
Query: types.DataGet{}, | ||
QueryAggregator: side.QueryAggregator, | ||
Value: side.Value, | ||
} | ||
switch side.Content { | ||
// data | ||
case "subQuery": | ||
sideOut.Query = ConvertSubQueryToDataGet(side.Query, side.QueryAggregator, | ||
side.AttributeId, side.AttributeIndex, loginId, languageCode) | ||
case "true": | ||
sideOut.Value = true | ||
|
||
// date/time | ||
case "nowDate": | ||
t := time.Now().UTC() | ||
sideOut.Value = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, | ||
t.Location()).UTC().Unix() + int64(side.NowOffset.Int32) | ||
case "nowDatetime": | ||
sideOut.Value = time.Now().UTC().Unix() + int64(side.NowOffset.Int32) | ||
case "nowTime": | ||
t := time.Now().UTC() | ||
sideOut.Value = time.Date(1970, 1, 1, t.Hour(), t.Minute(), t.Second(), 0, | ||
t.Location()).UTC().Unix() + int64(side.NowOffset.Int32) | ||
|
||
// user | ||
case "languageCode": | ||
sideOut.Value = languageCode | ||
case "login": | ||
sideOut.Value = loginId | ||
} | ||
return sideOut | ||
} | ||
|
||
for i, filter := range filters { | ||
|
||
filterOut := types.DataGetFilter{ | ||
Connector: filter.Connector, | ||
Operator: filter.Operator, | ||
Side0: processSide(filter.Side0), | ||
Side1: processSide(filter.Side1), | ||
} | ||
if i == 0 { | ||
filterOut.Side0.Brackets++ | ||
} | ||
if i == len(filters)-1 { | ||
filterOut.Side1.Brackets++ | ||
} | ||
filtersOut[i] = filterOut | ||
} | ||
return filtersOut | ||
} | ||
|
||
func ConvertQueryToDataJoins(joins []types.QueryJoin) []types.DataGetJoin { | ||
joinsOut := make([]types.DataGetJoin, 0) | ||
for _, join := range joins { | ||
joinsOut = append(joinsOut, types.DataGetJoin{ | ||
AttributeId: join.AttributeId.Bytes, | ||
Connector: join.Connector, | ||
Index: join.Index, | ||
IndexFrom: join.IndexFrom, | ||
}) | ||
} | ||
return joinsOut | ||
} | ||
|
||
func ConvertQueryToDataOrders(orders []types.QueryOrder) []types.DataGetOrder { | ||
ordersOut := make([]types.DataGetOrder, 0) | ||
for _, order := range orders { | ||
ordersOut = append(ordersOut, types.DataGetOrder{ | ||
AttributeId: pgtype.UUID{Bytes: order.AttributeId, Valid: true}, | ||
Index: pgtype.Int4{Int32: int32(order.Index), Valid: true}, | ||
Ascending: order.Ascending, | ||
}) | ||
} | ||
return ordersOut | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.