DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

529726cf2afa07bd9e277fe2e1ac3c8b2ef20519 — Jonni Liljamo 1 year, 11 months ago c4afb34
feat!(sdbapi): change errors to apierror
6 files changed, 24 insertions(+), 25 deletions(-)

R sdbapi/{errors/errors.go => apierror/apierror.go}
M sdbapi/handlers/gameinfo.go
M sdbapi/handlers/usercreate.go
M sdbapi/handlers/userinfo.go
M sdbapi/handlers/userinfop.go
M sdbapi/handlers/usertoken.go
R sdbapi/errors/errors.go => sdbapi/apierror/apierror.go +1 -1
@@ 6,7 6,7 @@
 * See LICENSE for licensing information.
 */

package errors
package apierror

const (
	Placeholder        string = "placeholder"

M sdbapi/handlers/gameinfo.go => sdbapi/handlers/gameinfo.go +5 -6
@@ 9,11 9,11 @@
package handlers

import (
	"api/apierror"
	"api/auth"
	"net/http"
	"api/errors"
	"api/db"
	"api/models"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/golang-jwt/jwt/v4"


@@ 26,7 26,7 @@ func GameInfo(c *gin.Context) {
	var game models.Game
	record := db.DbConn.Where("id = ?", id).First(&game)
	if record.Error != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": errors.GameNotFound})
		c.JSON(http.StatusNotFound, gin.H{"error": apierror.GameNotFound})
		c.Abort()
		return
	}


@@ 42,16 42,15 @@ func GameInfo(c *gin.Context) {
		var user models.User
		user_record := db.DbConn.Where("email = ?", claims.Email).First(&user)
		if user_record.Error != nil {
			c.JSON(http.StatusUnauthorized, gin.H{"error": errors.NotAuthorized})
			c.JSON(http.StatusUnauthorized, gin.H{"error": apierror.NotAuthorized})
			c.Abort()
			return
		}
	} else {
		c.JSON(http.StatusNotFound, gin.H{"error": errors.Placeholder})
		c.JSON(http.StatusNotFound, gin.H{"error": apierror.Placeholder})
		c.Abort()
		return
	}

	c.JSON(http.StatusOK, gin.H{"id": game.ID, "state": game.State, "p1": game.P1, "p2": game.P2})
}


M sdbapi/handlers/usercreate.go => sdbapi/handlers/usercreate.go +7 -7
@@ 9,8 9,8 @@
package handlers

import (
	"api/apierror"
	"api/db"
	"api/errors"
	"api/models"
	"net/http"
	"net/mail"


@@ 21,14 21,14 @@ import (
func CreateUser(c *gin.Context) {
	var user models.User
	if err := c.ShouldBindJSON(&user); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": errors.InvalidInput})
		c.JSON(http.StatusBadRequest, gin.H{"error": apierror.InvalidInput})
		c.Abort()
		return
	}

	// check username
	if len(user.Username) < 3 {
		c.JSON(http.StatusBadRequest, gin.H{"error": errors.UsernameTooShort})
		c.JSON(http.StatusBadRequest, gin.H{"error": apierror.UsernameTooShort})
		c.Abort()
		return
	}


@@ 36,27 36,27 @@ func CreateUser(c *gin.Context) {
	// check email
	_, mailErr := mail.ParseAddress(user.Email)
	if mailErr != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": errors.EmailInvalid})
		c.JSON(http.StatusBadRequest, gin.H{"error": apierror.EmailInvalid})
		c.Abort()
		return
	}

	// check password
	if len(user.Password) < 8 {
		c.JSON(http.StatusBadRequest, gin.H{"error": errors.PasswordTooShort})
		c.JSON(http.StatusBadRequest, gin.H{"error": apierror.PasswordTooShort})
		c.Abort()
		return
	}

	if err := user.HashPwd(user.Password); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": errors.PasswordHashFailed})
		c.JSON(http.StatusInternalServerError, gin.H{"error": apierror.PasswordHashFailed})
		c.Abort()
		return
	}

	entry := db.DbConn.Create(&user)
	if entry.Error != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": errors.UserCreationFailed})
		c.JSON(http.StatusInternalServerError, gin.H{"error": apierror.UserCreationFailed})
		c.Abort()
		return
	}

M sdbapi/handlers/userinfo.go => sdbapi/handlers/userinfo.go +2 -2
@@ 9,8 9,8 @@
package handlers

import (
	"api/apierror"
	"api/db"
	"api/errors"
	"api/models"
	"net/http"



@@ 23,7 23,7 @@ func UserInfo(c *gin.Context) {
	var user models.User
	record := db.DbConn.Where("id = ?", id).First(&user)
	if record.Error != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": errors.UserNotFound})
		c.JSON(http.StatusNotFound, gin.H{"error": apierror.UserNotFound})
		c.Abort()
		return
	}

M sdbapi/handlers/userinfop.go => sdbapi/handlers/userinfop.go +4 -4
@@ 9,9 9,9 @@
package handlers

import (
	"api/apierror"
	"api/auth"
	"api/db"
	"api/errors"
	"api/models"
	"net/http"



@@ 25,7 25,7 @@ func UserInfoP(c *gin.Context) {
	var user models.User
	record := db.DbConn.Where("id = ?", id).First(&user)
	if record.Error != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": errors.UserNotFound})
		c.JSON(http.StatusNotFound, gin.H{"error": apierror.UserNotFound})
		c.Abort()
		return
	}


@@ 37,12 37,12 @@ func UserInfoP(c *gin.Context) {

	if claims, ok := token.Claims.(*auth.JWTClaims); ok && token.Valid {
		if user.Email != claims.Email {
			c.JSON(http.StatusUnauthorized, gin.H{"error": errors.NotAuthorized})
			c.JSON(http.StatusUnauthorized, gin.H{"error": apierror.NotAuthorized})
			c.Abort()
			return
		}
	} else {
		c.JSON(http.StatusNotFound, gin.H{"error": errors.Placeholder})
		c.JSON(http.StatusNotFound, gin.H{"error": apierror.Placeholder})
		c.Abort()
		return
	}

M sdbapi/handlers/usertoken.go => sdbapi/handlers/usertoken.go +5 -5
@@ 9,9 9,9 @@
package handlers

import (
	"api/apierror"
	"api/auth"
	"api/db"
	"api/errors"
	"api/models"
	"net/http"



@@ 26,7 26,7 @@ type TokenRequest struct {
func GenerateToken(c *gin.Context) {
	var req TokenRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": errors.InvalidInput})
		c.JSON(http.StatusBadRequest, gin.H{"error": apierror.InvalidInput})
		c.Abort()
		return
	}


@@ 34,21 34,21 @@ func GenerateToken(c *gin.Context) {
	var user models.User
	record := db.DbConn.Where("email = ?", req.Email).First(&user)
	if record.Error != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": errors.UserNotFound})
		c.JSON(http.StatusNotFound, gin.H{"error": apierror.UserNotFound})
		c.Abort()
		return
	}

	credErr := user.VerifyPwd(req.Password)
	if credErr != nil {
		c.JSON(http.StatusUnauthorized, gin.H{"error": errors.InvalidCred})
		c.JSON(http.StatusUnauthorized, gin.H{"error": apierror.InvalidCred})
		c.Abort()
		return
	}

	token, err := auth.NewJWTToken(user.Username, user.Email)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": errors.NewJWTError})
		c.JSON(http.StatusInternalServerError, gin.H{"error": apierror.NewJWTError})
		c.Abort()
		return
	}