added basic skeleton and two API endpoints to test backend stack.

This commit is contained in:
2022-04-12 20:30:16 +02:00
parent 22e8290371
commit 11c0bd62e2
40 changed files with 1480 additions and 8 deletions
+31
View File
@@ -0,0 +1,31 @@
package api
import (
"git.aiterp.net/stufflog3/stufflog3-api/internal/slerrors"
"github.com/gin-gonic/gin"
"strconv"
)
func handler(key string, callback func(c *gin.Context) (interface{}, error)) gin.HandlerFunc {
return func(c *gin.Context) {
res, err := callback(c)
if err != nil {
slerrors.Respond(c, err)
return
}
resJson := make(map[string]interface{}, 1)
resJson[key] = res
c.JSON(200, resJson)
}
}
func reqInt(c *gin.Context, key string) (int, error) {
v, err := strconv.Atoi(c.Param(key))
if err != nil {
return 0, slerrors.BadRequest(key + " parameter must be a number")
}
return v, nil
}
+31
View File
@@ -0,0 +1,31 @@
package api
import (
"git.aiterp.net/stufflog3/stufflog3-api/internal/auth"
"git.aiterp.net/stufflog3/stufflog3-api/internal/database"
"git.aiterp.net/stufflog3/stufflog3-api/internal/slerrors"
"github.com/gin-gonic/gin"
)
func Scopes(g *gin.RouterGroup, db database.Database) {
g.GET("/", handler("goals", func(c *gin.Context) (interface{}, error) {
return db.Scopes().ListByUser(c.Request.Context(), auth.UserID(c))
}))
g.GET("/:id", handler("goals", func(c *gin.Context) (interface{}, error) {
id, err := reqInt(c, "id")
if err != nil {
return nil, err
}
scope, err := db.Scopes().Find(c.Request.Context(), id)
if err != nil {
return nil, err
}
if !scope.HasMember(auth.UserID(c)) {
return nil, slerrors.NotFound("Scope")
}
return scope, nil
}))
}