Skip to content

Commit

Permalink
Added Query object creation (POST)
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbcd committed Mar 30, 2021
1 parent 5e7fb9c commit 06de5d4
Show file tree
Hide file tree
Showing 6 changed files with 380 additions and 22 deletions.
292 changes: 292 additions & 0 deletions mocks/post/post-query.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,292 @@
{
"_type": "Query",
"starred": false,
"id": 39,
"name": "My filter-query",
"createdAt": "2021-03-30T16:55:47Z",
"updatedAt": "2021-03-30T16:55:47Z",
"filters": [
{
"_type": "StatusQueryFilter",
"name": "Status",
"_links": {
"schema": {
"href": "/api/v3/queries/filter_instance_schemas/status"
},
"filter": {
"href": "/api/v3/queries/filters/status",
"title": "Status"
},
"operator": {
"href": "/api/v3/queries/operators/o",
"title": "open"
},
"values": []
}
},
{
"_type": "AssigneeQueryFilter",
"name": "Assignee",
"_links": {
"schema": {
"href": "/api/v3/queries/filter_instance_schemas/assignee"
},
"filter": {
"href": "/api/v3/queries/filters/assignee",
"title": "Assignee"
},
"operator": {
"href": "/api/v3/queries/operators/=",
"title": "is"
},
"values": [
{
"href": "/api/v3/users/me",
"title": "me"
}
]
}
}
],
"sums": false,
"public": false,
"hidden": false,
"timelineVisible": false,
"showHierarchies": true,
"timelineZoomLevel": "auto",
"timelineLabels": {},
"highlightingMode": "inline",
"_embedded": {
"project": {
"_type": "Project",
"id": 1,
"identifier": "demo-project",
"name": "Demo project",
"active": true,
"public": true,
"description": {
"format": "markdown",
"raw": "This is a short summary of the goals of this demo project.",
"html": "<p class=\"op-uc-p\">This is a short summary of the goals of this demo project.</p>"
},
"createdAt": "2021-02-18T10:10:47Z",
"updatedAt": "2021-02-18T10:10:47Z",
"status": "on track",
"statusExplanation": {
"format": "markdown",
"raw": "All tasks are on schedule. The people involved know their tasks. The system is completely set up.",
"html": "<p class=\"op-uc-p\">All tasks are on schedule. The people involved know their tasks. The system is completely set up.</p>"
},
"_links": {
"self": {
"href": "/api/v3/projects/1",
"title": "Demo project"
},
"createWorkPackage": {
"href": "/api/v3/projects/1/work_packages/form",
"method": "post"
},
"createWorkPackageImmediately": {
"href": "/api/v3/projects/1/work_packages",
"method": "post"
},
"workPackages": {
"href": "/api/v3/projects/1/work_packages"
},
"categories": {
"href": "/api/v3/projects/1/categories"
},
"versions": {
"href": "/api/v3/projects/1/versions"
},
"memberships": {
"href": "/api/v3/memberships?filters=[{"project":{"operator":"=","values":["1"]}}]"
},
"types": {
"href": "/api/v3/projects/1/types"
},
"update": {
"href": "/api/v3/projects/1/form",
"method": "post"
},
"updateImmediately": {
"href": "/api/v3/projects/1",
"method": "patch"
},
"delete": {
"href": "/api/v3/projects/1",
"method": "delete"
},
"schema": {
"href": "/api/v3/projects/schema"
},
"parent": {
"href": null,
"title": null
}
}
},
"user": {
"_type": "User",
"id": 1,
"name": "John Smith",
"createdAt": "2021-02-18T10:10:46Z",
"updatedAt": "2021-03-30T16:08:19Z",
"login": "[email protected]",
"admin": true,
"firstName": "John",
"lastName": "Smith",
"email": "[email protected]",
"avatar": "https://secure.gravatar.com/avatar/af5c1e5d637c5bdf7be72b0904272a89?default=404&secure=true",
"status": "active",
"identityUrl": null,
"language": "en",
"_links": {
"self": {
"href": "/api/v3/users/1",
"title": "John Smith"
},
"memberships": {
"href": "/api/v3/memberships?filters=[{"principal":{"operator":"=","values":["1"]}}]",
"title": "Members"
},
"showUser": {
"href": "/users/1",
"type": "text/html"
},
"updateImmediately": {
"href": "/api/v3/users/1",
"title": "Update [email protected]",
"method": "patch"
},
"lock": {
"href": "/api/v3/users/1/lock",
"title": "Set lock on [email protected]",
"method": "post"
}
}
},
"sortBy": [
{
"_type": "QuerySortBy",
"id": "id-asc",
"name": "ID (Ascending)",
"_links": {
"self": {
"href": "/api/v3/queries/sort_bys/id-asc",
"title": "ID (Ascending)"
},
"column": {
"href": "/api/v3/queries/columns/id",
"title": "ID"
},
"direction": {
"href": "urn:openproject-org:api:v3:queries:directions:asc",
"title": "Ascending"
}
}
}
],
"columns": [
{
"_type": "QueryColumn::Property",
"id": "id",
"name": "ID",
"_links": {
"self": {
"href": "/api/v3/queries/columns/id",
"title": "ID"
}
}
},
{
"_type": "QueryColumn::Property",
"id": "subject",
"name": "Subject",
"_links": {
"self": {
"href": "/api/v3/queries/columns/subject",
"title": "Subject"
}
}
},
{
"_type": "QueryColumn::Property",
"id": "status",
"name": "Status",
"_links": {
"self": {
"href": "/api/v3/queries/columns/status",
"title": "Status"
}
}
}
],
"highlightedAttributes": []
},
"_links": {
"self": {
"href": "/api/v3/queries/39",
"title": "My filter-query"
},
"project": {
"href": "/api/v3/projects/1",
"title": "Demo project"
},
"results": {
"href": "/api/v3/projects/1/work_packages?columns[]=id&columns[]=subject&columns[]=status&filters=[{"status":{"operator":"o","values":[]}},{"assignee":{"operator":"=","values":["me"]}}]&offset=1&pageSize=20&showHierarchies=true&showSums=false&sortBy=[["id","asc"]]"
},
"star": {
"href": "/api/v3/queries/39/star",
"method": "patch"
},
"schema": {
"href": "/api/v3/projects/demo-project/queries/schema"
},
"update": {
"href": "/api/v3/queries/39/form",
"method": "post"
},
"updateImmediately": {
"href": "/api/v3/queries/39",
"method": "patch"
},
"updateOrderedWorkPackages": {
"href": "/api/v3/queries/39/order",
"method": "put"
},
"delete": {
"href": "/api/v3/queries/39",
"method": "delete"
},
"user": {
"href": "/api/v3/users/1",
"title": "John Smith"
},
"sortBy": [
{
"href": "/api/v3/queries/sort_bys/id-asc",
"title": "ID (Ascending)"
}
],
"groupBy": {
"href": null,
"title": null
},
"columns": [
{
"href": "/api/v3/queries/columns/id",
"title": "ID"
},
{
"href": "/api/v3/queries/columns/subject",
"title": "Subject"
},
{
"href": "/api/v3/queries/columns/status",
"title": "Status"
}
],
"highlightedAttributes": []
}
}
10 changes: 8 additions & 2 deletions openproject.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ type OPGenericDescription struct {
HTML string `json:"html,omitempty" structs:"html,omitempty"`
}

