From 4fa936d774bc773d12e8cb36e4216cf6d1194517 Mon Sep 17 00:00:00 2001 From: SoXX Date: Wed, 26 Jun 2024 09:20:52 +0200 Subject: [PATCH] fix(errors): added const Signed-off-by: SoXX --- internal/postgres/relationships.go | 13 +++++++------ internal/postgres/user.go | 28 ++++++++++++++-------------- pkg/errors/validation.go | 5 +++++ 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/internal/postgres/relationships.go b/internal/postgres/relationships.go index 6be3bde..d66106b 100644 --- a/internal/postgres/relationships.go +++ b/internal/postgres/relationships.go @@ -3,6 +3,7 @@ package postgres import ( "context" "errors" + errors2 "git.dragse.it/anthrove/otter-space-sdk/pkg/errors" "git.dragse.it/anthrove/otter-space-sdk/pkg/models" log "github.com/sirupsen/logrus" @@ -13,11 +14,11 @@ func CreateReferenceBetweenPostAndSource(ctx context.Context, db *gorm.DB, anthr var source models.Source if anthrovePostID == "" { - return &errors2.EntityValidationFailed{Reason: "anthrovePostID cannot be empty"} + return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty} } if len(anthrovePostID) != 25 { - return &errors2.EntityValidationFailed{Reason: "anthrovePostID needs to be 25 characters long"} + return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort} } if sourceDomain == "" { @@ -62,11 +63,11 @@ func CreateReferenceBetweenPostAndSource(ctx context.Context, db *gorm.DB, anthr func CreateReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) error { if anthrovePostID == "" { - return &errors2.EntityValidationFailed{Reason: "anthrovePostID cannot be empty"} + return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty} } if len(anthrovePostID) != 25 { - return &errors2.EntityValidationFailed{Reason: "anthrovePostID needs to be 25 characters long"} + return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort} } if anthroveUserID == "" { @@ -102,11 +103,11 @@ func CheckReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthrove var count int64 if anthrovePostID == "" { - return false, &errors2.EntityValidationFailed{Reason: "anthrovePostID cannot be empty"} + return false, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty} } if len(anthrovePostID) != 25 { - return false, &errors2.EntityValidationFailed{Reason: "anthrovePostID needs to be 25 characters long"} + return false, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort} } if anthroveUserID == "" { diff --git a/internal/postgres/user.go b/internal/postgres/user.go index a21b266..55a1d54 100644 --- a/internal/postgres/user.go +++ b/internal/postgres/user.go @@ -13,11 +13,11 @@ import ( func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) error { if anthroveUserID == "" { - return &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"} + return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty} } if len(anthroveUserID) != 25 { - return &errors2.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"} + return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort} } user := models.User{ @@ -40,11 +40,11 @@ func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.Anthrove func CreateUserWithRelationToSource(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, accountId string, accountUsername string) error { if anthroveUserID == "" { - return &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"} + return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty} } if len(anthroveUserID) != 25 { - return &errors2.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"} + return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort} } if accountId == "" { @@ -103,11 +103,11 @@ func GetUserFavoritesCount(ctx context.Context, db *gorm.DB, anthroveUserID mode var count int64 if anthroveUserID == "" { - return 0, &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"} + return 0, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty} } if len(anthroveUserID) != 25 { - return 0, &errors2.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"} + return 0, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort} } result := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ?", string(anthroveUserID)).Count(&count) @@ -131,11 +131,11 @@ func GetUserSourceLinks(ctx context.Context, db *gorm.DB, anthroveUserID models. userSourceMap := make(map[string]models.UserSource) if anthroveUserID == "" { - return nil, &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"} + return nil, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty} } if len(anthroveUserID) != 25 { - return nil, &errors2.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"} + return nil, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort} } result := db.WithContext(ctx).Model(&models.UserSource{}).Where("user_id = ?", string(anthroveUserID)).Find(&userSources) @@ -179,11 +179,11 @@ func GetUserSourceBySourceID(ctx context.Context, db *gorm.DB, anthroveUserID mo userSourceMap := make(map[string]models.UserSource) if anthroveUserID == "" { - return nil, &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"} + return nil, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty} } if len(anthroveUserID) != 25 { - return nil, &errors2.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"} + return nil, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort} } if sourceID == "" { @@ -259,11 +259,11 @@ func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUse var favoritePosts []models.Post if anthroveUserID == "" { - return nil, &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"} + return nil, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty} } if len(anthroveUserID) != 25 { - return nil, &errors2.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"} + return nil, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort} } err := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ?", string(anthroveUserID)).Offset(skip).Limit(limit).Find(&userFavorites).Error @@ -303,11 +303,11 @@ func GetUserTagWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthrov var userFavorites []models.UserFavorites if anthroveUserID == "" { - return nil, &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"} + return nil, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty} } if len(anthroveUserID) != 25 { - return nil, &errors2.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"} + return nil, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort} } result := db.WithContext(ctx).Where("user_id = ?", string(anthroveUserID)).Find(&userFavorites) diff --git a/pkg/errors/validation.go b/pkg/errors/validation.go index a04ac75..69efe47 100644 --- a/pkg/errors/validation.go +++ b/pkg/errors/validation.go @@ -2,6 +2,11 @@ package errors import "fmt" +const ( + AnthroveUserIDIsEmpty = "anthrovePostID cannot be empty" + AnthroveUserIDToShort = "anthrovePostID needs to be 25 characters long" +) + type EntityValidationFailed struct { Reason string }