diff --git a/controllers/DeleteTagController.go b/controllers/DeleteTagController.go new file mode 100755 index 0000000..c0cf333 --- /dev/null +++ b/controllers/DeleteTagController.go @@ -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) +} diff --git a/logic/DeleteTag.go b/logic/DeleteTag.go new file mode 100755 index 0000000..2c1762d --- /dev/null +++ b/logic/DeleteTag.go @@ -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 +} diff --git a/models/Tag.go b/models/Tag.go index 8dc8525..b16eaf7 100644 --- a/models/Tag.go +++ b/models/Tag.go @@ -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"` +} diff --git a/routes/api.go b/routes/api.go index 09fa0c7..46591ef 100755 --- a/routes/api.go +++ b/routes/api.go @@ -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)