Skip to content

Commit

Permalink
feat: delete 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 9900455 commit 395db6c
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 16 deletions.
43 changes: 41 additions & 2 deletions backend/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,45 @@ const docTemplate = `{
}
}
},
"/labels/delete/{:gid}/{:lname}": {
"delete": {
"description": "delete a label",
"tags": [
"labels"
],
"summary": "Delete A Label",
"parameters": [
{
"type": "string",
"description": "Group to delete label from",
"name": ":gid",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Name of label to delete",
"name": ":lname",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.Label"
}
},
"400": {
"description": "Bad Request",
"schema": {
"type": "string"
}
}
}
}
},
"/labels/new": {
"post": {
"description": "create a new label for a group",
Expand Down Expand Up @@ -204,7 +243,7 @@ const docTemplate = `{
{
"type": "string",
"description": "Task ID to assign users to",
"name": "tid",
"name": ":tid",
"in": "path",
"required": true
},
Expand Down Expand Up @@ -248,7 +287,7 @@ const docTemplate = `{
{
"type": "string",
"description": "Task ID to remove users from",
"name": "tid",
"name": ":tid",
"in": "path",
"required": true
},
Expand Down
43 changes: 41 additions & 2 deletions backend/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,45 @@
}
}
},
"/labels/delete/{:gid}/{:lname}": {
"delete": {
"description": "delete a label",
"tags": [
"labels"
],
"summary": "Delete A Label",
"parameters": [
{
"type": "string",
"description": "Group to delete label from",
"name": ":gid",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Name of label to delete",
"name": ":lname",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.Label"
}
},
"400": {
"description": "Bad Request",
"schema": {
"type": "string"
}
}
}
}
},
"/labels/new": {
"post": {
"description": "create a new label for a group",
Expand Down Expand Up @@ -197,7 +236,7 @@
{
"type": "string",
"description": "Task ID to assign users to",
"name": "tid",
"name": ":tid",
"in": "path",
"required": true
},
Expand Down Expand Up @@ -241,7 +280,7 @@
{
"type": "string",
"description": "Task ID to remove users from",
"name": "tid",
"name": ":tid",
"in": "path",
"required": true
},
Expand Down
30 changes: 28 additions & 2 deletions backend/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,32 @@ paths:
summary: Upload a file
tags:
- file
/labels/delete/{:gid}/{:lname}:
delete:
description: delete a label
parameters:
- description: Group to delete label from
in: path
name: :gid
required: true
type: string
- description: Name of label to delete
in: path
name: :lname
required: true
type: string
responses:
"200":
description: OK
schema:
$ref: '#/definitions/models.Label'
"400":
description: Bad Request
schema:
type: string
summary: Delete A Label
tags:
- labels
/labels/new:
post:
description: create a new label for a group
Expand Down Expand Up @@ -161,7 +187,7 @@ paths:
parameters:
- description: Task ID to assign users to
in: path
name: tid
name: :tid
required: true
type: string
- description: Users to assign to task and assignee
Expand Down Expand Up @@ -190,7 +216,7 @@ paths:
parameters:
- description: Task ID to remove users from
in: path
name: tid
name: :tid
required: true
type: string
- description: Users to remove from task
Expand Down
14 changes: 12 additions & 2 deletions backend/schema/labels/label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestLabelGroup(t *testing.T) {

t.Run("TestCreateNewLabel", func(t *testing.T) {
postRequest := LabelCreation{
GroupID: 1,
GroupID: 3,
LabelName: "Laundry",
LabelColor: "Orange",
}
Expand All @@ -65,7 +65,7 @@ func TestLabelGroup(t *testing.T) {
}

expectedResponse := models.Label{
GroupID: 1,
GroupID: 3,
LabelName: "Laundry",
LabelColor: "Orange",
}
Expand All @@ -74,4 +74,14 @@ func TestLabelGroup(t *testing.T) {
t.Error("Result was not correct")
}
})

t.Run("TestDeleteLabel", func(t *testing.T) {
w := httptest.NewRecorder()
req, _ := http.NewRequest("DELETE", "/labels/delete/2/Appointment", nil)
router.ServeHTTP(w, req)

if http.StatusOK != w.Code {
t.Error("Failed to delete label.")
}
})
}
26 changes: 26 additions & 0 deletions backend/schema/labels/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func LabelGroup(v1 *gin.RouterGroup, c *PgModel) *gin.RouterGroup {
labels := v1.Group("labels")
{
labels.POST("/new", c.CreateNewLabel)
labels.DELETE("/delete/:gid/:lname", c.DeleteLabel)
}

return labels
Expand Down Expand Up @@ -54,3 +55,28 @@ func (pg *PgModel) CreateNewLabel(c *gin.Context) {

c.JSON(http.StatusOK, label)
}

// DeleteLabel godoc
//
// @summary Delete A Label
// @description delete a label
// @tags labels
//
// @param :gid path string true "Group to delete label from"
// @param :lname path string true "Name of label to delete"
//
// @success 200 {object} models.Label
// @failure 400 {object} string
// @router /labels/delete/{:gid}/{:lname} [DELETE]
func (pg *PgModel) DeleteLabel(c *gin.Context) {
group_id := c.Param("gid")
label_name := c.Param("lname")

err := DeleteLabelFromDB(pg.Conn, group_id, label_name)
if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
}

c.JSON(http.StatusOK, nil)
}
16 changes: 16 additions & 0 deletions backend/schema/labels/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package labels

import (
"carewallet/models"
"strconv"

"github.com/jackc/pgx"
)
Expand All @@ -14,6 +15,7 @@ func CreateNewLabelInDB(pool *pgx.Conn, requestBody LabelCreation) (models.Label
_, err := pool.Exec("INSERT INTO label (group_id, label_name, label_color) VALUES ($1, $2, $3)", groupID, labelName, labelColor)

if err != nil {
print(err.Error())
return models.Label{}, err
}

Expand All @@ -24,3 +26,17 @@ func CreateNewLabelInDB(pool *pgx.Conn, requestBody LabelCreation) (models.Label
}
return label, nil
}

func DeleteLabelFromDB(pool *pgx.Conn, groupID string, labelName string) error {
groupIDInt, err := strconv.Atoi(groupID)
if err != nil {
return err
}

_, err = pool.Exec("DELETE FROM label WHERE group_id = $1 AND label_name = $2", groupIDInt, labelName)
if err != nil {
return err
}

return nil
}
16 changes: 8 additions & 8 deletions backend/schema/tasks/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ type Assignment struct {
// @description assign users to task
// @tags tasks
//
// @param tid path string true "Task ID to assign users to"
// @param _ body Assignment true "Users to assign to task and assignee"
// @param :tid path string true "Task ID to assign users to"
// @param _ body Assignment true "Users to assign to task and assignee"
//
// @success 200 {array} models.TaskUser
// @failure 400 {object} string
// @success 200 {array} models.TaskUser
// @failure 400 {object} string
// @router /tasks/{tid}/assign [post]
func (pg *PgModel) AssignUsersToTask(c *gin.Context) {
var requestBody Assignment
Expand Down Expand Up @@ -110,11 +110,11 @@ type Removal struct {
// @description remove users from task
// @tags tasks
//
// @param tid path string true "Task ID to remove users from"
// @param _ body Removal true "Users to remove from task"
// @param :tid path string true "Task ID to remove users from"
// @param _ body Removal true "Users to remove from task"
//
// @success 200 {array} models.TaskUser
// @failure 400 {object} string
// @success 200 {array} models.TaskUser
// @failure 400 {object} string
// @router /tasks/{tid}/remove [delete]
func (pg *PgModel) RemoveUsersFromTask(c *gin.Context) {
var requestBody Removal
Expand Down

0 comments on commit 395db6c

Please sign in to comment.