Refactor db operations into PostService and add OnConflict for account inserts

This commit is contained in:
2025-05-13 15:47:01 +03:30
parent 25d9b67be6
commit a405289b33
3 changed files with 73 additions and 52 deletions

View File

@@ -10,50 +10,4 @@ func AddMigrations(db *gorm.DB) {
db.AutoMigrate(&models.Post{}, &models.MediaAttachment{}, &models.Account{})
}
func GetExistingPostIds(db *gorm.DB) []string {
var existingPostIds []string
db.Model(&models.Post{}).Pluck("id", &existingPostIds)
return existingPostIds
}
func GetExistingAccountIds(db *gorm.DB) []string {
var existingAccountIds []string
db.Model(&models.Account{}).Pluck("acc_id", &existingAccountIds)
return existingAccountIds
}
func GetNewPosts(existingPostIds []string, posts []models.Post) []models.Post {
var newPosts []models.Post = nil
for _, post := range posts {
if !arrayContains(existingPostIds, post.ID) && len(post.Attachments) > 0 && !post.Account.IsBot {
newPosts = append(newPosts, post)
}
}
return newPosts
}
func GetNewAccounts(existingAccountIds []string, posts []models.Post) []models.Account {
var newAccounts []models.Account = nil
for _, post := range posts {
if !arrayContains(existingAccountIds, post.Account.AccId) {
newAccounts = append(newAccounts, post.Account)
}
}
return newAccounts
}
func InsertNewPosts(db *gorm.DB, newPosts []models.Post) int {
return int(db.Create(&newPosts).RowsAffected)
}
func InsertNewAccounts(db *gorm.DB, newAccounts []models.Account) int {
return int(db.Create(&newAccounts).RowsAffected)
}
func arrayContains(arr []string, str string) bool {
for _, a := range arr {
if a == str {
return true
}
}
return false
}

View File

@@ -0,0 +1,65 @@
package services
import (
"CatsOfMastodonBotGo/internal/models"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type PostService struct {
db *gorm.DB
}
// Constructor
func NewPostService(db *gorm.DB) *PostService {
return &PostService{db: db}
}
func (ps *PostService) GetExistingPostIds() []string {
var existingPostIds []string
ps.db.Model(&models.Post{}).Pluck("id", &existingPostIds)
return existingPostIds
}
func (ps *PostService) GetExistingAccountIds() []string {
var existingAccountIds []string
ps.db.Model(&models.Account{}).Pluck("acc_id", &existingAccountIds)
return existingAccountIds
}
func (*PostService) GetNewPosts(existingPostIds []string, posts []models.Post) []models.Post {
var newPosts []models.Post = nil
for _, post := range posts {
if !arrayContains(existingPostIds, post.ID) && len(post.Attachments) > 0 && !post.Account.IsBot {
newPosts = append(newPosts, post)
}
}
return newPosts
}
func (*PostService) GetNewAccounts(existingAccountIds []string, posts []models.Post) []models.Account {
var newAccounts []models.Account = nil
for _, post := range posts {
if !arrayContains(existingAccountIds, post.Account.AccId) {
newAccounts = append(newAccounts, post.Account)
}
}
return newAccounts
}
func (ps *PostService) InsertNewPosts(newPosts []models.Post) int {
return int(ps.db.Create(&newPosts).RowsAffected)
}
func (ps *PostService) InsertNewAccounts(newAccounts []models.Account) int {
return int(ps.db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&newAccounts).RowsAffected)
}
func arrayContains(arr []string, str string) bool {
for _, a := range arr {
if a == str {
return true
}
}
return false
}