Skip to content
This repository was archived by the owner on Dec 18, 2019. It is now read-only.

Commit

Permalink
[#7] Support default links
Browse files Browse the repository at this point in the history
Signed-off-by: Wilson Júnior <[email protected]>
  • Loading branch information
wpjunior committed Jan 24, 2016
1 parent 64be674 commit e25708a
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 4 deletions.
2 changes: 1 addition & 1 deletion db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ type Database interface {
}

type ItemSchemasReply struct {
Items []schemas.ItemSchema `json:"items"`
Items []*schemas.ItemSchema `json:"items"`
}
2 changes: 1 addition & 1 deletion db/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (m *MongoDB) FindItemSchema(filter *db.Filter) (*db.ItemSchemasReply, error
query := session.DB("").C(schemas.ItemSchemaCollectionName).Find(where)

reply := &db.ItemSchemasReply{}
reply.Items = []schemas.ItemSchema{}
reply.Items = []*schemas.ItemSchema{}
err := query.Skip(filter.Skip()).Limit(filter.PerPage).Iter().All(&reply.Items)

if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions schemas/item_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type ItemSchema struct {
Properties Properties `json:"properties,omitempty"`
// used only in draft4
Required []string `json:"required,omitempty"`
Links *Links `json:"links,omitempty"`
}

// NewItemSchemaFromReader return a new ItemSchema by an io.Reader.
Expand Down Expand Up @@ -60,6 +61,11 @@ func (schema *ItemSchema) String() string {
return fmt.Sprintf(`<ItemSchema "%s">`, schema.CollectionName)
}

func (schema *ItemSchema) AttachDefaultLinks() {
defaultLinks := BuildDefaultLinks(schema.CollectionName)
schema.Links = &defaultLinks
}

func (schema *ItemSchema) validate() errors.Error {
validation := &errors.ValidationError{}

Expand Down
32 changes: 32 additions & 0 deletions schemas/link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package schemas

import (
"fmt"
)

type Link struct {
Rel string `json:"rel" bson:"rel"`
Href string `json:"href" bson:"href"`
Title string `json:"title,omitempty" bson:"title,omitempty"`
TargetSchema map[string]interface{} `json:"targetSchema,omitempty" bson:"targetSchema,omitempty"`
MediaType string `json:"mediaType,omitempty" bson:"mediaType,omitempty"`
Method string `json:"method,omitempty" bson:"method,omitempty"`
EncType string `json:"encType,omitempty" bson:"encType,omitempty"`
Schema map[string]interface{} `json:"schema,omitempty" bson:"schema,omitempty"`
}

type Links []Link

func BuildDefaultLinks(collectionName string) Links {
collectionUrl := fmt.Sprintf("/api/%s", collectionName)
itemUrl := fmt.Sprintf("/api/%s/{id}", collectionName)

return Links{
Link{Rel: "self", Href: itemUrl},
Link{Rel: "item", Href: itemUrl},
Link{Rel: "create", Method: "POST", Href: collectionUrl},
Link{Rel: "update", Method: "PUT", Href: itemUrl},
Link{Rel: "delete", Method: "DELETE", Href: itemUrl},
Link{Rel: "parent", Href: collectionUrl},
}
}
19 changes: 19 additions & 0 deletions schemas/link_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package schemas

import (
"gopkg.in/check.v1"
//"strings"
//"testing"
)

func (s *S) TestBuildDefaultLinks(c *check.C) {
links := BuildDefaultLinks("backstage-users")
c.Assert(links, check.DeepEquals, Links{
Link{Rel: "self", Href: "/api/backstage-users/{id}"},
Link{Rel: "item", Href: "/api/backstage-users/{id}"},
Link{Rel: "create", Method: "POST", Href: "/api/backstage-users"},
Link{Rel: "update", Method: "PUT", Href: "/api/backstage-users/{id}"},
Link{Rel: "delete", Method: "DELETE", Href: "/api/backstage-users/{id}"},
Link{Rel: "parent", Href: "/api/backstage-users"},
})
}
7 changes: 6 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ func (s *Server) findItemSchema(t *transaction.Transaction) {
return
}

for _, itemSchema := range reply.Items {
itemSchema.AttachDefaultLinks()
}

t.WriteResult(reply)
}

Expand All @@ -115,7 +119,7 @@ func (s *Server) findItemSchemaByCollectionName(t *transaction.Transaction) {
t.WriteError(err)
return
}

itemSchema.AttachDefaultLinks()
t.WriteResult(itemSchema)
}

Expand All @@ -133,6 +137,7 @@ func (s *Server) findOneItemSchema(t *transaction.Transaction) {
return
}

itemSchema.AttachDefaultLinks()
t.WriteResult(itemSchema)
}

Expand Down
2 changes: 1 addition & 1 deletion transaction/transaction_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package transaction

import (
"github.com/backstage/beat/errors"
simplejson "github.com/bitly/go-simplejson"
"gopkg.in/check.v1"
"github.com/backstage/beat/errors"
"net/http"
"net/http/httptest"
"testing"
Expand Down

0 comments on commit e25708a

Please sign in to comment.