Refactor db operations into PostService and add OnConflict for account inserts
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
65
internal/services/postService.go
Normal file
65
internal/services/postService.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user