Skip to content

Commit

Permalink
Add new end-point to handle parents and children data records
Browse files Browse the repository at this point in the history
  • Loading branch information
vkuznet committed Jun 1, 2024
1 parent 8912d90 commit 9d095ab
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
31 changes: 31 additions & 0 deletions dbs/children.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package dbs

// dbs.children module
// Copyright (c) 2023 - Valentin Kuznetsov <[email protected]>
//
// nolint: gocyclo

// Children DBS API
//
//gocyclo:ignore
func (a *API) GetChild() error {
var args []interface{}
var conds []string
var err error

tmpl := make(map[string]any)
tmpl["Owner"] = DBOWNER
stm, err := LoadTemplateSQL("select_child", tmpl)
if err != nil {
return Error(err, LoadErrorCode, "", "dbs.children.GetChild")
}

stm = WhereClause(stm, conds)

// use generic query API to fetch the results from DB
err = executeAll(a.Writer, a.Separator, stm, args...)
if err != nil {
return Error(err, QueryErrorCode, "", "dbs.children.GetChild")
}
return nil
}
4 changes: 2 additions & 2 deletions dbs/parents.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ func (a *API) GetParent() error {
tmpl["Owner"] = DBOWNER
stm, err := LoadTemplateSQL("select_parent", tmpl)
if err != nil {
return Error(err, LoadErrorCode, "", "dbs.parents.Parents")
return Error(err, LoadErrorCode, "", "dbs.parents.GetParent")
}

stm = WhereClause(stm, conds)

// use generic query API to fetch the results from DB
err = executeAll(a.Writer, a.Separator, stm, args...)
if err != nil {
return Error(err, QueryErrorCode, "", "dbs.parents.Parents")
return Error(err, QueryErrorCode, "", "dbs.parents.GetParent")
}
return nil
}
Expand Down
24 changes: 24 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ type NameRequest struct {
Name string `uri:"name" json:"name"`
}

// ChildHandler provides access to /child and /child/:name end-point
func ChildHandler(c *gin.Context) {
ApiHandler(c, "child")
}

// ParentHandler provides access to /parent and /parent/:name end-point
func ParentHandler(c *gin.Context) {
ApiHandler(c, "parent")
}

// FileHandler provides access to /files and /file/:name end-point
func FileHandler(c *gin.Context) {
ApiHandler(c, "file")
Expand Down Expand Up @@ -151,6 +161,10 @@ func getApi(c *gin.Context, a string) (*dbs.API, error) {
api.Params["dataset"] = rest.Name
} else if a == "file" && rest.Name != "" {
api.Params["file"] = rest.Name
} else if a == "parent" && rest.Name != "" {
api.Params["parent"] = rest.Name
} else if a == "child" && rest.Name != "" {
api.Params["child"] = rest.Name
}
}

Expand Down Expand Up @@ -181,6 +195,10 @@ func DBSGetHandler(c *gin.Context, a string) {
err = api.GetDataset()
} else if a == "file" {
err = api.GetFile()
} else if a == "child" {
err = api.GetChild()
} else if a == "parent" {
err = api.GetParent()
} else {
err = dbs.NotImplementedApiErr
}
Expand All @@ -206,6 +224,8 @@ func DBSPostHandler(c *gin.Context, a string) {
err = api.InsertDataset()
} else if a == "file" {
err = api.InsertFile()
} else if a == "parent" {
err = api.InsertParent()
} else {
err = dbs.NotImplementedApiErr
}
Expand All @@ -229,6 +249,8 @@ func DBSPutHandler(c *gin.Context, a string) {
err = api.UpdateDataset()
} else if a == "file" {
err = api.UpdateFile()
} else if a == "Parent" {
err = api.UpdateParent()
} else {
err = dbs.NotImplementedApiErr
}
Expand All @@ -252,6 +274,8 @@ func DBSDeleteHandler(c *gin.Context, a string) {
err = api.DeleteDataset()
} else if a == "file" {
err = api.DeleteFile()
} else if a == "parent" {
err = api.DeleteParent()
} else {
err = dbs.NotImplementedApiErr
}
Expand Down
23 changes: 21 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,35 @@ func setupRouter() *gin.Engine {
server.Route{Method: "GET", Path: "/datasets", Handler: DatasetHandler, Authorized: false},
server.Route{Method: "GET", Path: "/dataset", Handler: DatasetHandler, Authorized: false},
server.Route{Method: "GET", Path: "/dataset/*name", Handler: DatasetHandler, Authorized: false},

server.Route{Method: "GET", Path: "/files", Handler: FileHandler, Authorized: false},
server.Route{Method: "GET", Path: "/file", Handler: FileHandler, Authorized: false},
server.Route{Method: "GET", Path: "/file/*name", Handler: FileHandler, Authorized: false},

server.Route{Method: "GET", Path: "/parents", Handler: ParentHandler, Authorized: false},
server.Route{Method: "GET", Path: "/parent", Handler: ParentHandler, Authorized: false},
server.Route{Method: "GET", Path: "/parent/*name", Handler: ParentHandler, Authorized: false},

server.Route{Method: "GET", Path: "/children", Handler: ChildHandler, Authorized: false},
server.Route{Method: "GET", Path: "/child", Handler: ChildHandler, Authorized: false},
server.Route{Method: "GET", Path: "/child/*name", Handler: ChildHandler, Authorized: false},

// authorized routes

// dataset routes
server.Route{Method: "POST", Path: "/dataset", Handler: DatasetHandler, Authorized: true, Scope: "write"},
server.Route{Method: "POST", Path: "/file", Handler: FileHandler, Authorized: true, Scope: "write"},
server.Route{Method: "PUT", Path: "/dataset/*name", Handler: DatasetHandler, Authorized: true, Scope: "write"},
server.Route{Method: "PUT", Path: "/file/*name", Handler: FileHandler, Authorized: true, Scope: "write"},
server.Route{Method: "DELETE", Path: "/dataset/*name", Handler: DatasetHandler, Authorized: true, Scope: "delete"},

// file routes
server.Route{Method: "POST", Path: "/file", Handler: FileHandler, Authorized: true, Scope: "write"},
server.Route{Method: "PUT", Path: "/file/*name", Handler: FileHandler, Authorized: true, Scope: "write"},
server.Route{Method: "DELETE", Path: "/file/*name", Handler: FileHandler, Authorized: true, Scope: "delete"},

// parent routes
server.Route{Method: "POST", Path: "/parent", Handler: ParentHandler, Authorized: true, Scope: "write"},
server.Route{Method: "PUT", Path: "/parent/*name", Handler: ParentHandler, Authorized: true, Scope: "write"},
server.Route{Method: "DELETE", Path: "/parent/*name", Handler: ParentHandler, Authorized: true, Scope: "write"},
}
r := server.Router(routes, nil, "static", srvConfig.Config.DataBookkeeping.WebServer)
return r
Expand Down

0 comments on commit 9d095ab

Please sign in to comment.