feat(database): added all missing functions

This commit is contained in:
SoXX 2024-08-11 00:13:46 +02:00
parent 1dc9b1fb7c
commit 2b54f25eea
9 changed files with 615 additions and 75 deletions

127
pkg/database/post.go Normal file
View File

@ -0,0 +1,127 @@
package database
import (
"context"
"errors"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
"gorm.io/gorm"
)
func CreatePost(ctx context.Context, post models.Post) (models.Post, error) {
if client == nil {
return models.Post{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
result := client.WithContext(ctx).Create(&post)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.Post{}, &otterError.Database{Reason: otterError.DuplicateKey}
}
return models.Post{}, result.Error
}
return post, nil
}
func CreatePostInBatch(ctx context.Context, post []models.Post, batchSize int) error {
if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if post == nil {
return &otterError.EntityValidationFailed{Reason: otterError.PostListIsEmpty}
}
if len(post) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.PostListIsEmpty}
}
if batchSize == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}
}
result := client.WithContext(ctx).CreateInBatches(&post, batchSize)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.Database{Reason: otterError.DuplicateKey}
}
return result.Error
}
return nil
}
func GetPostByID(ctx context.Context, id models.PostID) (models.Post, error) {
var post models.Post
if client == nil {
return models.Post{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if len(id) == 0 {
return models.Post{}, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty}
}
result := client.WithContext(ctx).First(&post, "id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return models.Post{}, &otterError.Database{Reason: otterError.NoDataFound}
}
return models.Post{}, result.Error
}
return post, nil
}
func UpdatePost(ctx context.Context, anthrovePost models.Post) error {
if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if len(anthrovePost.ID) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty}
}
updatePost := models.Post{
Rating: anthrovePost.Rating,
Tags: anthrovePost.Tags,
References: anthrovePost.References,
}
result := client.WithContext(ctx).Model(&updatePost).Update("deleted_at", gorm.DeletedAt{})
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &otterError.Database{Reason: otterError.NoDataFound}
}
return result.Error
}
return nil
}
func DeletePost(ctx context.Context, id models.PostID) error {
var userFavorite models.UserFavorite
if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if len(id) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty}
}
if len(id) != 25 {
return &otterError.EntityValidationFailed{Reason: otterError.PostIDToShort}
}
result := client.WithContext(ctx).Delete(&userFavorite, "id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &otterError.Database{Reason: otterError.NoDataFound}
}
return result.Error
}
return nil
}

View File

