Skip to content

Commit

Permalink
adding models for beta users and deploy history
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkanwisher committed Oct 20, 2017
1 parent 22a0f93 commit df4cf16
Show file tree
Hide file tree
Showing 8 changed files with 367 additions and 42 deletions.
34 changes: 0 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,9 @@
# API Server

Simple Rest API using gin(framework) & gorm(orm)

## Endpoint list

### Accounts Resource

```
GET /accounts
GET /accounts/:id
POST /accounts
PUT /accounts/:id
DELETE /accounts/:id
```

### Apikeys Resource

```
GET /apikeys
GET /apikeys/:id
POST /apikeys
PUT /apikeys/:id
DELETE /apikeys/:id
```

### Applications Resource

```
GET /applications
GET /applications/:id
POST /applications
PUT /applications/:id
DELETE /applications/:id
```

server runs at http://localhost:8080



### Setup
```bash
glide install
Expand Down
235 changes: 235 additions & 0 deletions controllers/deploy_history.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
package controllers

import (
"encoding/json"
"net/http"

dbpkg "github.com/loomnetwork/dashboard/db/db"
"github.com/loomnetwork/dashboard/db/helper"
"github.com/loomnetwork/dashboard/db/models"
"github.com/loomnetwork/dashboard/db/version"

"github.com/gin-gonic/gin"
)

func GetDeployHistories(c *gin.Context) {
ver, err := version.New(c)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

db := dbpkg.DBInstance(c)
parameter, err := dbpkg.NewParameter(c, models.DeployHistory{})
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

db, err = parameter.Paginate(db)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

db = parameter.SetPreloads(db)
db = parameter.SortRecords(db)
db = parameter.FilterFields(db)
deployHistories := []models.DeployHistory{}
fields := helper.ParseFields(c.DefaultQuery("fields", "*"))
queryFields := helper.QueryFields(models.DeployHistory{}, fields)

if err := db.Select(queryFields).Find(&deployHistories).Error; err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

index := 0

if len(deployHistories) > 0 {
index = int(deployHistories[len(deployHistories)-1].ID)
}

if err := parameter.SetHeaderLink(c, index); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

if version.Range("1.0.0", "<=", ver) && version.Range(ver, "<", "2.0.0") {
// conditional branch by version.
// 1.0.0 <= this version < 2.0.0 !!
}

if _, ok := c.GetQuery("stream"); ok {
enc := json.NewEncoder(c.Writer)
c.Status(200)

for _, deployHistory := range deployHistories {
fieldMap, err := helper.FieldToMap(deployHistory, fields)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

if err := enc.Encode(fieldMap); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
}
} else {
fieldMaps := []map[string]interface{}{}

for _, deployHistory := range deployHistories {
fieldMap, err := helper.FieldToMap(deployHistory, fields)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

fieldMaps = append(fieldMaps, fieldMap)
}

if _, ok := c.GetQuery("pretty"); ok {
c.IndentedJSON(200, fieldMaps)
} else {
c.JSON(200, fieldMaps)
}
}
}

func GetDeployHistory(c *gin.Context) {
ver, err := version.New(c)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

db := dbpkg.DBInstance(c)
parameter, err := dbpkg.NewParameter(c, models.DeployHistory{})
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

db = parameter.SetPreloads(db)
deployHistory := models.DeployHistory{}
id := c.Params.ByName("id")
fields := helper.ParseFields(c.DefaultQuery("fields", "*"))
queryFields := helper.QueryFields(models.DeployHistory{}, fields)

if err := db.Select(queryFields).First(&deployHistory, id).Error; err != nil {
content := gin.H{"error": "deploy_history with id#" + id + " not found"}
c.JSON(404, content)
return
}

fieldMap, err := helper.FieldToMap(deployHistory, fields)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

if version.Range("1.0.0", "<=", ver) && version.Range(ver, "<", "2.0.0") {
// conditional branch by version.
// 1.0.0 <= this version < 2.0.0 !!
}

if _, ok := c.GetQuery("pretty"); ok {
c.IndentedJSON(200, fieldMap)
} else {
c.JSON(200, fieldMap)
}
}

func CreateDeployHistory(c *gin.Context) {
ver, err := version.New(c)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

db := dbpkg.DBInstance(c)
deployHistory := models.DeployHistory{}

if err := c.Bind(&deployHistory); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

if err := db.Create(&deployHistory).Error; err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

if version.Range("1.0.0", "<=", ver) && version.Range(ver, "<", "2.0.0") {
// conditional branch by version.
// 1.0.0 <= this version < 2.0.0 !!
}

c.JSON(201, deployHistory)
}

func UpdateDeployHistory(c *gin.Context) {
ver, err := version.New(c)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

db := dbpkg.DBInstance(c)
id := c.Params.ByName("id")
deployHistory := models.DeployHistory{}

if db.First(&deployHistory, id).Error != nil {
content := gin.H{"error": "deploy_history with id#" + id + " not found"}
c.JSON(404, content)
return
}

if err := c.Bind(&deployHistory); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

if err := db.Save(&deployHistory).Error; err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

if version.Range("1.0.0", "<=", ver) && version.Range(ver, "<", "2.0.0") {
// conditional branch by version.
// 1.0.0 <= this version < 2.0.0 !!
}

c.JSON(200, deployHistory)
}

func DeleteDeployHistory(c *gin.Context) {
ver, err := version.New(c)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

db := dbpkg.DBInstance(c)
id := c.Params.ByName("id")
deployHistory := models.DeployHistory{}

if db.First(&deployHistory, id).Error != nil {
content := gin.H{"error": "deploy_history with id#" + id + " not found"}
c.JSON(404, content)
return
}

if err := db.Delete(&deployHistory).Error; err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

if version.Range("1.0.0", "<=", ver) && version.Range(ver, "<", "2.0.0") {
// conditional branch by version.
// 1.0.0 <= this version < 2.0.0 !!
}

c.Writer.WriteHeader(http.StatusNoContent)
}
20 changes: 12 additions & 8 deletions controllers/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ func APIEndpoints(c *gin.Context) {
baseURL := fmt.Sprintf("%s://%s", reqScheme, reqHost)

resources := map[string]string{
"accounts_url": baseURL + "/accounts",
"account_url": baseURL + "/accounts/{id}",
"apikeys_url": baseURL + "/apikeys",
"apikey_url": baseURL + "/apikeys/{id}",
"applications_url": baseURL + "/applications",
"application_url": baseURL + "/applications/{id}",
"upload_url": baseURL + "/upload",
"login_oauth": baseURL + "/login_oauth",
"accounts_url": baseURL + "/accounts",
"account_url": baseURL + "/accounts/{id}",
"apikeys_url": baseURL + "/apikeys",
"apikey_url": baseURL + "/apikeys/{id}",
"applications_url": baseURL + "/applications",
"application_url": baseURL + "/applications/{id}",
"upload_url": baseURL + "/upload",
"login_oauth": baseURL + "/login_oauth",
"beta_users_url": baseURL + "/beta_users",
"beta_users_url": baseURL + "/beta_users/{id}",
"deploy_histories_url": baseURL + "/deploy_histories",
"deploy_history_url": baseURL + "/deploy_histories/{id}",
}

c.IndentedJSON(http.StatusOK, resources)
Expand Down
90 changes: 90 additions & 0 deletions docs/deploy_history.apib
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Group DeployHistories
Welcome to the deploy histories API. This API provides access to the deploy histories service.

## deploy histories [/deploy_histories]

### Create deploy history [POST]

Create a new deploy history

+ Request deploy history (application/json; charset=utf-8)
+ Headers

Accept: application/vnd.loomnetwork+json
+ Attributes

+ account_id: 1 (number)
+ application_id: 1 (number)
+ version: 1 (number)

+ Response 201 (application/json; charset=utf-8)
+ Attributes (deploy_history, fixed)

### Get deploy histories [GET]

Returns a deploy history list.

+ Request (application/json; charset=utf-8)
+ Headers

Accept: application/vnd.loomnetwork+json

+ Response 200 (application/json; charset=utf-8)
+ Attributes (array, fixed)
+ (deploy_history)

## deploy history details [/deploy_histories/{id}]

+ Parameters
+ id: `1` (enum[string]) - The ID of the desired deploy history.
+ Members
+ `1`
+ `2`
+ `3`

### Get deploy history [GET]

Returns a deploy history.

+ Request (application/json; charset=utf-8)
+ Headers

Accept: application/vnd.loomnetwork+json

+ Response 200 (application/json; charset=utf-8)
+ Attributes (deploy_history, fixed)

### Update deploy history [PUT]

Update a deploy history.

+ Request deploy_history (application/json; charset=utf-8)
+ Headers

Accept: application/vnd.loomnetwork+json
+ Attributes

+ account_id: 1 (number)
+ application_id: 1 (number)
+ version: 1 (number)

+ Response 200 (application/json; charset=utf-8)
+ Attributes (deploy_history, fixed)

### Delete deploy history [DELETE]

Delete a deploy history.

+ Request (application/json; charset=utf-8)
+ Headers

Accept: application/vnd.loomnetwork+json

+ Response 204

# Data Structures
## deploy_history (object)

+ account_id: *1* (number)
+ application_id: *1* (number)
+ version: *1* (number)
Loading

0 comments on commit df4cf16

Please sign in to comment.