-
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.
- Loading branch information
1 parent
c2a8fc4
commit 9900455
Showing
10 changed files
with
334 additions
and
5 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
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,7 @@ | ||
package models | ||
|
||
type Label struct { | ||
GroupID int `json:"group_id"` | ||
LabelName string `json:"label_name"` | ||
LabelColor string `json:"label_color"` | ||
} |
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,77 @@ | ||
package labels | ||
|
||
import ( | ||
"bytes" | ||
"carewallet/configuration" | ||
"carewallet/db" | ||
"carewallet/models" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/gin-contrib/cors" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func TestLabelGroup(t *testing.T) { | ||
config, err := configuration.GetConfiguration() | ||
|
||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Unable to retreive configuration file: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
conn := db.ConnectPosgresDatabase(config) | ||
defer conn.Close() | ||
|
||
controller := PgModel{Conn: conn} | ||
router := gin.Default() | ||
router.Use(cors.Default()) | ||
|
||
v1 := router.Group("/") | ||
{ | ||
LabelGroup(v1, &controller) | ||
} | ||
|
||
t.Run("TestCreateNewLabel", func(t *testing.T) { | ||
postRequest := LabelCreation{ | ||
GroupID: 1, | ||
LabelName: "Laundry", | ||
LabelColor: "Orange", | ||
} | ||
|
||
requestJSON, err := json.Marshal(postRequest) | ||
if err != nil { | ||
t.Error("Failed to marshal remove request to JSON") | ||
} | ||
|
||
w := httptest.NewRecorder() | ||
req, _ := http.NewRequest("POST", "/labels/new", bytes.NewBuffer(requestJSON)) | ||
router.ServeHTTP(w, req) | ||
|
||
if http.StatusOK != w.Code { | ||
t.Error("Failed to create new label.") | ||
} | ||
|
||
var postResponse models.Label | ||
err = json.Unmarshal(w.Body.Bytes(), &postResponse) | ||
|
||
if err != nil { | ||
t.Error("Failed to unmarshal json") | ||
} | ||
|
||
expectedResponse := models.Label{ | ||
GroupID: 1, | ||
LabelName: "Laundry", | ||
LabelColor: "Orange", | ||
} | ||
|
||
if !reflect.DeepEqual(expectedResponse, postResponse) { | ||
t.Error("Result was not correct") | ||
} | ||
}) | ||
} |
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,56 @@ | ||
package labels | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/jackc/pgx" | ||
) | ||
|
||
type PgModel struct { | ||
Conn *pgx.Conn | ||
} | ||
|
||
func LabelGroup(v1 *gin.RouterGroup, c *PgModel) *gin.RouterGroup { | ||
|
||
labels := v1.Group("labels") | ||
{ | ||
labels.POST("/new", c.CreateNewLabel) | ||
} | ||
|
||
return labels | ||
} | ||
|
||
type LabelCreation struct { | ||
GroupID int `json:"group_id"` | ||
LabelName string `json:"label_name"` | ||
LabelColor string `json:"label_color"` | ||
} | ||
|
||
// CreateNewLabel godoc | ||
// | ||
// @summary Create A New Label | ||
// @description create a new label for a group | ||
// @tags labels | ||
// | ||
// @param _ body LabelCreation true "Label creation data" | ||
// | ||
// @success 200 {object} models.Label | ||
// @failure 400 {object} string | ||
// @router /labels/new [post] | ||
func (pg *PgModel) CreateNewLabel(c *gin.Context) { | ||
var requestBody LabelCreation | ||
|
||
if err := c.BindJSON(&requestBody); err != nil { | ||
c.JSON(http.StatusBadRequest, err.Error()) | ||
return | ||
} | ||
|
||
label, err := CreateNewLabelInDB(pg.Conn, requestBody) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, err.Error()) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, label) | ||
} |
Oops, something went wrong.