-
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.
- Loading branch information
Showing
6 changed files
with
84 additions
and
52 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 CreateTagController(c echo.Context) error { | ||
// parse & validate request | ||
var validator models.TagCreateValidation | ||
if status, err := helpers.Validate(c, &validator); err != nil { | ||
return c.String(status, err.Error()) | ||
} | ||
|
||
status, dbTag, err := logic.CreateTag(validator.Name, validator.FileId, c.Get("UserID").(uint)) | ||
|
||
if err != nil { | ||
return c.String(status, err.Error()) | ||
} | ||
|
||
return c.JSON(status, dbTag) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package logic | ||
|
||
import ( | ||
"ch/kirari04/videocms/inits" | ||
"ch/kirari04/videocms/models" | ||
"errors" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
func CreateTag(tagName string, toLinkId uint, userId uint) (status int, newTag *models.Tag, err error) { | ||
//check if requested folder exists (if set) | ||
var link models.Link | ||
if err := inits.DB.First(&link, toLinkId).Error; err != nil { | ||
return http.StatusBadRequest, nil, errors.New("link doesn't exist") | ||
} | ||
if link.UserID != userId { | ||
return http.StatusBadRequest, nil, errors.New("link doesn't exist") | ||
} | ||
|
||
// check if tag already exists else create new | ||
var tag models.Tag | ||
if err := inits.DB.Where(&models.Tag{Name: tagName, UserId: userId}).First(&tag).Error; err != nil { | ||
tag = models.Tag{Name: tagName, UserId: userId} | ||
if err := inits.DB.Create(&tag).Error; err != nil { | ||
return http.StatusBadRequest, nil, errors.New("failed to create new tag") | ||
} | ||
} | ||
|
||
if err := inits.DB.Model(&link).Association("Tags").Append(&tag); err != nil { | ||
log.Printf("Error adding new tag: %v", err) | ||
return http.StatusInternalServerError, nil, echo.ErrInternalServerError | ||
} | ||
|
||
return http.StatusOK, &tag, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package models | ||
|
||
type Tag struct { | ||
Model | ||
Name string `gorm:"size:128;"` | ||
Links []Link `gorm:"many2many:links_tags;"` | ||
UserId uint `gorm:"index"` | ||
User User | ||
} | ||
|
||
type TagCreateValidation struct { | ||
Name string `validate:"required,min=1,max=120"` | ||
FileId uint `validate:"required,number"` | ||
} |
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