Skip to content

Commit

Permalink
Closes #7
Browse files Browse the repository at this point in the history
  • Loading branch information
Massad committed May 9, 2020
1 parent 057313e commit 396ed9c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 44 deletions.
Binary file added .DS_Store
Binary file not shown.
46 changes: 24 additions & 22 deletions controllers/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/Massad/gin-boilerplate/forms"
"github.com/Massad/gin-boilerplate/models"

"net/http"

"github.com/gin-gonic/gin"
)

Expand All @@ -19,57 +21,57 @@ func (ctrl ArticleController) Create(c *gin.Context) {
userID := getUserID(c)

if userID == 0 {
c.JSON(403, gin.H{"message": "Please login first"})
c.JSON(http.StatusUnauthorized, gin.H{"message": "Please login first"})
c.Abort()
return
}

var articleForm forms.ArticleForm

if c.BindJSON(&articleForm) != nil {
c.JSON(406, gin.H{"message": "Invalid form", "form": articleForm})
if c.ShouldBindJSON(&articleForm) != nil {
c.JSON(http.StatusNotAcceptable, gin.H{"message": "Invalid form", "form": articleForm})
c.Abort()
return
}

articleID, err := articleModel.Create(userID, articleForm)

if articleID > 0 && err != nil {
c.JSON(406, gin.H{"message": "Article could not be created", "error": err.Error()})
c.JSON(http.StatusNotAcceptable, gin.H{"message": "Article could not be created", "error": err.Error()})
c.Abort()
return
}

c.JSON(200, gin.H{"message": "Article created", "id": articleID})
c.JSON(http.StatusOK, gin.H{"message": "Article created", "id": articleID})
}

//All ...
func (ctrl ArticleController) All(c *gin.Context) {
userID := getUserID(c)

if userID == 0 {
c.JSON(403, gin.H{"message": "Please login first"})
c.JSON(http.StatusUnauthorized, gin.H{"message": "Please login first"})
c.Abort()
return
}

data, err := articleModel.All(userID)

if err != nil {
c.JSON(406, gin.H{"Message": "Could not get the articles", "error": err.Error()})
c.JSON(http.StatusNotAcceptable, gin.H{"Message": "Could not get the articles", "error": err.Error()})
c.Abort()
return
}

c.JSON(200, gin.H{"data": data})
c.JSON(http.StatusOK, gin.H{"data": data})
}

//One ...
func (ctrl ArticleController) One(c *gin.Context) {
userID := getUserID(c)

if userID == 0 {
c.JSON(403, gin.H{"message": "Please login first"})
c.JSON(http.StatusUnauthorized, gin.H{"message": "Please login first"})
c.Abort()
return
}
Expand All @@ -80,13 +82,13 @@ func (ctrl ArticleController) One(c *gin.Context) {

data, err := articleModel.One(userID, id)
if err != nil {
c.JSON(404, gin.H{"Message": "Article not found", "error": err.Error()})
c.JSON(http.StatusNotFound, gin.H{"Message": "Article not found", "error": err.Error()})
c.Abort()
return
}
c.JSON(200, gin.H{"data": data})
c.JSON(http.StatusOK, gin.H{"data": data})
} else {
c.JSON(404, gin.H{"Message": "Invalid parameter"})
c.JSON(http.StatusNotFound, gin.H{"Message": "Invalid parameter"})
}
}

Expand All @@ -95,7 +97,7 @@ func (ctrl ArticleController) Update(c *gin.Context) {
userID := getUserID(c)

if userID == 0 {
c.JSON(403, gin.H{"message": "Please login first"})
c.JSON(http.StatusUnauthorized, gin.H{"message": "Please login first"})
c.Abort()
return
}
Expand All @@ -105,21 +107,21 @@ func (ctrl ArticleController) Update(c *gin.Context) {

var articleForm forms.ArticleForm

if c.BindJSON(&articleForm) != nil {
c.JSON(406, gin.H{"message": "Invalid parameters", "form": articleForm})
if c.ShouldBindJSON(&articleForm) != nil {
c.JSON(http.StatusNotAcceptable, gin.H{"message": "Invalid parameters", "form": articleForm})
c.Abort()
return
}

err := articleModel.Update(userID, id, articleForm)
if err != nil {
c.JSON(406, gin.H{"Message": "Article could not be updated", "error": err.Error()})
c.JSON(http.StatusNotAcceptable, gin.H{"Message": "Article could not be updated", "error": err.Error()})
c.Abort()
return
}
c.JSON(200, gin.H{"message": "Article updated"})
c.JSON(http.StatusOK, gin.H{"message": "Article updated"})
} else {
c.JSON(404, gin.H{"Message": "Invalid parameter", "error": err.Error()})
c.JSON(http.StatusNotFound, gin.H{"Message": "Invalid parameter", "error": err.Error()})
}
}

Expand All @@ -128,7 +130,7 @@ func (ctrl ArticleController) Delete(c *gin.Context) {
userID := getUserID(c)

if userID == 0 {
c.JSON(403, gin.H{"message": "Please login first"})
c.JSON(http.StatusUnauthorized, gin.H{"message": "Please login first"})
c.Abort()
return
}
Expand All @@ -138,12 +140,12 @@ func (ctrl ArticleController) Delete(c *gin.Context) {

err := articleModel.Delete(userID, id)
if err != nil {
c.JSON(406, gin.H{"Message": "Article could not be deleted", "error": err.Error()})
c.JSON(http.StatusNotAcceptable, gin.H{"Message": "Article could not be deleted", "error": err.Error()})
c.Abort()
return
}
c.JSON(200, gin.H{"message": "Article deleted"})
c.JSON(http.StatusOK, gin.H{"message": "Article deleted"})
} else {
c.JSON(404, gin.H{"Message": "Invalid parameter"})
c.JSON(http.StatusNotFound, gin.H{"Message": "Invalid parameter"})
}
}
22 changes: 12 additions & 10 deletions controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"github.com/Massad/gin-boilerplate/forms"
"github.com/Massad/gin-boilerplate/models"

"net/http"

"github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin"
)
Expand Down Expand Up @@ -39,8 +41,8 @@ func getSessionUserInfo(c *gin.Context) (userSessionInfo models.UserSessionInfo)
func (ctrl UserController) Signin(c *gin.Context) {
var signinForm forms.SigninForm

if c.BindJSON(&signinForm) != nil {
c.JSON(406, gin.H{"message": "Invalid form", "form": signinForm})
if c.ShouldBindJSON(&signinForm) != nil {
c.JSON(http.StatusNotAcceptable, gin.H{"message": "Invalid form", "form": signinForm})
c.Abort()
return
}
Expand All @@ -53,9 +55,9 @@ func (ctrl UserController) Signin(c *gin.Context) {
session.Set("user_name", user.Name)
session.Save()

c.JSON(200, gin.H{"message": "User signed in", "user": user})
c.JSON(http.StatusOK, gin.H{"message": "User signed in", "user": user})
} else {
c.JSON(406, gin.H{"message": "Invalid signin details", "error": err.Error()})
c.JSON(http.StatusNotAcceptable, gin.H{"message": "Invalid signin details", "error": err.Error()})
}

}
Expand All @@ -64,16 +66,16 @@ func (ctrl UserController) Signin(c *gin.Context) {
func (ctrl UserController) Signup(c *gin.Context) {
var signupForm forms.SignupForm

if c.BindJSON(&signupForm) != nil {
c.JSON(406, gin.H{"message": "Invalid form", "form": signupForm})
if c.ShouldBindJSON(&signupForm) != nil {
c.JSON(http.StatusNotAcceptable, gin.H{"message": "Invalid form", "form": signupForm})
c.Abort()
return
}

user, err := userModel.Signup(signupForm)

if err != nil {
c.JSON(406, gin.H{"message": err.Error()})
c.JSON(http.StatusNotAcceptable, gin.H{"message": err.Error()})
c.Abort()
return
}
Expand All @@ -84,9 +86,9 @@ func (ctrl UserController) Signup(c *gin.Context) {
session.Set("user_email", user.Email)
session.Set("user_name", user.Name)
session.Save()
c.JSON(200, gin.H{"message": "Success signup", "user": user})
c.JSON(http.StatusOK, gin.H{"message": "Success signup", "user": user})
} else {
c.JSON(406, gin.H{"message": "Could not signup this user", "error": err.Error()})
c.JSON(http.StatusNotAcceptable, gin.H{"message": "Could not signup this user", "error": err.Error()})
}

}
Expand All @@ -96,5 +98,5 @@ func (ctrl UserController) Signout(c *gin.Context) {
session := sessions.Default(c)
session.Clear()
session.Save()
c.JSON(200, gin.H{"message": "Signed out..."})
c.JSON(http.StatusOK, gin.H{"message": "Signed out..."})
}
24 changes: 12 additions & 12 deletions tests/article_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestSignup(t *testing.T) {
resp := httptest.NewRecorder()

testRouter.ServeHTTP(resp, req)
assert.Equal(t, resp.Code, 200)
assert.Equal(t, resp.Code, http.StatusOK)
}

/**
Expand Down Expand Up @@ -130,7 +130,7 @@ func TestSignupInvalidEmail(t *testing.T) {
resp := httptest.NewRecorder()

testRouter.ServeHTTP(resp, req)
assert.Equal(t, resp.Code, 400) //406
assert.Equal(t, resp.Code, http.StatusNotAcceptable)
}

/**
Expand Down Expand Up @@ -163,7 +163,7 @@ func TestSignin(t *testing.T) {

signinCookie = resp.Header().Get("Set-Cookie")

assert.Equal(t, resp.Code, 200)
assert.Equal(t, resp.Code, http.StatusOK)
}

/**
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestCreateArticle(t *testing.T) {

articleID = res.ID

assert.Equal(t, resp.Code, 200)
assert.Equal(t, resp.Code, http.StatusOK)
}

/**
Expand Down Expand Up @@ -237,14 +237,14 @@ func TestCreateInvalidArticle(t *testing.T) {
resp := httptest.NewRecorder()

testRouter.ServeHTTP(resp, req)
assert.Equal(t, resp.Code, 400) //406
assert.Equal(t, resp.Code, http.StatusNotAcceptable)
}

/**
* TestCreateArticleNotSignedIn
* Test article creation with a not signed in user
*
* Must return response code 403
* Must return response code 401
*/
func TestCreateArticleNotSignedIn(t *testing.T) {
testRouter := SetupRouter()
Expand All @@ -266,7 +266,7 @@ func TestCreateArticleNotSignedIn(t *testing.T) {
resp := httptest.NewRecorder()

testRouter.ServeHTTP(resp, req)
assert.Equal(t, resp.Code, 403)
assert.Equal(t, resp.Code, http.StatusUnauthorized)
}

/**
Expand All @@ -290,7 +290,7 @@ func TestGetArticle(t *testing.T) {
resp := httptest.NewRecorder()

testRouter.ServeHTTP(resp, req)
assert.Equal(t, resp.Code, 200)
assert.Equal(t, resp.Code, http.StatusOK)
}

/**
Expand All @@ -312,7 +312,7 @@ func TestGetInvalidArticle(t *testing.T) {
resp := httptest.NewRecorder()

testRouter.ServeHTTP(resp, req)
assert.Equal(t, resp.Code, 404)
assert.Equal(t, resp.Code, http.StatusNotFound)
}

/**
Expand Down Expand Up @@ -344,7 +344,7 @@ func TestUpdateArticle(t *testing.T) {
resp := httptest.NewRecorder()

testRouter.ServeHTTP(resp, req)
assert.Equal(t, resp.Code, 200)
assert.Equal(t, resp.Code, http.StatusOK)
}

/**
Expand All @@ -368,7 +368,7 @@ func TestDeleteArticle(t *testing.T) {
resp := httptest.NewRecorder()

testRouter.ServeHTTP(resp, req)
assert.Equal(t, resp.Code, 200)
assert.Equal(t, resp.Code, http.StatusOK)
}

/**
Expand All @@ -389,7 +389,7 @@ func TestUserSignout(t *testing.T) {
resp := httptest.NewRecorder()

testRouter.ServeHTTP(resp, req)
assert.Equal(t, resp.Code, 200)
assert.Equal(t, resp.Code, http.StatusOK)
}

/**
Expand Down

0 comments on commit 396ed9c

Please sign in to comment.