Implement retrieving of user data

This commit is contained in:
eikendev 2020-08-04 18:42:03 +02:00
parent a3de04b2a5
commit e4e87f2710
No known key found for this signature in database
GPG key ID: A1BDB1B28C8EF694
7 changed files with 101 additions and 73 deletions

View file

@ -11,28 +11,10 @@ import (
"github.com/gin-gonic/gin"
)
// The ApplicationDatabase interface for encapsulating database access.
type ApplicationDatabase interface {
CreateApplication(application *model.Application) error
DeleteApplication(application *model.Application) error
GetApplicationByID(ID uint) (*model.Application, error)
GetApplicationByToken(token string) (*model.Application, error)
GetApplications(user *model.User) ([]model.Application, error)
UpdateApplication(application *model.Application) error
GetUserByID(ID uint) (*model.User, error)
}
// The ApplicationDispatcher interface for relaying notifications.
type ApplicationDispatcher interface {
RegisterApplication(name, user string) (string, error)
DeregisterApplication(a *model.Application) error
}
// ApplicationHandler holds information for processing requests about applications.
type ApplicationHandler struct {
DB ApplicationDatabase
DP ApplicationDispatcher
DB Database
DP Dispatcher
}
func (h *ApplicationHandler) applicationExists(token string) bool {
@ -40,20 +22,6 @@ func (h *ApplicationHandler) applicationExists(token string) bool {
return application != nil
}
func (h *ApplicationHandler) getApplication(ctx *gin.Context) (*model.Application, error) {
id, err := getID(ctx)
if err != nil {
return nil, err
}
application, err := h.DB.GetApplicationByID(id)
if success := successOrAbort(ctx, http.StatusNotFound, err); !success {
return nil, err
}
return application, nil
}
func (h *ApplicationHandler) registerApplication(ctx *gin.Context, a *model.Application, u *model.User) error {
log.Printf("Registering application %s.\n", a.Name)
@ -123,6 +91,9 @@ func (h *ApplicationHandler) CreateApplication(ctx *gin.Context) {
}
user := authentication.GetUser(ctx)
if user == nil {
return
}
application, err := h.createApplication(ctx, createApplication.Name, user)
if err != nil {
@ -132,10 +103,10 @@ func (h *ApplicationHandler) CreateApplication(ctx *gin.Context) {
ctx.JSON(http.StatusOK, &application)
}
// GetApplications returns all applications for the current user.
// GetApplications returns all applications of the current user.
func (h *ApplicationHandler) GetApplications(ctx *gin.Context) {
user, err := getUser(ctx, h.DB)
if err != nil {
user := authentication.GetUser(ctx)
if user == nil {
return
}
@ -147,15 +118,15 @@ func (h *ApplicationHandler) GetApplications(ctx *gin.Context) {
ctx.JSON(http.StatusOK, &applications)
}
// GetApplication returns all applications for the current user.
// GetApplication returns the application with the specified ID.
func (h *ApplicationHandler) GetApplication(ctx *gin.Context) {
application, err := h.getApplication(ctx)
application, err := getApplication(ctx, h.DB)
if err != nil {
return
}
user, err := getUser(ctx, h.DB)
if err != nil {
user := authentication.GetUser(ctx)
if user == nil {
return
}
@ -170,7 +141,7 @@ func (h *ApplicationHandler) GetApplication(ctx *gin.Context) {
// DeleteApplication deletes an application with a certain ID.
func (h *ApplicationHandler) DeleteApplication(ctx *gin.Context) {
application, err := h.getApplication(ctx)
application, err := getApplication(ctx, h.DB)
if err != nil {
return
}
@ -190,7 +161,7 @@ func (h *ApplicationHandler) DeleteApplication(ctx *gin.Context) {
// UpdateApplication updates an application with a certain ID.
func (h *ApplicationHandler) UpdateApplication(ctx *gin.Context) {
application, err := h.getApplication(ctx)
application, err := getApplication(ctx, h.DB)
if err != nil {
return
}