Compare commits

..

No commits in common. "615a6b1a25e8eddeabdfbd915a53adc256fa74a7" and "4fa936d774bc773d12e8cb36e4216cf6d1194517" have entirely different histories.

12 changed files with 123 additions and 373 deletions

View File

@ -4,7 +4,7 @@ import (
"context"
"errors"
otterError "git.dragse.it/anthrove/otter-space-sdk/pkg/error"
errors2 "git.dragse.it/anthrove/otter-space-sdk/pkg/errors"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
log "github.com/sirupsen/logrus"
@ -14,19 +14,19 @@ import (
func CreatePost(ctx context.Context, db *gorm.DB, anthrovePost *models.Post) error {
if anthrovePost == nil {
return &otterError.EntityValidationFailed{Reason: "anthrovePost is nil"}
return &errors2.EntityValidationFailed{Reason: "anthrovePost is nil"}
}
result := db.WithContext(ctx).Create(&anthrovePost)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.EntityAlreadyExists{}
return &errors2.EntityAlreadyExists{}
}
return result.Error
}
if result.RowsAffected == 0 {
return &otterError.NoDataWritten{}
return &errors2.NoDataWritten{}
}
log.WithFields(log.Fields{
@ -40,18 +40,18 @@ func CreatePost(ctx context.Context, db *gorm.DB, anthrovePost *models.Post) err
func GetPostByAnthroveID(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID) (*models.Post, error) {
if anthrovePostID == "" {
return nil, &otterError.EntityValidationFailed{Reason: "anthrovePostID is not set"}
return nil, &errors2.EntityValidationFailed{Reason: "anthrovePostID is not set"}
}
if len(anthrovePostID) != 25 {
return nil, &otterError.EntityValidationFailed{Reason: "anthrovePostID needs to be 25 characters long"}
return nil, &errors2.EntityValidationFailed{Reason: "anthrovePostID needs to be 25 characters long"}
}
var post models.Post
result := db.WithContext(ctx).First(&post, "id = ?", anthrovePostID)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
return nil, &errors2.NoDataFound{}
}
return nil, result.Error
}
@ -62,7 +62,7 @@ func GetPostByAnthroveID(ctx context.Context, db *gorm.DB, anthrovePostID models
func GetPostByURL(ctx context.Context, db *gorm.DB, sourceURL string) (*models.Post, error) {
if sourceURL == "" {
return nil, &otterError.EntityValidationFailed{Reason: "sourceURL is not set"}
return nil, &errors2.EntityValidationFailed{Reason: "sourceURL is not set"}
}
var post models.Post
@ -70,7 +70,7 @@ func GetPostByURL(ctx context.Context, db *gorm.DB, sourceURL string) (*models.P
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
return nil, &errors2.NoDataFound{}
}
return nil, result.Error
}
@ -81,7 +81,7 @@ func GetPostByURL(ctx context.Context, db *gorm.DB, sourceURL string) (*models.P
func GetPostBySourceID(ctx context.Context, db *gorm.DB, sourceID models.AnthroveSourceID) (*models.Post, error) {
if sourceID == "" {
return nil, &otterError.EntityValidationFailed{Reason: "sourceID is not set"}
return nil, &errors2.EntityValidationFailed{Reason: "sourceID is not set"}
}
var post models.Post
@ -89,7 +89,7 @@ func GetPostBySourceID(ctx context.Context, db *gorm.DB, sourceID models.Anthrov
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
return nil, &errors2.NoDataFound{}
}
return nil, result.Error
}

View File

@ -63,15 +63,6 @@ func TestCreateAnthrovePostNode(t *testing.T) {
},
wantErr: true,
},
{
name: "Test 3: Nill",
args: args{
ctx: context.Background(),
db: gormDB,
anthrovePost: nil,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

View File

@ -4,7 +4,7 @@ import (
"context"
"errors"
otterError "git.dragse.it/anthrove/otter-space-sdk/pkg/error"
errors2 "git.dragse.it/anthrove/otter-space-sdk/pkg/errors"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
@ -14,22 +14,22 @@ func CreateReferenceBetweenPostAndSource(ctx context.Context, db *gorm.DB, anthr
var source models.Source
if anthrovePostID == "" {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty}
}
if len(anthrovePostID) != 25 {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort}
}
if sourceDomain == "" {
return &otterError.EntityValidationFailed{Reason: "sourceDomain cannot be empty"}
return &errors2.EntityValidationFailed{Reason: "sourceDomain cannot be empty"}
}
// Find the source
result := db.WithContext(ctx).Where("domain = ?", sourceDomain).First(&source)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &otterError.NoDataFound{}
return &errors2.NoDataFound{}
}
return result.Error
}
@ -43,13 +43,13 @@ func CreateReferenceBetweenPostAndSource(ctx context.Context, db *gorm.DB, anthr
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.EntityAlreadyExists{}
return &errors2.EntityAlreadyExists{}
}
return result.Error
}
if result.RowsAffected == 0 {
return &otterError.NoDataWritten{}
return &errors2.NoDataWritten{}
}
log.WithFields(log.Fields{
@ -63,15 +63,15 @@ 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 &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty}
}
if len(anthrovePostID) != 25 {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort}
}
if anthroveUserID == "" {
return &otterError.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
return &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
}
userFavorite := models.UserFavorites{
@ -82,13 +82,13 @@ func CreateReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthrov
result := db.WithContext(ctx).Create(&userFavorite)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.EntityAlreadyExists{}
return &errors2.EntityAlreadyExists{}
}
return result.Error
}
if result.RowsAffected == 0 {
return &otterError.NoDataWritten{}
return &errors2.NoDataWritten{}
}
log.WithFields(log.Fields{
@ -103,25 +103,25 @@ func CheckReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthrove
var count int64
if anthrovePostID == "" {
return false, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
return false, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty}
}
if len(anthrovePostID) != 25 {
return false, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
return false, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort}
}
if anthroveUserID == "" {
return false, &otterError.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
return false, &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
}
if len(anthroveUserID) != 25 {
return false, &otterError.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"}
return false, &errors2.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"}
}
result := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ? AND post_id = ?", string(anthroveUserID), string(anthrovePostID)).Count(&count)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return false, &otterError.NoDataFound{}
return false, &errors2.NoDataFound{}
}
return false, result.Error
}

View File

@ -3,11 +3,10 @@ package postgres
import (
"context"
"fmt"
"testing"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"git.dragse.it/anthrove/otter-space-sdk/test"
"gorm.io/gorm"
"testing"
)
func TestCheckUserToPostLink(t *testing.T) {
@ -106,28 +105,6 @@ func TestCheckUserToPostLink(t *testing.T) {
want: false,
wantErr: true,
},
{
name: "Test 5: No AnthrovePostID given",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "",
anthrovePostID: "123456",
},
want: false,
wantErr: true,
},
{
name: "Test 6: No anthrovePostID given",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: invalidUserID,
anthrovePostID: "",
},
want: false,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@ -143,8 +120,7 @@ func TestCheckUserToPostLink(t *testing.T) {
}
}
func TestCheckUserToPostLinkWithNoData(t *testing.T) {
func TestEstablishAnthrovePostToSourceLink(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
@ -154,20 +130,9 @@ func TestCheckUserToPostLinkWithNoData(t *testing.T) {
defer container.Terminate(ctx)
// Setup Test
validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
invalidUserID := models.AnthroveUserID("XXX")
validPostID := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1"))
err = CreateUser(ctx, gormDB, validUserID)
if err != nil {
t.Fatal(err)
}
post := &models.Post{
BaseModel: models.BaseModel[models.AnthrovePostID]{
ID: validPostID,
ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")),
},
Rating: "safe",
}
@ -177,100 +142,74 @@ func TestCheckUserToPostLinkWithNoData(t *testing.T) {
t.Fatal(err)
}
err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validUserID, post.ID)
source := &models.Source{
DisplayName: "e621",
Domain: "e621.net",
Icon: "icon.e621.net",
}
err = CreateSource(ctx, gormDB, source)
if err != nil {
t.Fatal(err)
}
// Test
type args struct {
ctx context.Context
db *gorm.DB
anthroveUserID models.AnthroveUserID
anthrovePostID models.AnthrovePostID
ctx context.Context
db *gorm.DB
anthrovePostID models.AnthrovePostID
sourceDomain models.AnthroveSourceDomain
anthrovePostRelationship *models.PostReference
}
tests := []struct {
name string
args args
want bool
wantErr bool
}{
{
name: "Test 1: Valid AnthroveUserID and AnthrovePostID",
name: "Test 1: Valid AnthrovePostID and anthroveSourceDomain",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: validUserID,
anthrovePostID: post.ID,
sourceDomain: "e621.net",
},
want: true,
wantErr: false,
},
{
name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID",
name: "Test 2: Invalid AnthrovePostID and Valid anthroveSourceDomain",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: validUserID,
anthrovePostID: "qadw",
},
want: false,
wantErr: true,
},
{
name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: invalidUserID,
anthrovePostID: post.ID,
},
want: false,
wantErr: true,
},
{
name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: invalidUserID,
anthrovePostID: "123456",
sourceDomain: "e621.net",
},
want: false,
wantErr: true,
},
{
name: "Test 5: No AnthrovePostID given",
name: "Test 3: Invalid anthroveSourceDomain and Valid AnthrovePostID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "",
anthrovePostID: "123456",
anthrovePostID: "1234",
sourceDomain: "fa.banana",
},
want: false,
wantErr: true,
},
{
name: "Test 6: No anthrovePostID given",
name: "Test 4: Invalid anthroveSourceDomain and Invalid AnthrovePostID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: invalidUserID,
anthrovePostID: "",
anthrovePostID: "696969",
sourceDomain: "hehe.funny.number",
},
want: false,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CheckReferenceBetweenUserAndPost(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID)
if (err != nil) != tt.wantErr {
t.Errorf("CheckReferenceBetweenUserAndPost() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("CheckReferenceBetweenUserAndPost() got = %v, want %v", got, tt.want)
if err := CreateReferenceBetweenPostAndSource(tt.args.ctx, tt.args.db, tt.args.anthrovePostID, tt.args.sourceDomain); (err != nil) != tt.wantErr {
t.Errorf("CreateReferenceBetweenPostAndSource() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
@ -361,26 +300,6 @@ func TestEstablishUserToPostLink(t *testing.T) {
},
wantErr: true,
},
{
name: "Test 5: AnthrovePostID is empty",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: invalidUserID,
anthrovePostID: "",
},
wantErr: true,
},
{
name: "Test 6: anthroveUserID is empty",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "",
anthrovePostID: validPostID,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

View File

@ -4,7 +4,7 @@ import (
"context"
"errors"
otterError "git.dragse.it/anthrove/otter-space-sdk/pkg/error"
errors2 "git.dragse.it/anthrove/otter-space-sdk/pkg/errors"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
log "github.com/sirupsen/logrus"
@ -15,20 +15,20 @@ import (
func CreateSource(ctx context.Context, db *gorm.DB, anthroveSource *models.Source) error {
if anthroveSource.Domain == "" {
return &otterError.EntityValidationFailed{Reason: "Domain is required"}
return &errors2.EntityValidationFailed{Reason: "Domain is required"}
}
result := db.WithContext(ctx).Where(models.Source{Domain: anthroveSource.Domain}).FirstOrCreate(anthroveSource)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.EntityAlreadyExists{}
return &errors2.EntityAlreadyExists{}
}
return result.Error
}
if result.RowsAffected == 0 {
return &otterError.NoDataWritten{}
return &errors2.NoDataWritten{}
}
log.WithFields(log.Fields{
@ -48,7 +48,7 @@ func GetAllSource(ctx context.Context, db *gorm.DB) ([]models.Source, error) {
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
return nil, &errors2.NoDataFound{}
}
return nil, result.Error
}
@ -65,14 +65,14 @@ func GetSourceByDomain(ctx context.Context, db *gorm.DB, sourceDomain models.Ant
var sources models.Source
if sourceDomain == "" {
return nil, &otterError.EntityValidationFailed{Reason: "AnthroveSourceDomain is not set"}
return nil, &errors2.EntityValidationFailed{Reason: "AnthroveSourceDomain is not set"}
}
result := db.WithContext(ctx).Where("domain = ?", sourceDomain).First(&sources)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
return nil, &errors2.NoDataFound{}
}
return nil, result.Error
}

View File

@ -3,11 +3,10 @@ package postgres
import (
"context"
"fmt"
"testing"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"git.dragse.it/anthrove/otter-space-sdk/test"
"gorm.io/gorm"
"testing"
)
func TestCreateSourceNode(t *testing.T) {
@ -31,13 +30,7 @@ func TestCreateSourceNode(t *testing.T) {
}
invalidSource := &models.Source{
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validPostID},
Domain: "notfound.intern",
}
invalidSourceDomain := &models.Source{
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validPostID},
Domain: "",
Domain: "",
}
// Test
@ -65,7 +58,7 @@ func TestCreateSourceNode(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
anthroveSource: invalidSourceDomain,
anthroveSource: invalidSource,
},
wantErr: true,
},
@ -74,7 +67,7 @@ func TestCreateSourceNode(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
anthroveSource: invalidSource,
anthroveSource: validSource,
},
wantErr: true,
},

View File

@ -4,7 +4,7 @@ import (
"context"
"errors"
otterError "git.dragse.it/anthrove/otter-space-sdk/pkg/error"
errors2 "git.dragse.it/anthrove/otter-space-sdk/pkg/errors"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
@ -13,19 +13,19 @@ import (
func CreateTag(ctx context.Context, db *gorm.DB, tag *models.Tag) error {
if tag == nil {
return &otterError.EntityValidationFailed{Reason: "Tag is nil"}
return &errors2.EntityValidationFailed{Reason: "Tag is nil"}
}
result := db.WithContext(ctx).Create(tag)
result := db.WithContext(ctx).Where(tag).Create(tag)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.EntityAlreadyExists{}
return &errors2.EntityAlreadyExists{}
}
return result.Error
}
if result.RowsAffected == 0 {
return &otterError.NoDataWritten{}
return &errors2.NoDataWritten{}
}
log.WithFields(log.Fields{
@ -39,15 +39,15 @@ func CreateTag(ctx context.Context, db *gorm.DB, tag *models.Tag) error {
func CreateTagAndReferenceToPost(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, tag *models.Tag) error {
if anthrovePostID == "" {
return &otterError.EntityValidationFailed{Reason: "anthrovePostID cannot be empty"}
return &errors2.EntityValidationFailed{Reason: "anthrovePostID cannot be empty"}
}
if len(anthrovePostID) != 25 {
return &otterError.EntityValidationFailed{Reason: "anthrovePostID needs to be 25 characters long"}
return &errors2.EntityValidationFailed{Reason: "anthrovePostID needs to be 25 characters long"}
}
if tag == nil {
return &otterError.EntityValidationFailed{Reason: "Tag is nil"}
return &errors2.EntityValidationFailed{Reason: "Tag is nil"}
}
pgPost := models.Post{
@ -59,9 +59,9 @@ func CreateTagAndReferenceToPost(ctx context.Context, db *gorm.DB, anthrovePostI
err := db.WithContext(ctx).Model(&pgPost).Association("Tags").Append(tag)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return &otterError.NoDataFound{}
return &errors2.NoDataFound{}
}
return errors.Join(err, &otterError.NoRelationCreated{})
return errors.Join(err, &errors2.NoRelationCreated{})
}
log.WithFields(log.Fields{
@ -77,9 +77,10 @@ func GetTags(ctx context.Context, db *gorm.DB) ([]models.Tag, error) {
var tags []models.Tag
result := db.WithContext(ctx).Find(&tags)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
return nil, &errors2.NoDataFound{}
}
return nil, result.Error
}

View File

@ -3,11 +3,10 @@ package postgres
import (
"context"
"fmt"
"testing"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"git.dragse.it/anthrove/otter-space-sdk/test"
"gorm.io/gorm"
"testing"
)
func TestCreateTagNodeWitRelation(t *testing.T) {
@ -55,7 +54,7 @@ func TestCreateTagNodeWitRelation(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
PostID: post.ID,
PostID: models.AnthrovePostID(post.ID),
tag: tag,
},
wantErr: false,
@ -65,7 +64,7 @@ func TestCreateTagNodeWitRelation(t *testing.T) {
args: args{
ctx: ctx,
db: gormDB,
PostID: post.ID,
PostID: models.AnthrovePostID(post.ID),
tag: nil,
},
wantErr: true,
@ -181,68 +180,3 @@ func checkTag(got []models.Tag, want []models.Tag) bool {
return true
}
func TestCreateTag(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
t.Fatalf("Could not start PostgreSQL container: %v", err)
}
defer container.Terminate(ctx)
// Setup Test
validTag := models.Tag{
Name: "JayTheFerret",
Type: "artist",
}
invalidTag := models.Tag{}
// Test
type args struct {
ctx context.Context
db *gorm.DB
tag *models.Tag
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 1: Valid Tag",
args: args{
ctx: ctx,
db: gormDB,
tag: &validTag,
},
wantErr: false,
},
{
name: "Test 2: Duplicate Tag",
args: args{
ctx: ctx,
db: gormDB,
tag: &validTag,
},
wantErr: true,
},
{
name: "Test 3: Invalid Tag",
args: args{
ctx: ctx,
db: gormDB,
tag: &invalidTag,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CreateTag(tt.args.ctx, tt.args.db, tt.args.tag); (err != nil) != tt.wantErr {
t.Errorf("CreateTag() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

View File

@ -4,7 +4,7 @@ import (
"context"
"errors"
otterError "git.dragse.it/anthrove/otter-space-sdk/pkg/error"
errors2 "git.dragse.it/anthrove/otter-space-sdk/pkg/errors"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
@ -13,11 +13,11 @@ import (
func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) error {
if anthroveUserID == "" {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty}
}
if len(anthroveUserID) != 25 {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort}
}
user := models.User{
@ -28,9 +28,8 @@ func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.Anthrove
result := db.WithContext(ctx).FirstOrCreate(&user)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.EntityAlreadyExists{}
return &errors2.EntityAlreadyExists{}
}
return result.Error
}
@ -41,19 +40,19 @@ 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 &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty}
}
if len(anthroveUserID) != 25 {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
return &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort}
}
if accountId == "" {
return &otterError.EntityValidationFailed{Reason: "accountID cannot be empty"}
return &errors2.EntityValidationFailed{Reason: "accountID cannot be empty"}
}
if accountUsername == "" {
return &otterError.EntityValidationFailed{Reason: "accountUsername cannot be empty"}
return &errors2.EntityValidationFailed{Reason: "accountUsername cannot be empty"}
}
err := CreateUser(ctx, db, anthroveUserID)
@ -68,7 +67,7 @@ func CreateUserWithRelationToSource(ctx context.Context, db *gorm.DB, anthroveUs
result := db.WithContext(ctx).Where(&source).First(&source)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &otterError.NoDataFound{}
return &errors2.NoDataFound{}
}
return result.Error
}
@ -83,11 +82,11 @@ func CreateUserWithRelationToSource(ctx context.Context, db *gorm.DB, anthroveUs
result = db.WithContext(ctx).FirstOrCreate(&userSource)
if result.Error != nil {
return errors.Join(result.Error, &otterError.NoRelationCreated{})
return errors.Join(result.Error, &errors2.NoRelationCreated{})
}
if result.RowsAffected == 0 {
return &otterError.NoDataWritten{}
return &errors2.NoDataWritten{}
}
log.WithFields(log.Fields{
@ -104,17 +103,17 @@ func GetUserFavoritesCount(ctx context.Context, db *gorm.DB, anthroveUserID mode
var count int64
if anthroveUserID == "" {
return 0, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
return 0, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty}
}
if len(anthroveUserID) != 25 {
return 0, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
return 0, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort}
}
result := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ?", string(anthroveUserID)).Count(&count)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return 0, &otterError.NoDataFound{}
return 0, &errors2.NoDataFound{}
}
return 0, result.Error
}
@ -132,17 +131,17 @@ func GetUserSourceLinks(ctx context.Context, db *gorm.DB, anthroveUserID models.
userSourceMap := make(map[string]models.UserSource)
if anthroveUserID == "" {
return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
return nil, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty}
}
if len(anthroveUserID) != 25 {
return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
return nil, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort}
}
result := db.WithContext(ctx).Model(&models.UserSource{}).Where("user_id = ?", string(anthroveUserID)).Find(&userSources)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
return nil, &errors2.NoDataFound{}
}
return nil, result.Error
}
@ -152,7 +151,7 @@ func GetUserSourceLinks(ctx context.Context, db *gorm.DB, anthroveUserID models.
result = db.WithContext(ctx).Model(&models.Source{}).Where("id = ?", userSource.SourceID).First(&source)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
return nil, &errors2.NoDataFound{}
}
return nil, result.Error
}
@ -180,25 +179,25 @@ func GetUserSourceBySourceID(ctx context.Context, db *gorm.DB, anthroveUserID mo
userSourceMap := make(map[string]models.UserSource)
if anthroveUserID == "" {
return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
return nil, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty}
}
if len(anthroveUserID) != 25 {
return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
return nil, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort}
}
if sourceID == "" {
return nil, &otterError.EntityValidationFailed{Reason: "sourceID cannot be empty"}
return nil, &errors2.EntityValidationFailed{Reason: "sourceID cannot be empty"}
}
if len(sourceID) != 25 {
return nil, &otterError.EntityValidationFailed{Reason: "sourceID needs to be 25 characters long"}
return nil, &errors2.EntityValidationFailed{Reason: "sourceID needs to be 25 characters long"}
}
result := db.WithContext(ctx).Model(&models.UserSource{}).InnerJoins("Source", db.Where("id = ?", sourceID)).Where("user_id = ?", string(anthroveUserID)).First(&userSources)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
return nil, &errors2.NoDataFound{}
}
return nil, result.Error
}
@ -208,7 +207,7 @@ func GetUserSourceBySourceID(ctx context.Context, db *gorm.DB, anthroveUserID mo
result = db.WithContext(ctx).Model(&models.Source{}).Where("id = ?", userSource.SourceID).First(&source)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
return nil, &errors2.NoDataFound{}
}
return nil, result.Error
}
@ -239,7 +238,7 @@ func GetAllAnthroveUserIDs(ctx context.Context, db *gorm.DB) ([]models.AnthroveU
result := db.WithContext(ctx).Model(&models.User{}).Find(&users)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
return nil, &errors2.NoDataFound{}
}
return nil, result.Error
}
@ -260,17 +259,17 @@ func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUse
var favoritePosts []models.Post
if anthroveUserID == "" {
return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
return nil, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty}
}
if len(anthroveUserID) != 25 {
return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
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
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
return nil, &errors2.NoDataFound{}
}
return nil, err
}
@ -280,7 +279,7 @@ func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUse
err = db.WithContext(ctx).Model(&models.Post{}).Where("id = ?", userFavorite.PostID).First(&post).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
return nil, &errors2.NoDataFound{}
}
return nil, err
}
@ -304,17 +303,17 @@ func GetUserTagWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthrov
var userFavorites []models.UserFavorites
if anthroveUserID == "" {
return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
return nil, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDIsEmpty}
}
if len(anthroveUserID) != 25 {
return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
return nil, &errors2.EntityValidationFailed{Reason: errors2.AnthroveUserIDToShort}
}
result := db.WithContext(ctx).Where("user_id = ?", string(anthroveUserID)).Find(&userFavorites)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
return nil, &errors2.NoDataFound{}
}
return nil, result.Error
}
@ -326,13 +325,10 @@ func GetUserTagWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthrov
for _, userFavorite := range userFavorites {
var post models.Post
result = db.WithContext(ctx).Preload("Tags", func(db *gorm.DB) *gorm.DB {
return db.Order("tag_type ASC")
}).First(&post, "id = ?", userFavorite.PostID)
result = db.WithContext(ctx).Preload("Tags").First(&post, "id = ?", userFavorite.PostID)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
return nil, &errors2.NoDataFound{}
}
return nil, result.Error
}

View File

@ -54,15 +54,6 @@ func TestCreateUser(t *testing.T) {
},
wantErr: true,
},
{
name: "Test 3: No anthroveUserID given",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@ -356,7 +347,7 @@ func TestGetUserSourceBySourceID(t *testing.T) {
wantErr: true,
},
{
name: "Test 5: Valid AnthroveUserID and No anthroveUserID",
name: "Test 5: Valid AnthroveUserID and No SourceDisplayName",
args: args{
ctx: ctx,
db: gormDB,
@ -367,7 +358,7 @@ func TestGetUserSourceBySourceID(t *testing.T) {
wantErr: true,
},
{
name: "Test 6: No AnthroveUserID and No anthroveUserID",
name: "Test 6: No AnthroveUserID and No SourceDisplayName",
args: args{
ctx: ctx,
db: gormDB,
@ -377,17 +368,6 @@ func TestGetUserSourceBySourceID(t *testing.T) {
want: nil,
wantErr: true,
},
{
name: "Test 7: No anthroveUserID given",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: validUserID,
sourceID: "",
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@ -525,30 +505,6 @@ func TestGetUserFavoriteNodeWithPagination(t *testing.T) {
want: expectedResult3,
wantErr: false,
},
{
name: "Test 4: No anthroveUserID given",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "",
skip: 0,
limit: 3,
},
want: nil,
wantErr: true,
},
{
name: "Test 5: Short anthroveUserID given",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "aaa",
skip: 0,
limit: 3,
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@ -707,7 +663,7 @@ func TestGetUserSourceLinks(t *testing.T) {
}
err = CreateSource(ctx, gormDB, eSource)
if err != nil {
t.Fatal("Create Source e621:", err)
t.Fatal(err)
}
faSource := &models.Source{
@ -717,7 +673,7 @@ func TestGetUserSourceLinks(t *testing.T) {
}
err = CreateSource(ctx, gormDB, faSource)
if err != nil {
t.Fatal("Create Source fa:", err)
t.Fatal(err)
}
expectedResult := make(map[string]models.UserSource)
@ -740,11 +696,11 @@ func TestGetUserSourceLinks(t *testing.T) {
err = CreateUserWithRelationToSource(ctx, gormDB, validAnthroveUserID, eSource.ID, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername)
if err != nil {
t.Fatal("CreateUserWithRelationToSource e621:", err)
t.Fatal(err)
}
err = CreateUserWithRelationToSource(ctx, gormDB, validAnthroveUserID, faSource.ID, expectedResult["fa"].UserID, expectedResult["fa"].AccountUsername)
if err != nil {
t.Fatal("CreateUserWithRelationToSource fa:", err)
t.Fatal(err)
}
// Test
@ -769,26 +725,6 @@ func TestGetUserSourceLinks(t *testing.T) {
want: expectedResult,
wantErr: false,
},
{
name: "Test 3: No AnthroveID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "",
},
want: nil,
wantErr: true,
},
{
name: "Test 1: AnthroveID to short",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "aaa",
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@ -836,7 +772,7 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, post.ID)
err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, models.AnthrovePostID(post.ID))
if err != nil {
t.Fatal(err)
}
@ -901,26 +837,6 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) {
want: expectedResult,
wantErr: false,
},
{
name: "Test 2: No anthroveUserID given",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "",
},
want: nil,
wantErr: true,
},
{
name: "Test 3: short anthroveUserID given",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "aaa",
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

View File

@ -1,4 +1,4 @@
package error
package errors
type EntityAlreadyExists struct{}

View File

@ -1,4 +1,4 @@
package error
package errors
import "fmt"