Skip to content

Commit

Permalink
Example: Create user with chocolate relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwdata committed Nov 11, 2016
1 parent d3dd640 commit f049fd8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
59 changes: 59 additions & 0 deletions examples/crud_example_test.go
Expand Up @@ -176,6 +176,65 @@ var _ = Describe("CrudExample", func() {
`))
}

It("Creates a user with references chocolates", func() {
createChocolate()

rec = httptest.NewRecorder()
req, err := http.NewRequest("POST", "/v0/users", strings.NewReader(`
{
"data": {
"type": "users",
"attributes": {
"user-name": "marvin"
},
"relationships": {
"sweets": {
"data": [
{
"id": "1",
"type": "chocolates"
}
]
}
}
}
}
`))
Expect(err).ToNot(HaveOccurred())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusCreated))
Expect(rec.Body.String()).To(MatchJSON(`
{
"meta": {
"author": "The api2go examples crew",
"license": "wtfpl",
"license-url": "http://www.wtfpl.net"
},
"data": {
"id": "1",
"type": "users",
"attributes": {
"user-name": "marvin"
},
"relationships": {
"sweets": {
"data": [
{
"id": "1",
"type": "chocolates"
}
],
"links": {
"related": "http://localhost:31415/v0/users/1/sweets",
"self": "http://localhost:31415/v0/users/1/relationships/sweets"
}
}
}
}
}
`))
})

It("Replaces users sweets", func() {
createUser()
createChocolate()
Expand Down
4 changes: 2 additions & 2 deletions examples/model/model_user.go
Expand Up @@ -41,9 +41,9 @@ func (u User) GetReferences() []jsonapi.Reference {
// GetReferencedIDs to satisfy the jsonapi.MarshalLinkedRelations interface
func (u User) GetReferencedIDs() []jsonapi.ReferenceID {
result := []jsonapi.ReferenceID{}
for _, chocolate := range u.Chocolates {
for _, chocolateID := range u.ChocolatesIDs {
result = append(result, jsonapi.ReferenceID{
ID: chocolate.ID,
ID: chocolateID,
Type: "chocolates",
Name: "sweets",
})
Expand Down

0 comments on commit f049fd8

Please sign in to comment.