Skip to content

Commit

Permalink
feat: add a new label crud and test
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviaseds committed Feb 8, 2024
1 parent c2a8fc4 commit 9900455
Show file tree
Hide file tree
Showing 10 changed files with 334 additions and 5 deletions.
62 changes: 62 additions & 0 deletions backend/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,40 @@ const docTemplate = `{
}
}
},
"/labels/new": {
"post": {
"description": "create a new label for a group",
"tags": [
"labels"
],
"summary": "Create A New Label",
"parameters": [
{
"description": "Label creation data",
"name": "_",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/labels.LabelCreation"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.Label"
}
},
"400": {
"description": "Bad Request",
"schema": {
"type": "string"
}
}
}
}
},
"/medications": {
"get": {
"description": "get all user medications",
Expand Down Expand Up @@ -249,6 +283,20 @@ const docTemplate = `{
}
},
"definitions": {
"labels.LabelCreation": {
"type": "object",
"properties": {
"group_id": {
"type": "integer"
},
"label_color": {
"type": "string"
},
"label_name": {
"type": "string"
}
}
},
"models.File": {
"type": "object",
"properties": {
Expand All @@ -275,6 +323,20 @@ const docTemplate = `{
}
}
},
"models.Label": {
"type": "object",
"properties": {
"group_id": {
"type": "integer"
},
"label_color": {
"type": "string"
},
"label_name": {
"type": "string"
}
}
},
"models.Medication": {
"type": "object",
"properties": {
Expand Down
62 changes: 62 additions & 0 deletions backend/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,40 @@
}
}
},
"/labels/new": {
"post": {
"description": "create a new label for a group",
"tags": [
"labels"
],
"summary": "Create A New Label",
"parameters": [
{
"description": "Label creation data",
"name": "_",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/labels.LabelCreation"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.Label"
}
},
"400": {
"description": "Bad Request",
"schema": {
"type": "string"
}
}
}
}
},
"/medications": {
"get": {
"description": "get all user medications",
Expand Down Expand Up @@ -242,6 +276,20 @@
}
},
"definitions": {
"labels.LabelCreation": {
"type": "object",
"properties": {
"group_id": {
"type": "integer"
},
"label_color": {
"type": "string"
},
"label_name": {
"type": "string"
}
}
},
"models.File": {
"type": "object",
"properties": {
Expand All @@ -268,6 +316,20 @@
}
}
},
"models.Label": {
"type": "object",
"properties": {
"group_id": {
"type": "integer"
},
"label_color": {
"type": "string"
},
"label_name": {
"type": "string"
}
}
},
"models.Medication": {
"type": "object",
"properties": {
Expand Down
40 changes: 40 additions & 0 deletions backend/docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
basePath: /
definitions:
labels.LabelCreation:
properties:
group_id:
type: integer
label_color:
type: string
label_name:
type: string
type: object
models.File:
properties:
file_id:
Expand All @@ -17,6 +26,15 @@ definitions:
upload_date:
type: string
type: object
models.Label:
properties:
group_id:
type: integer
label_color:
type: string
label_name:
type: string
type: object
models.Medication:
properties:
medication_id:
Expand Down Expand Up @@ -102,6 +120,28 @@ paths:
summary: Upload a file
tags:
- file
/labels/new:
post:
description: create a new label for a group
parameters:
- description: Label creation data
in: body
name: _
required: true
schema:
$ref: '#/definitions/labels.LabelCreation'
responses:
"200":
description: OK
schema:
$ref: '#/definitions/models.Label'
"400":
description: Bad Request
schema:
type: string
summary: Create A New Label
tags:
- labels
/medications:
get:
description: get all user medications
Expand Down
4 changes: 3 additions & 1 deletion backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"carewallet/db"
_ "carewallet/docs"
"carewallet/schema/files"
"carewallet/schema/labels"
"carewallet/schema/medication"
"carewallet/schema/tasks"
"fmt"
Expand Down Expand Up @@ -39,8 +40,9 @@ func main() {
v1 := r.Group("/")
{
medication.GetMedicationGroup(v1, &medication.PgModel{Conn: conn})
files.GetFileGroup(v1, &files.PgModel{Conn: conn})
files.FileGroup(v1, &files.PgModel{Conn: conn})
tasks.TaskGroup(v1, &tasks.PgModel{Conn: conn})
labels.LabelGroup(v1, &labels.PgModel{Conn: conn})
}

if enviroment == configuration.EnvironmentLocal {
Expand Down
7 changes: 7 additions & 0 deletions backend/models/label.go
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"`
}
2 changes: 1 addition & 1 deletion backend/schema/files/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type PgModel struct {
Conn *pgx.Conn
}

func GetFileGroup(v1 *gin.RouterGroup, c *PgModel) *gin.RouterGroup {
func FileGroup(v1 *gin.RouterGroup, c *PgModel) *gin.RouterGroup {

files := v1.Group("files")
{
Expand Down
77 changes: 77 additions & 0 deletions backend/schema/labels/label_test.go
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")
}
})
}
56 changes: 56 additions & 0 deletions backend/schema/labels/routes.go
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)
}
Loading

0 comments on commit 9900455

Please sign in to comment.