Restructure project layout

This commit is contained in:
eikendev 2021-01-16 16:56:49 +01:00
parent a49db216d5
commit 9a4a096526
No known key found for this signature in database
GPG key ID: A1BDB1B28C8EF694
32 changed files with 35 additions and 35 deletions

49
internal/api/context.go Normal file
View file

@ -0,0 +1,49 @@
package api
import (
"errors"
"net/http"
"github.com/pushbits/server/internal/model"
"github.com/gin-gonic/gin"
)
func getID(ctx *gin.Context) (uint, error) {
id, ok := ctx.MustGet("id").(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
}
func getApplication(ctx *gin.Context, db Database) (*model.Application, error) {
id, err := getID(ctx)
if err != nil {
return nil, err
}
application, err := db.GetApplicationByID(id)
if success := successOrAbort(ctx, http.StatusNotFound, err); !success {
return nil, err
}
return application, nil
}
func getUser(ctx *gin.Context, db Database) (*model.User, error) {
id, err := getID(ctx)
if err != nil {
return nil, err
}
user, err := db.GetUserByID(id)
if success := successOrAbort(ctx, http.StatusNotFound, err); !success {
return nil, err
}
return user, nil
}