@ -8,43 +8,40 @@ import (
"gorm.io/gorm"
)
func CreateUserSource(ctx context.Context, userSource models.UserSource) (models.UserSource, error) {
func CreateSource(ctx context.Context, source models.Source) (models.Source, error) {
if client == nil {
return models.UserSource{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
return models.Source{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
result := client.WithContext(ctx).Create(&userSource)
result := client.WithContext(ctx).Create(&source)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.UserSource{}, &otterError.Database{Reason: otterError.DuplicateKey}
return models.Source{}, &otterError.Database{Reason: otterError.DuplicateKey}
}
return models.UserSource{}, result.Error
return models.Source{}, result.Error
}
return userSource, nil
return source, nil
}
func UpdateUserSource(ctx context.Context, userSource models.UserSource) error {
func CreateSourceInBatch(ctx context.Context, source []models.Source, batchSize int) error {
if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if len(userSource.ID) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDEmpty}
if source == nil {
return &otterError.EntityValidationFailed{Reason: otterError.SourceListIsEmpty}
}
updatedUserSource := models.UserSource{
BaseModel: models.BaseModel[models.UserSourceID]{
ID: userSource.ID,
},
ScrapeTimeInterval: userSource.ScrapeTimeInterval,
AccountUsername: userSource.AccountUsername,
AccountID: userSource.AccountID,
LastScrapeTime: userSource.LastScrapeTime,
AccountValidate: userSource.AccountValidate,
if len(source) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.SourceListIsEmpty}
}
result := client.WithContext(ctx).Updates(&updatedUserSource)
if batchSize == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}
}
result := client.WithContext(ctx).CreateInBatches(&source, batchSize)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.Database{Reason: otterError.DuplicateKey}
@ -55,48 +52,96 @@ func UpdateUserSource(ctx context.Context, userSource models.UserSource) error {
return nil
}
func GetUserSourceByID(ctx context.Context, id models.UserSourceID) (models.UserSource, error) {
var user models.UserSource
if client == nil {
return models.UserSource{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if len(id) == 0 {
return models.UserSource{}, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDEmpty}
}
if len(id) != 25 {
return models.UserSource{}, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort}
}
result := client.WithContext(ctx).First(&user, "id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return models.UserSource{}, &otterError.Database{Reason: otterError.NoDataFound}
}
return models.UserSource{}, result.Error
}
return user, nil
}
func DeleteUserSource(ctx context.Context, id models.UserSourceID) error {
var user models.UserSource
func UpdateSource(ctx context.Context, source models.Source) error {
if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if len(id) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDEmpty}
if len(source.ID) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty}
}
if len(id) != 25 {
return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort}
updateSource := models.Source{
DisplayName: source.DisplayName,
Domain: source.Domain,
Icon: source.Icon,
}
result := client.WithContext(ctx).Delete(&user, id)
result := client.WithContext(ctx).Model(&updateSource).Update("deleted_at", gorm.DeletedAt{})
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &otterError.Database{Reason: otterError.NoDataFound}
}
return result.Error
}
return nil
}
func GetSourceByID(ctx context.Context, id models.SourceID) (models.Source, error) {
var source models.Source
if client == nil {
return models.Source{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if len(id) == 0 {
return models.Source{}, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty}
}
if len(id) != 25 {
return models.Source{}, &otterError.EntityValidationFailed{Reason: otterError.SourceIDToShort}
}
result := client.WithContext(ctx).First(&source, "id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return models.Source{}, &otterError.Database{Reason: otterError.NoDataFound}
}
return models.Source{}, result.Error
}
return source, nil
}
func GetSourceByDomain(ctx context.Context, sourceDomain models.SourceDomain) (models.Source, error) {
var source models.Source
if client == nil {
return models.Source{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if len(sourceDomain) == 0 {
return models.Source{}, &otterError.EntityValidationFailed{Reason: otterError.SourceDomainIsEmpty}
}
result := client.WithContext(ctx).First(&source, "domain = ?", sourceDomain)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return models.Source{}, &otterError.Database{Reason: otterError.NoDataFound}
}
return models.Source{}, result.Error
}
return source, nil
}
func DeleteSource(ctx context.Context, id models.SourceID) error {
var source models.Source
if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if len(id) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty}
}
if len(id) != 25 {
return &otterError.EntityValidationFailed{Reason: otterError.SourceIDToShort}
}
result := client.WithContext(ctx).Delete(&source, id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &otterError.Database{Reason: otterError.NoDataFound}

81
pkg/database/tag.go Normal file
View File

@ -0,0 +1,81 @@
package database
import (
"context"
"errors"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
"gorm.io/gorm"
)
func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagType) error {
if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
tag := models.Tag{
Name: tagName,
Type: tagType,
}
result := client.WithContext(ctx).Create(&tag)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.Database{Reason: otterError.DuplicateKey}
}
return result.Error
}
return nil
}
func CreateTagInBatch(ctx context.Context, tags []models.Tag, batchSize int) error {
if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if tags == nil {
return &otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty}
}
if len(tags) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty}
}
if batchSize == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}
}
result := client.WithContext(ctx).CreateInBatches(&tags, batchSize)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.Database{Reason: otterError.DuplicateKey}
}
return result.Error
}
return nil
}
func DeleteTag(ctx context.Context, tagName models.TagName) error {
var tag models.Tag
if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if len(tagName) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty}
}
result := client.WithContext(ctx).Delete(&tag, tagName)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &otterError.Database{Reason: otterError.NoDataFound}
}
return result.Error
}
return nil
}

80
pkg/database/tagAlias.go Normal file
View File

@ -0,0 +1,80 @@
package database
import (
"context"
"errors"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
"gorm.io/gorm"
)
func CreateTagAlias(ctx context.Context, tagAliasName models.TagAliasName, tagName models.TagName) error {
if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
tag := models.TagAlias{
Name: tagAliasName,
TagID: tagName,
}
result := client.WithContext(ctx).Create(&tag)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.Database{Reason: otterError.DuplicateKey}
}
return result.Error
}
return nil
}
func CreateTagAliasInBatch(ctx context.Context, tagsAliases []models.TagAlias, batchSize int) error {
if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if tagsAliases == nil {
return &otterError.EntityValidationFailed{Reason: otterError.TagAliasListIsEmpty}
}
if len(tagsAliases) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.TagAliasListIsEmpty}
}
if batchSize == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}
}
result := client.WithContext(ctx).CreateInBatches(&tagsAliases, batchSize)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.Database{Reason: otterError.DuplicateKey}
}
return result.Error
}
return nil
}
func DeleteTagAlias(ctx context.Context, tagAliasName models.TagAliasName) error {
var tagAlias models.TagAlias
if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if len(tagAliasName) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.TagAliasNameIsEmpty}
}
result := client.WithContext(ctx).Delete(&tagAlias, tagAliasName)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &otterError.Database{Reason: otterError.NoDataFound}
}
return result.Error
}
return nil
}

80
pkg/database/tagGroup.go Normal file
View File

@ -0,0 +1,80 @@
package database
import (
"context"
"errors"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
"gorm.io/gorm"
)
func CreateTagGroup(ctx context.Context, tagGroupName models.TagGroupName, tagName models.TagName) error {
if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
tag := models.TagGroup{
Name: tagGroupName,
TagID: tagName,
}
result := client.WithContext(ctx).Create(&tag)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.Database{Reason: otterError.DuplicateKey}
}
return result.Error
}
return nil
}
func CreateTagGroupInBatch(ctx context.Context, tagsGroups []models.TagGroup, batchSize int) error {
if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if tagsGroups == nil {
return &otterError.EntityValidationFailed{Reason: otterError.TagGroupListIsEmpty}
}
if len(tagsGroups) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.TagGroupListIsEmpty}
}
if batchSize == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}
}
result := client.WithContext(ctx).CreateInBatches(&tagsGroups, batchSize)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.Database{Reason: otterError.DuplicateKey}
}
return result.Error
}
return nil
}
func DeleteTagGroup(ctx context.Context, tagGroupName models.TagGroupName) error {
var tagGroup models.TagGroup
if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if len(tagGroupName) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.TagGroupNameIsEmpty}
}
result := client.WithContext(ctx).Delete(&tagGroup, tagGroupName)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &otterError.Database{Reason: otterError.NoDataFound}
}
return result.Error
}
return nil
}

108
pkg/database/userSource.go Normal file
View File

@ -0,0 +1,108 @@
package database
import (
"context"
"errors"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
"gorm.io/gorm"
)
func CreateUserSource(ctx context.Context, userSource models.UserSource) (models.UserSource, error) {
if client == nil {
return models.UserSource{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
result := client.WithContext(ctx).Create(&userSource)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.UserSource{}, &otterError.Database{Reason: otterError.DuplicateKey}
}
return models.UserSource{}, result.Error
}
return userSource, nil
}
func UpdateUserSource(ctx context.Context, userSource models.UserSource) error {
if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if len(userSource.ID) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDIsEmpty}
}
updatedUserSource := models.UserSource{
BaseModel: models.BaseModel[models.UserSourceID]{
ID: userSource.ID,
},
ScrapeTimeInterval: userSource.ScrapeTimeInterval,
AccountUsername: userSource.AccountUsername,
AccountID: userSource.AccountID,
LastScrapeTime: userSource.LastScrapeTime,
AccountValidate: userSource.AccountValidate,
}
result := client.WithContext(ctx).Updates(&updatedUserSource)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.Database{Reason: otterError.DuplicateKey}
}
return result.Error
}
return nil
}
func GetUserSourceByID(ctx context.Context, id models.UserSourceID) (models.UserSource, error) {
var user models.UserSource
if client == nil {
return models.UserSource{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if len(id) == 0 {
return models.UserSource{}, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDIsEmpty}
}
if len(id) != 25 {
return models.UserSource{}, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort}
}
result := client.WithContext(ctx).First(&user, "id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return models.UserSource{}, &otterError.Database{Reason: otterError.NoDataFound}
}
return models.UserSource{}, result.Error
}
return user, nil
}
func DeleteUserSource(ctx context.Context, id models.UserSourceID) error {
var user models.UserSource
if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if len(id) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDIsEmpty}
}
if len(id) != 25 {
return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort}
}
result := client.WithContext(ctx).Delete(&user, id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &otterError.Database{Reason: otterError.NoDataFound}
}
return result.Error
}
return nil
}

View File

@ -3,16 +3,33 @@ package error
import "fmt"
const (
UserIDIsEmpty = "PostID cannot be empty"
UserIDToShort = "PostID needs to be 25 characters long"
SourceIDEmpty = "SourceID cannot be empty"
SourceIDToShort = "SourceID needs to be 25 characters long"
UserSourceIDEmpty = "UserSourceID cannot be empty"
UserSourceIDToShort = "UserSourceID needs to be 25 characters long"
TagIDEmpty = "tagID cannot be empty"
UserIDIsEmpty = "postID cannot be empty"
UserIDToShort = "postID needs to be 25 characters long"
SourceListIsEmpty = "sourceList cannot be empty"
SourceIDIsEmpty = "SourceID cannot be empty"
SourceIDToShort = "sourceID needs to be 25 characters long"
SourceDomainIsEmpty = "source Domain cannot be empty"
UserSourceIDIsEmpty = "userSourceID cannot be empty"
UserSourceIDToShort = "userSourceID needs to be 25 characters long"
TagListIsEmpty = "tagList cannot be empty"
TagNameIsEmpty = "tagName cannot be empty"
TagAliasListIsEmpty = "tagAliasList cannot be empty"
TagAliasNameIsEmpty = "tagAliasName cannot be empty"
TagGroupListIsEmpty = "tagGroupList cannot be empty"
TagGroupNameIsEmpty = "tagGroupName cannot be empty"
UserFavoriteListIsEmpty = "userFavoriteList cannot be empty"
UserFavoriteIDIsEmpty = "userFavoriteID cannot be empty"
UserFavoriteIDToShort = "UserFavoriteID needs to be 25 characters long"
UserFavoriteIDToShort = "userFavoriteID needs to be 25 characters long"
PostListIsEmpty = "userFavoriteList cannot be empty"
PostIDIsEmpty = "userFavoriteID cannot be empty"
PostIDToShort = "PostID needs to be 25 characters long"
BatchSizeIsEmpty = "batchSize cannot be empty"
)

View File

@ -3,17 +3,19 @@ package models
import "time"
type (
UserID string
PostID string
SourceID string
SourceDomain string
PostURL string
TagGroupName string
TagAliasName string
TagID string
UserID string
PostID string
PostURL string
SourceID string
SourceDomain string
TagName string
TagGroupName string
TagAliasName string
ScrapeTimeInterval int
UserLastScrapeTime time.Time
TagName string
Rating string
TagType string

View File

@ -2,7 +2,7 @@ package models
// Tag models
type Tag struct {
Name string `json:"name" gorm:"primaryKey"`
Name TagName `json:"name" gorm:"primaryKey"`
Type TagType `json:"type" gorm:"column:tag_type"`
Aliases []TagAlias `json:"aliases" gorm:"foreignKey:TagID"`
Groups []TagGroup `json:"groups" gorm:"foreignKey:TagID"`
@ -15,8 +15,8 @@ func (Tag) TableName() string {
// TagAlias model
type TagAlias struct {
Name string `json:"name" gorm:"primaryKey"`
TagID string `json:"tag_id"`
Name TagAliasName `json:"name" gorm:"primaryKey"`
TagID TagName `json:"tag_id"`
}
func (TagAlias) TableName() string {
@ -25,8 +25,8 @@ func (TagAlias) TableName() string {
// TagGroup model
type TagGroup struct {
Name string `json:"name" gorm:"primaryKey"`
TagID string `json:"tag_id"`
Name TagGroupName `json:"name" gorm:"primaryKey"`
TagID TagName `json:"tag_id"`
}
func (TagGroup) TableName() string {