tags: Rename getTag to getTagByNameAndColor

and formatted code, updated some logs
This commit is contained in:
IRHM
2024-09-16 23:58:13 +01:00
parent 09ac804730
commit fe7730dbcc
2 changed files with 14 additions and 10 deletions
+7 -7
View File
@@ -236,24 +236,24 @@ func successfulImport(db *gorm.DB, userId uint, contentId int, contentType Conte
for _, v := range ar.Tags {
// Check if tag exists
var t Tag
t, err := getTag(db, userId, v.Name, v.Color, v.BgColor)
t, err := getTagByNameAndColor(db, userId, v.Name, v.Color, v.BgColor)
if err != nil && err.Error() != "tag does not exist" {
slog.Error("successfulImport: Failed to get tags", "error", err)
slog.Error("successfulImport: Failed to check for an existing tag", "name", v.Name, "error", err)
continue
}
if t.ID == 0 {
tag, err := addTag(db, userId, TagAddRequest{
Name: v.Name,
Color: v.Color,
BgColor: v.BgColor,
Name: v.Name,
Color: v.Color,
BgColor: v.BgColor,
})
if err != nil {
slog.Error("successfulImport: Failed to add tag.", "error", err)
slog.Error("successfulImport: Failed to add a tag.", "name", v.Name, "error", err)
continue
}
t = tag
}
// Associate the watched entry with the tag
err = addWatchedToTag(db, userId, t.ID, w.ID)
if err != nil {
+7 -3
View File
@@ -54,15 +54,19 @@ func getTags(db *gorm.DB, userId uint) ([]Tag, error) {
// return *tag, nil
// }
func getTag(db *gorm.DB, userId uint, tagName string, tagColor string, tagBgColor string) (Tag, error) {
// This method should only be used when we don't have the tagId
// (eg: when we are importing data) because this is not technically
// reliable, since users can have multiple tags with the same name/colors
// (realistically they probably won't, but...).
func getTagByNameAndColor(db *gorm.DB, userId uint, tagName string, tagColor string, tagBgColor string) (Tag, error) {
tag := new(Tag)
res := db.Model(&Tag{}).Where("name = ? AND user_id = ? AND color = ? AND bg_color = ?", tagName, userId, tagColor, tagBgColor).Preload("Watched").Find(&tag)
if res.Error != nil {
slog.Error("getTag: Failed getting tag from database", "error", res.Error.Error())
slog.Error("getTagByNameAndColor: Failed getting tag from database", "error", res.Error.Error())
return Tag{}, errors.New("failed getting tag")
}
if tag.ID == 0 {
slog.Error("getTag: Tag does not exist for this user.", "user_id", userId)
slog.Error("getTagByNameAndColor: Tag does not exist for this user.", "user_id", userId)
return Tag{}, errors.New("tag does not exist")
}
return *tag, nil