CSS
This commit is contained in:
@@ -6,12 +6,12 @@ import (
|
||||
)
|
||||
|
||||
// Hello handler
|
||||
func Hello(c *fiber.Ctx) error {
|
||||
func (h Handlers) Hello(c *fiber.Ctx) error {
|
||||
return c.SendString("Hello, World!")
|
||||
}
|
||||
|
||||
// UserGet handler
|
||||
func UserGet(c *fiber.Ctx) error {
|
||||
log.WithFields(log.Fields{"userID": c.Params("userID")}).Debug("GETing user")
|
||||
return c.SendString("USER ffs")
|
||||
// AccountGet handler
|
||||
func (h Handlers) AccountGet(c *fiber.Ctx) error {
|
||||
log.WithFields(log.Fields{"accountID": c.Params("accountID")}).Debug("GETing account")
|
||||
return c.SendString("Account ffs")
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
// RequireJSON is a middleware that makes sure the request content-type always is application/json (or nothing, defaulting to application/json)
|
||||
func RequireJSON(c *fiber.Ctx) error {
|
||||
func (h Handlers) RequireJSON(c *fiber.Ctx) error {
|
||||
c.Accepts("application/json")
|
||||
contentType := string(c.Request().Header.ContentType())
|
||||
|
||||
|
||||
@@ -1,44 +1,76 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitlab.larvit.se/power-plan/api/src/db"
|
||||
"gitlab.larvit.se/power-plan/auth/src/db"
|
||||
"gitlab.larvit.se/power-plan/auth/src/utils"
|
||||
)
|
||||
|
||||
// UserCreate creates a new user
|
||||
func UserCreate(c *fiber.Ctx) error {
|
||||
type UserInput struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
// AccountCreate creates a new account
|
||||
func (h Handlers) AccountCreate(c *fiber.Ctx) error {
|
||||
type AccountInput struct {
|
||||
AccountName string `json:"accountName"`
|
||||
Password string `json:"password"`
|
||||
Fields []db.AccountCreateInputFields `json:"fields"`
|
||||
}
|
||||
|
||||
userInput := new(UserInput)
|
||||
accountInput := new(AccountInput)
|
||||
|
||||
if err := c.BodyParser(userInput); err != nil {
|
||||
if err := c.BodyParser(accountInput); err != nil {
|
||||
return c.Status(400).JSON([]ResJSONError{
|
||||
{Error: err.Error()},
|
||||
})
|
||||
}
|
||||
|
||||
fmt.Println(accountInput)
|
||||
|
||||
var errors []ResJSONError
|
||||
|
||||
if userInput.Username == "" {
|
||||
errors = append(errors, ResJSONError{Error: "Can not be empty", Field: "username"})
|
||||
}
|
||||
if userInput.Password == "" {
|
||||
errors = append(errors, ResJSONError{Error: "Can not be empty", Field: "password"})
|
||||
if accountInput.AccountName == "" {
|
||||
errors = append(errors, ResJSONError{Error: "Can not be empty", Field: "accountName"})
|
||||
}
|
||||
|
||||
if len(errors) != 0 {
|
||||
return c.Status(400).JSON(errors)
|
||||
}
|
||||
|
||||
createdUser := db.User{
|
||||
ID: uuid.New(),
|
||||
Username: userInput.Username,
|
||||
newAccountID, uuidErr := uuid.NewRandom()
|
||||
if uuidErr != nil {
|
||||
log.Fatal("Could not create new Uuid, err: " + uuidErr.Error())
|
||||
}
|
||||
|
||||
return c.Status(201).JSON(createdUser)
|
||||
hashedPwd, pwdErr := utils.HashPassword(accountInput.Password)
|
||||
if pwdErr != nil {
|
||||
log.Fatal("Could not hash password, err: " + pwdErr.Error())
|
||||
}
|
||||
|
||||
apiKey := utils.RandString(60)
|
||||
|
||||
createdAccount, err := h.Db.AccountCreate(db.AccountCreateInput{
|
||||
ID: newAccountID,
|
||||
AccountName: accountInput.AccountName,
|
||||
APIKey: apiKey,
|
||||
Fields: accountInput.Fields,
|
||||
Password: hashedPwd,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if strings.HasPrefix(err.Error(), "ERROR: duplicate key") {
|
||||
return c.Status(409).JSON([]ResJSONError{{Error: "accountName is already taken"}})
|
||||
}
|
||||
return c.Status(500).JSON([]ResJSONError{{Error: err.Error()}})
|
||||
}
|
||||
|
||||
return c.Status(201).JSON(createdAccount)
|
||||
}
|
||||
|
||||
// AccountAuthAPIKey auths an APIKey
|
||||
func (h Handlers) AccountAuthAPIKey(c *fiber.Ctx) error {
|
||||
return c.Status(200).JSON("key höhö")
|
||||
}
|
||||
|
||||
@@ -1,5 +1,14 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"gitlab.larvit.se/power-plan/auth/src/db"
|
||||
)
|
||||
|
||||
// Handlers is the overall struct for all http request handlers
|
||||
type Handlers struct {
|
||||
Db db.Db
|
||||
}
|
||||
|
||||
// ResJSONError is an error field that is used in JSON error responses
|
||||
type ResJSONError struct {
|
||||
Error string `json:"error"`
|
||||
|
||||
Reference in New Issue
Block a user