// OPGenericLink is an structure widely used in several OpenProject API objects
type OPGenericLink struct {
Href string `json:"href,omitempty" structs:"href,omitempty"`
Name string `json:"name,omitempty" structs:"name,omitempty"`
}

// Time represents the Time definition of OpenProject as a time.Time of go
type Time time.Time

Expand Down Expand Up @@ -483,9 +489,9 @@ func GetListWithContext(ctx context.Context, objService interface{}, apiEndPoint

// CreateWithContext (generic) creates an instance af an object (HTTP POST verb)
// Return the instance of the object rendered into proper struct as interface{} to be cast in the caller
func CreateWithContext(ctx context.Context, objService interface{}, apiEndPoint string) (interface{}, *Response, error) {
func CreateWithContext(ctx context.Context, object interface{}, objService interface{}, apiEndPoint string) (interface{}, *Response, error) {
client, resultObj := getObjectAndClient(objService)
req, err := client.NewRequestWithContext(ctx, "POST", apiEndPoint, resultObj)
req, err := client.NewRequestWithContext(ctx, "POST", apiEndPoint, object)
if err != nil {
return nil, nil, err
}
Expand Down
49 changes: 34 additions & 15 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,23 @@ type QueryElements struct {
// Query is the object representing OpenProject queries.
// TODO: Complete fields (i.e. timelineVisible, showHierarchies, timelineZoomLevel, showHierarchies, etc...
type Query struct {
Type string `json:"_type,omitempty" structs:"_type,omitempty"`
Starred bool `json:"starred,omitempty" structs:"starred,omitempty"`
ID int `json:"id,omitempty" structs:"id,omitempty"`
Name string `json:"name,omitempty" structs:"name,omitempty"`
CreatedAt *Time `json:"createdAt,omitempty" structs:"createdAt,omitempty"`
UpdatedAt *Time `json:"updatedAt,omitempty" structs:"updatedAt,omitempty"`
Filters []QueryFilter `json:"filters,omitempty" structs:"filters,omitempty"`
Sums bool `json:"sums,omitempty" structs:"sums,omitempty"`
Public bool `json:"public,omitempty" structs:"public,omitempty"`
Hidden bool `json:"hidden,omitempty" structs:"hidden,omitempty"`
Type string `json:"_type,omitempty" structs:"_type,omitempty"`
Starred bool `json:"starred,omitempty" structs:"starred,omitempty"`
ID int `json:"id,omitempty" structs:"id,omitempty"`
Name string `json:"name,omitempty" structs:"name,omitempty"`
Project string `json:"project,omitempty" structs:"columns,omitempty"`
CreatedAt *Time `json:"createdAt,omitempty" structs:"createdAt,omitempty"`
UpdatedAt *Time `json:"updatedAt,omitempty" structs:"updatedAt,omitempty"`
Filters []QueryFilter `json:"filters,omitempty" structs:"filters,omitempty"`
Sums bool `json:"sums,omitempty" structs:"sums,omitempty"`
Public bool `json:"public,omitempty" structs:"public,omitempty"`
Hidden bool `json:"hidden,omitempty" structs:"hidden,omitempty"`
TimelineVisible bool `json:"timelineVisible,omitempty" structs:"timelineVisible,omitempty"`
HighlightingMode string `json:"highlightingMode,omitempty" structs:"highlightingMode,omitempty"`
ShowHierarchies bool `json:"showHierarchies,omitempty" structs:"showHierarchies,omitempty"`
Columns []OPGenericLink `json:"columns,omitempty" structs:"columns,omitempty"`
GroupBy []OPGenericLink `json:"groupBy,omitempty" structs:"groupBy,omitempty"`
SortBy []OPGenericLink `json:"sortBy,omitempty" structs:"sortBy,omitempty"`
}

// QueryFilter filters within a query
Expand All @@ -58,11 +65,6 @@ func (s *QueryService) Get(queryID string) (*Query, *Response, error) {
return s.GetWithContext(context.Background(), queryID)
}

// GetList wraps GetListWithContext using the background context.
func (s *QueryService) GetList() (*SearchResultQuery, *Response, error) {
return s.GetListWithContext(context.Background())
}

// GetListWithContext Retrieve status list with context
// TODO: Implement search parameters-options
func (s *QueryService) GetListWithContext(ctx context.Context) (*SearchResultQuery, *Response, error) {
Expand All @@ -71,6 +73,23 @@ func (s *QueryService) GetListWithContext(ctx context.Context) (*SearchResultQue
return Obj.(*SearchResultQuery), Resp, err
}

// GetList wraps GetListWithContext using the background context.
func (s *QueryService) GetList() (*SearchResultQuery, *Response, error) {
return s.GetListWithContext(context.Background())
}

// CreateWithContext creates a query from a JSON representation.
func (s *QueryService) CreateWithContext(ctx context.Context, queryObj *Query) (*Query, *Response, error) {
apiEndpoint := "api/v3/queries"
wpResponse, resp, err := CreateWithContext(ctx, queryObj, s, apiEndpoint)
return wpResponse.(*Query), resp, err
}

// Create wraps CreateWithContext using the background context.
func (s *QueryService) Create(queryObj *Query) (*Query, *Response, error) {
return s.CreateWithContext(context.Background(), queryObj)
}

// DeleteWithContext will delete a single query object
func (s *QueryService) DeleteWithContext(ctx context.Context, queryID string) (*Response, error) {
apiEndPoint := fmt.Sprintf("api/v3/queries/%s", queryID)
Expand Down
Loading

0 comments on commit 06de5d4

Please sign in to comment.