Remove some duplication in API handlers

This commit is contained in:
eikendev 2020-08-02 17:31:41 +02:00
parent 018ce2e537
commit a5418d9698
No known key found for this signature in database
GPG key ID: A1BDB1B28C8EF694
5 changed files with 66 additions and 54 deletions

19
api/context.go Normal file
View file

@ -0,0 +1,19 @@
package api
import (
"errors"
"net/http"
"github.com/gin-gonic/gin"
)
func getID(ctx *gin.Context) (uint, error) {
id, ok := ctx.MustGet("user").(uint)
if !ok {
err := errors.New("an error occured while retrieving ID from context")
ctx.AbortWithError(http.StatusInternalServerError, err)
return 0, err
}
return id, nil
}