BREAKING CHANGE: V2 of thr SDK #12

Merged
fenpaws merged 124 commits from develop/postgresql into main 2024-07-01 20:46:28 +00:00
13 changed files with 395 additions and 11 deletions
Showing only changes of commit 2a752adcac - Show all commits

View File

@ -37,7 +37,7 @@ func CreateReferenceBetweenPostAndSource(ctx context.Context, db *gorm.DB, anthr
} }
func CreateReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) error { func CreateReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) error {
userFavorite := models.UserFavorite{ userFavorite := models.UserFavorites{
UserID: string(anthroveUserID), UserID: string(anthroveUserID),
PostID: string(anthrovePostID), PostID: string(anthrovePostID),
} }
@ -57,7 +57,7 @@ func CreateReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthrov
func CheckReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) (bool, error) { func CheckReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) (bool, error) {
var count int64 var count int64
err := db.WithContext(ctx).Model(&models.UserFavorite{}).Where("user_id = ? AND post_id = ?", string(anthroveUserID), string(anthrovePostID)).Count(&count).Error err := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ? AND post_id = ?", string(anthroveUserID), string(anthrovePostID)).Count(&count).Error
if err != nil { if err != nil {
return false, err return false, err
} }

View File

@ -95,7 +95,7 @@ func GetUserFavoritesCount(ctx context.Context, db *gorm.DB, anthroveUserID mode
} }
var count int64 var count int64
err := db.WithContext(ctx).Model(&models.UserFavorite{}).Where("user_id = ?", string(anthroveUserID)).Count(&count).Error err := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ?", string(anthroveUserID)).Count(&count).Error
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"anthrove_user_id": anthroveUserID, "anthrove_user_id": anthroveUserID,
@ -223,10 +223,10 @@ func GetAllAnthroveUserIDs(ctx context.Context, db *gorm.DB) ([]models.AnthroveU
} }
func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) { func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) {
var userFavorites []models.UserFavorite var userFavorites []models.UserFavorites
var favoritePosts []models.Post var favoritePosts []models.Post
err := db.WithContext(ctx).Model(&models.UserFavorite{}).Where("user_id = ?", string(anthroveUserID)).Offset(skip).Limit(limit).Find(&userFavorites).Error err := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ?", string(anthroveUserID)).Offset(skip).Limit(limit).Find(&userFavorites).Error
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"anthrove_user_id": anthroveUserID, "anthrove_user_id": anthroveUserID,
@ -262,7 +262,7 @@ func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUse
} }
func GetUserTagWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) { func GetUserTagWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) {
var userFavorites []models.UserFavorite var userFavorites []models.UserFavorites
err := db.WithContext(ctx).Where("user_id = ?", string(anthroveUserID)).Find(&userFavorites).Error err := db.WithContext(ctx).Where("user_id = ?", string(anthroveUserID)).Find(&userFavorites).Error
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{

55
pkg/models/orm_test.go Normal file
View File

@ -0,0 +1,55 @@
package models
import (
"gorm.io/gorm"
"testing"
"time"
)
func TestBaseModel_BeforeCreate(t *testing.T) {
type fields struct {
ID string
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt
}
type args struct {
db *gorm.DB
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "Test 1: Prefilled ID",
fields: fields{
ID: "1",
},
args: args{db: nil},
wantErr: false,
},
{
name: "Test 1: Autogenerate ID",
fields: fields{
ID: "",
},
args: args{db: nil},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
base := &BaseModel{
ID: tt.fields.ID,
CreatedAt: tt.fields.CreatedAt,
UpdatedAt: tt.fields.UpdatedAt,
DeletedAt: tt.fields.DeletedAt,
}
if err := base.BeforeCreate(tt.args.db); (err != nil) != tt.wantErr {
t.Errorf("BeforeCreate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

View File

@ -5,7 +5,7 @@ type Post struct {
BaseModel BaseModel
Rating Rating `gorm:"type:enum('safe','questionable','explicit')"` Rating Rating `gorm:"type:enum('safe','questionable','explicit')"`
Tags []Tag `gorm:"many2many:post_tags;"` Tags []Tag `gorm:"many2many:post_tags;"`
Favorites []UserFavorite `gorm:"foreignKey:PostID"` Favorites []UserFavorites `gorm:"foreignKey:PostID"`
References []PostReference `gorm:"foreignKey:PostID"` References []PostReference `gorm:"foreignKey:PostID"`
} }

View File

@ -0,0 +1,42 @@
package models
import "testing"
func TestPostReference_TableName(t *testing.T) {
type fields struct {
PostID string
SourceID string
URL string
SourcePostID string
FullFileURL string
PreviewFileURL string
SampleFileURL string
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Test 1: PostReference",
fields: fields{},
want: "PostReference",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
po := PostReference{
PostID: tt.fields.PostID,
SourceID: tt.fields.SourceID,
URL: tt.fields.URL,
SourcePostID: tt.fields.SourcePostID,
FullFileURL: tt.fields.FullFileURL,
PreviewFileURL: tt.fields.PreviewFileURL,
SampleFileURL: tt.fields.SampleFileURL,
}
if got := po.TableName(); got != tt.want {
t.Errorf("TableName() = %v, want %v", got, tt.want)
}
})
}
}

38
pkg/models/post_test.go Normal file
View File

@ -0,0 +1,38 @@
package models
import "testing"
func TestPost_TableName(t *testing.T) {
type fields struct {
BaseModel BaseModel
Rating Rating
Tags []Tag
Favorites []UserFavorites
References []PostReference
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Test 1: Is name Post",
fields: fields{},
want: "Post",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
po := Post{
BaseModel: tt.fields.BaseModel,
Rating: tt.fields.Rating,
Tags: tt.fields.Tags,
Favorites: tt.fields.Favorites,
References: tt.fields.References,
}
if got := po.TableName(); got != tt.want {
t.Errorf("TableName() = %v, want %v", got, tt.want)
}
})
}
}

40
pkg/models/source_test.go Normal file
View File

@ -0,0 +1,40 @@
package models
import "testing"
func TestSource_TableName(t *testing.T) {
type fields struct {
BaseModel BaseModel
DisplayName string
Domain string
Icon string
UserSources []UserSource
References []PostReference
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Test 1: Is name Source",
fields: fields{},
want: "Source",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
so := Source{
BaseModel: tt.fields.BaseModel,
DisplayName: tt.fields.DisplayName,
Domain: tt.fields.Domain,
Icon: tt.fields.Icon,
UserSources: tt.fields.UserSources,
References: tt.fields.References,
}
if got := so.TableName(); got != tt.want {
t.Errorf("TableName() = %v, want %v", got, tt.want)
}
})
}
}

