Loggest thine Stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

130 lines
3.3 KiB

package httpapi
import (
"git.aiterp.net/stufflog3/stufflog3/entities"
"git.aiterp.net/stufflog3/stufflog3/models"
"git.aiterp.net/stufflog3/stufflog3/usecases/auth"
"git.aiterp.net/stufflog3/stufflog3/usecases/scopes"
"github.com/gin-gonic/gin"
"net/http"
)
func AllScopeMiddleware(scopes *scopes.Service, auth *auth.Service) gin.HandlerFunc {
return func(c *gin.Context) {
user := auth.GetUser(c.Request.Context())
if user == nil {
c.AbortWithStatusJSON(http.StatusUnauthorized, Error{
Code: http.StatusNotFound,
Message: "Scopes not found or inaccessible",
})
return
}
userScopes, err := scopes.List(c.Request.Context())
if err != nil {
c.AbortWithStatusJSON(http.StatusUnauthorized, Error{
Code: http.StatusNotFound,
Message: "Scopes not found or inaccessible",
})
return
}
c.Request = c.Request.WithContext(scopes.CreateListContext(c.Request.Context(), user.ID, userScopes))
}
}
func ScopeMiddleware(scopes *scopes.Service, auth *auth.Service) gin.HandlerFunc {
return func(c *gin.Context) {
user := auth.GetUser(c.Request.Context())
if user == nil {
c.AbortWithStatusJSON(http.StatusUnauthorized, Error{
Code: http.StatusNotFound,
Message: "Scope not found or inaccessible",
})
}
id, err := reqInt(c, "scope_id")
if err != nil {
c.AbortWithStatusJSON(http.StatusUnauthorized, Error{
Code: http.StatusBadRequest,
Message: "Invalid scope ID in path!",
Data: models.BadInputError{
Object: "params",
Field: "scopeid",
Problem: "Not a number",
},
})
}
scope, err := scopes.Find(c.Request.Context(), id)
if err != nil {
c.AbortWithStatusJSON(http.StatusUnauthorized, Error{
Code: http.StatusNotFound,
Message: "Scope not found or inaccessible",
})
return
}
c.Request = c.Request.WithContext(scopes.CreateContext(c.Request.Context(), user.ID, *scope))
}
}
func Scopes(g *gin.RouterGroup, scopes *scopes.Service) {
type scopeInput struct {
entities.Scope
OwnerName string `json:"ownerName"`
}
g.GET("", handler("scopes", func(c *gin.Context) (interface{}, error) {
return scopes.List(c.Request.Context())
}))
g.GET("/:scope_id", handler("scope", func(c *gin.Context) (interface{}, error) {
id, err := reqInt(c, "scope_id")
if err != nil {
return nil, err
}
return scopes.Find(c.Request.Context(), id)
}))
g.POST("", handler("scope", func(c *gin.Context) (interface{}, error) {
input := scopeInput{}
err := c.BindJSON(&input)
if err != nil {
return nil, models.BadInputError{
Object: "Scope",
Problem: "Invalid JSON: " + err.Error(),
}
}
return scopes.Create(c.Request.Context(), input.Scope, input.OwnerName)
}))
g.PUT("/:scope_id", handler("scope", func(c *gin.Context) (interface{}, error) {
input := models.ScopeUpdate{}
err := c.BindJSON(&input)
if err != nil {
return nil, models.BadInputError{
Object: "Scope",
Problem: "Invalid JSON: " + err.Error(),
}
}
id, err := reqInt(c, "scope_id")
if err != nil {
return nil, err
}
return scopes.Update(c.Request.Context(), id, input)
}))
g.DELETE("/:scope_id", handler("scope", func(c *gin.Context) (interface{}, error) {
id, err := reqInt(c, "scope_id")
if err != nil {
return nil, err
}
return scopes.Delete(c.Request.Context(), id)
}))
}