-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding models for beta users and deploy history
- Loading branch information
1 parent
22a0f93
commit df4cf16
Showing
8 changed files
with
367 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.