Initialize repository

This commit is contained in:
eikendev 2020-07-26 00:28:38 +02:00
commit 1d758fcfd0
No known key found for this signature in database
GPG key ID: A1BDB1B28C8EF694
28 changed files with 1107 additions and 0 deletions

48
api/user.go Normal file
View file

@ -0,0 +1,48 @@
package api
import (
"errors"
"net/http"
"github.com/eikendev/pushbits/model"
"github.com/gin-gonic/gin"
)
// The UserDatabase interface for encapsulating database access.
type UserDatabase interface {
CreateUser(user *model.User) error
GetUserByName(name string) (*model.User, error)
}
// UserHandler holds information for processing requests about users.
type UserHandler struct {
DB UserDatabase
}
func (h *UserHandler) userExists(name string) bool {
user, _ := h.DB.GetUserByName(name)
return user != nil
}
// CreateUser creates a new user.
func (h *UserHandler) CreateUser(ctx *gin.Context) {
externalUser := model.ExternalUserWithCredentials{}
if success := successOrAbort(ctx, http.StatusBadRequest, ctx.Bind(&externalUser)); !success {
return
}
user := externalUser.IntoInternalUser()
if h.userExists(user.Name) {
ctx.AbortWithError(http.StatusBadRequest, errors.New("username already exists"))
return
}
if success := successOrAbort(ctx, http.StatusInternalServerError, h.DB.CreateUser(user)); !success {
return
}
ctx.JSON(http.StatusOK, user.IntoExternalUser())
}