Skip to content

Commit

Permalink
implemented delete tag functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirari04 committed Apr 9, 2024
1 parent 10fdbf0 commit 9abba77
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
25 changes: 25 additions & 0 deletions controllers/DeleteTagController.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package controllers

import (
"ch/kirari04/videocms/helpers"
"ch/kirari04/videocms/logic"
"ch/kirari04/videocms/models"

"github.com/labstack/echo/v4"
)

func DeleteTagController(c echo.Context) error {
// parse & validate request
var validator models.TagDeleteValidation
if status, err := helpers.Validate(c, &validator); err != nil {
return c.String(status, err.Error())
}

status, err := logic.DeleteTag(validator.TagID, validator.FileID, c.Get("UserID").(uint))

if err != nil {
return c.String(status, err.Error())
}

return c.NoContent(status)
}
39 changes: 39 additions & 0 deletions logic/DeleteTag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package logic

import (
"ch/kirari04/videocms/inits"
"ch/kirari04/videocms/models"
"errors"
"log"
"net/http"

"github.com/labstack/echo/v4"
)

func DeleteTag(tagId uint, fromLinkId uint, userId uint) (status int, err error) {
//check if requested folder exists (if set)
var link models.Link
if err := inits.DB.First(&link, fromLinkId).Error; err != nil {
return http.StatusBadRequest, errors.New("link doesn't exist")
}
if link.UserID != userId {
return http.StatusBadRequest, errors.New("link doesn't exist")
}

// check if tag exists
var tag models.Tag
if err := inits.DB.First(&tag, tagId).Error; err != nil {
return http.StatusBadRequest, errors.New("tag doesn't exist")
}

if tag.UserId != userId {
return http.StatusBadRequest, errors.New("tag doesn't exist")
}

if err := inits.DB.Model(&link).Association("Tags").Delete(&tag); err != nil {
log.Printf("Error removing tag: %v", err)
return http.StatusInternalServerError, echo.ErrInternalServerError
}

return http.StatusOK, nil
}
5 changes: 5 additions & 0 deletions models/Tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ type TagCreateValidation struct {
Name string `validate:"required,min=1,max=120" json:"Name" form:"Name"`
FileID uint `validate:"required,number" json:"FileID" form:"FileID"`
}

type TagDeleteValidation struct {
TagID uint `validate:"required,number" json:"TagID" form:"TagID"`
FileID uint `validate:"required,number" json:"FileID" form:"FileID"`
}
1 change: 1 addition & 0 deletions routes/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func Api() {
protectedApi.GET("/files", controllers.ListFiles)
protectedApi.DELETE("/files", controllers.DeleteFilesController)
protectedApi.POST("/file/tag", controllers.CreateTagController)
protectedApi.DELETE("/file/tag", controllers.DeleteTagController)

protectedApi.GET("/account", controllers.GetAccount)
protectedApi.GET("/account/settings", controllers.GetUserSettingsController)
Expand Down

0 comments on commit 9abba77

Please sign in to comment.