-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented delete tag functionality
- Loading branch information
Showing
4 changed files
with
70 additions
and
0 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
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) | ||
} |
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,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 | ||
} |
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