96
pkg/models/tag_test.go Normal file
View File

@ -0,0 +1,96 @@
package models
import "testing"
func TestTagAlias_TableName(t *testing.T) {
type fields struct {
Name string
TagID string
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Test 1: Is Name TagAlias",
fields: fields{},
want: "TagAlias",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ta := TagAlias{
Name: tt.fields.Name,
TagID: tt.fields.TagID,
}
if got := ta.TableName(); got != tt.want {
t.Errorf("TableName() = %v, want %v", got, tt.want)
}
})
}
}
func TestTagGroup_TableName(t *testing.T) {
type fields struct {
Name string
TagID string
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Test 1: Is name TagGroup",
fields: fields{},
want: "TagGroup",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ta := TagGroup{
Name: tt.fields.Name,
TagID: tt.fields.TagID,
}
if got := ta.TableName(); got != tt.want {
t.Errorf("TableName() = %v, want %v", got, tt.want)
}
})
}
}
func TestTag_TableName(t *testing.T) {
type fields struct {
Name string
Type TagType
Aliases []TagAlias
Groups []TagGroup
Posts []Post
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Test 1: Is name Tag",
fields: fields{},
want: "Tag",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ta := Tag{
Name: tt.fields.Name,
Type: tt.fields.Type,
Aliases: tt.fields.Aliases,
Groups: tt.fields.Groups,
Posts: tt.fields.Posts,
}
if got := ta.TableName(); got != tt.want {
t.Errorf("TableName() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -3,7 +3,7 @@ package models
// User model // User model
type User struct { type User struct {
BaseModel BaseModel
Favorites []UserFavorite `gorm:"foreignKey:UserID"` Favorites []UserFavorites `gorm:"foreignKey:UserID"`
Sources []UserSource `gorm:"foreignKey:UserID"` Sources []UserSource `gorm:"foreignKey:UserID"`
} }

View File

@ -2,13 +2,13 @@ package models
import "time" import "time"
type UserFavorite struct { type UserFavorites struct {
UserID string `gorm:"primaryKey"` UserID string `gorm:"primaryKey"`
PostID string `gorm:"primaryKey"` PostID string `gorm:"primaryKey"`
CreatedAt time.Time CreatedAt time.Time
} }
func (UserFavorite) TableName() string { func (UserFavorites) TableName() string {
return "UserFavorites" return "UserFavorites"
} }

View File

@ -0,0 +1,37 @@
package models
import (
"testing"
"time"
)
func TestUserFavorite_TableName(t *testing.T) {
type fields struct {
UserID string
PostID string
CreatedAt time.Time
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Test 1: Is name UserFavorites",
fields: fields{},
want: "UserFavorites",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
us := UserFavorites{
UserID: tt.fields.UserID,
PostID: tt.fields.PostID,
CreatedAt: tt.fields.CreatedAt,
}
if got := us.TableName(); got != tt.want {
t.Errorf("TableName() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -0,0 +1,42 @@
package models
import "testing"
func TestUserSource_TableName(t *testing.T) {
type fields struct {
User User
UserID string
Source Source
SourceID string
ScrapeTimeInterval string
AccountUsername string
AccountID string
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Test 1: Is name UserSource",
fields: fields{},
want: "UserSource",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
us := UserSource{
User: tt.fields.User,
UserID: tt.fields.UserID,
Source: tt.fields.Source,
SourceID: tt.fields.SourceID,
ScrapeTimeInterval: tt.fields.ScrapeTimeInterval,
AccountUsername: tt.fields.AccountUsername,
AccountID: tt.fields.AccountID,
}
if got := us.TableName(); got != tt.want {
t.Errorf("TableName() = %v, want %v", got, tt.want)
}
})
}
}

34
pkg/models/user_test.go Normal file
View File

@ -0,0 +1,34 @@
package models
import "testing"
func TestUser_TableName(t *testing.T) {
type fields struct {
BaseModel BaseModel
Favorites []UserFavorites
Sources []UserSource
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Test 1: Is name User",
fields: fields{},
want: "User",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
us := User{
BaseModel: tt.fields.BaseModel,
Favorites: tt.fields.Favorites,
Sources: tt.fields.Sources,
}
if got := us.TableName(); got != tt.want {
t.Errorf("TableName() = %v, want %v", got, tt.want)
}
})
}
}