diff --git a/README.md b/README.md index 4689264..fa7e625 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/controllers/deploy_history.go b/controllers/deploy_history.go new file mode 100644 index 0000000..22448c5 --- /dev/null +++ b/controllers/deploy_history.go @@ -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) +} diff --git a/controllers/root.go b/controllers/root.go index bcd4dc8..8bb83e7 100644 --- a/controllers/root.go +++ b/controllers/root.go @@ -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) diff --git a/docs/deploy_history.apib b/docs/deploy_history.apib new file mode 100644 index 0000000..e99c2c6 --- /dev/null +++ b/docs/deploy_history.apib @@ -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) diff --git a/docs/index.apib b/docs/index.apib index e837daf..8f7b810 100644 --- a/docs/index.apib +++ b/docs/index.apib @@ -6,3 +6,4 @@ HOST: http://localhost:8080 + \ No newline at end of file diff --git a/models/beta_users.go b/models/beta_users.go new file mode 100644 index 0000000..2edbb64 --- /dev/null +++ b/models/beta_users.go @@ -0,0 +1,7 @@ +// models/beat_users.go +package models + +type BetaUsers struct { + //gorm.Model + Email string `json:"email" form:"email"` +} diff --git a/models/deploy_history.go b/models/deploy_history.go new file mode 100644 index 0000000..3d2c239 --- /dev/null +++ b/models/deploy_history.go @@ -0,0 +1,9 @@ +// models/beat_users.go +package models + +type DeployHistory struct { + //gorm.Model + AccountID uint `json:"account_id" form:"account_id"` + ApplicationID uint `json:"application_id" form:"application_id"` + Version uint `json:"version" form:"version"` +} diff --git a/router/router.go b/router/router.go index 5e6c200..2e68a23 100644 --- a/router/router.go +++ b/router/router.go @@ -75,5 +75,18 @@ func Initialize(r *gin.Engine, c *config.Config) { api.PUT("/applications/:id", controllers.UpdateApplication) api.DELETE("/applications/:id", controllers.DeleteApplication) + //Not sure we need to expose this, maybe just edit database + /* + api.GET("/beta_users", controllers.GetBetaUsers) + api.GET("/beta_users/:id", controllers.GetBetaUsers) + api.POST("/beta_users", controllers.CreateBetaUsers) + api.PUT("/beta_users/:id", controllers.UpdateBetaUsers) + api.DELETE("/beta_users/:id", controllers.DeleteBetaUsers) + */ + api.GET("/deploy_histories", controllers.GetDeployHistories) + api.GET("/deploy_histories/:id", controllers.GetDeployHistory) + api.POST("/deploy_histories", controllers.CreateDeployHistory) + api.PUT("/deploy_histories/:id", controllers.UpdateDeployHistory) + api.DELETE("/deploy_histories/:id", controllers.DeleteDeployHistory) } }