api2go-gorm-gin-crud-example

command module
v0.0.0-...-c50696d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2017 License: MIT Imports: 9 Imported by: 0

README

api2go-gorm-gin-crud-example

Build Status

This is a CRUD example using jinzhu/gorm and gin-gonic/gin. This example is a fork of the one in manyminds/api2go.

Examples

Examples can be found here.

Database setup

Before running the server or running tests, copy .envrc.example to .envrc and edit .envrc for your need. Two envrinment variables DB_DIALECT and DB_PARAMS are passed to sql.Open(driverName, datasourceName string) (https://golang.org/pkg/database/sql/#Open).

After editing, run the following command to set environment variables DB_DIALECT and DB_PARAMS.

source .envrc

Tests

source .envrc
go test ./...
ginkgo -r                # Alternative
ginkgo watch -r -notify  # Watch for changes

Documentation

Overview

Package examples shows how to implement a basic CRUD for two data structures with the api2go server functionality. To play with this example server you can run some of the following curl requests

In order to demonstrate dynamic baseurl handling for requests, apply the --header="REQUEST_URI:https://www.your.domain.example.com" parameter to any of the commands.

Create a new user:

curl -X POST http://localhost:31415/v0/users -d '{"data" : {"type" : "users" , "attributes": {"user-name" : "marvin"}}}'

List users:

curl -X GET http://localhost:31415/v0/users

List paginated users:

curl -X GET 'http://localhost:31415/v0/users?page\[offset\]=0&page\[limit\]=2'

OR

curl -X GET 'http://localhost:31415/v0/users?page\[number\]=1&page\[size\]=2'

Update:

curl -vX PATCH http://localhost:31415/v0/users/1 -d '{ "data" : {"type" : "users", "id": "1", "attributes": {"user-name" : "better marvin"}}}'

Delete:

curl -vX DELETE http://localhost:31415/v0/users/2

Create a chocolate with the name sweet

curl -X POST http://localhost:31415/v0/chocolates -d '{"data" : {"type" : "chocolates" , "attributes": {"name" : "Ritter Sport", "taste": "Very Good"}}}'

Create a user with a sweet

curl -X POST http://localhost:31415/v0/users -d '{"data" : {"type" : "users" , "attributes": {"user-name" : "marvin"}, "relationships": {"sweets": {"data": [{"type": "chocolates", "id": "1"}]}}}}'

List a users sweets

curl -X GET http://localhost:31415/v0/users/1/sweets

Replace a users sweets

curl -X PATCH http://localhost:31415/v0/users/1/relationships/sweets -d '{"data" : [{"type": "chocolates", "id": "2"}]}'

Add a sweet

curl -X POST http://localhost:31415/v0/users/1/relationships/sweets -d '{"data" : [{"type": "chocolates", "id": "2"}]}'

Remove a sweet

curl -X DELETE http://localhost:31415/v0/users/1/relationships/sweets -d '{"data" : [{"type": "chocolates", "id": "2"}]}'

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL