otter-space-sdk/pkg/database/post.go

128 lines
3.3 KiB
Go
Raw Normal View History

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
}