-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Introduce feedback api * Add binding required * Add test
1 parent
e496cb0
commit 8b87e20
Showing
8 changed files
with
125 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package feedback | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/NetSepio/gateway/api/middleware/auth/paseto" | ||
"github.com/NetSepio/gateway/config/dbconfig" | ||
"github.com/NetSepio/gateway/models" | ||
"github.com/NetSepio/gateway/util/pkg/httphelper" | ||
"github.com/jinzhu/gorm" | ||
"github.com/lib/pq" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// ApplyRoutes applies router to gin Router | ||
func ApplyRoutes(r *gin.RouterGroup) { | ||
g := r.Group("/feedback") | ||
{ | ||
g.Use(paseto.PASETO) | ||
g.POST("", createFeedback) | ||
} | ||
} | ||
|
||
func createFeedback(c *gin.Context) { | ||
db := dbconfig.GetDb() | ||
var requestBody PostFeedbackRequest | ||
err := c.BindJSON(&requestBody) | ||
if err != nil { | ||
return | ||
} | ||
walletAddress := c.GetString("walletAddress") | ||
|
||
result := db.Model(&models.User{}).Where("wallet_address = ?", walletAddress). | ||
Update("feedbacks", gorm.Expr("feedbacks || ?", pq.StringArray([]string{requestBody.Feedback}))) | ||
|
||
if result.Error != nil { | ||
httphelper.ErrResponse(c, http.StatusInternalServerError, "Unexpected error occured") | ||
|
||
return | ||
} | ||
if result.RowsAffected == 0 { | ||
httphelper.ErrResponse(c, http.StatusNotFound, "Record not found") | ||
|
||
return | ||
} | ||
httphelper.SuccessResponse(c, "Feedback added", nil) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package feedback | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/NetSepio/gateway/config" | ||
"github.com/NetSepio/gateway/util/pkg/logwrapper" | ||
"github.com/NetSepio/gateway/util/testingcommon" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_PostFeedback(t *testing.T) { | ||
config.Init("../../../.env") | ||
logwrapper.Init("../../../logs") | ||
t.Cleanup(testingcommon.DeleteCreatedEntities()) | ||
testWallet := testingcommon.GenerateWallet() | ||
header := testingcommon.PrepareAndGetAuthHeader(t, testWallet.WalletAddress) | ||
|
||
url := "/api/v1.0/feedback" | ||
|
||
t.Run("Should be able to add feedback", func(t *testing.T) { | ||
rr := httptest.NewRecorder() | ||
|
||
requestBody := PostFeedbackRequest{ | ||
Feedback: "Very helpfull for avoiding spam and harmfull domains", | ||
} | ||
jsonData, err := json.Marshal(requestBody) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) | ||
req.Header.Add("Authorization", header) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
c, _ := gin.CreateTestContext(rr) | ||
c.Request = req | ||
c.Set("walletAddress", testWallet.WalletAddress) | ||
|
||
createFeedback(c) | ||
assert.Equal(t, http.StatusOK, rr.Result().StatusCode) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package feedback | ||
|
||
type PostFeedbackRequest struct { | ||
Feedback string `json:"feedback" binding:"required"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
package models | ||
|
||
import "github.com/lib/pq" | ||
|
||
type User struct { | ||
Name string `json:"name,omitempty"` | ||
WalletAddress string `gorm:"primary_key" json:"walletAddress"` | ||
FlowIds []FlowId `gorm:"foreignkey:WalletAddress" json:"-"` | ||
ProfilePictureUrl string `json:"profilePictureUrl,omitempty"` | ||
Country string `json:"country,omitempty"` | ||
Name string `json:"name,omitempty"` | ||
WalletAddress string `gorm:"primary_key" json:"walletAddress"` | ||
FlowIds []FlowId `gorm:"foreignkey:WalletAddress" json:"-"` | ||
ProfilePictureUrl string `json:"profilePictureUrl,omitempty"` | ||
Country string `json:"country,omitempty"` | ||
Feedbacks pq.StringArray `json:"-" gorm:"type:text[]"` | ||
} |