Refactor db operations into PostService and add OnConflict for account inserts
This commit is contained in:
14
cmd/main.go
14
cmd/main.go
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"CatsOfMastodonBotGo/internal/helpers"
|
"CatsOfMastodonBotGo/internal/helpers"
|
||||||
"CatsOfMastodonBotGo/internal/server"
|
"CatsOfMastodonBotGo/internal/server"
|
||||||
|
"CatsOfMastodonBotGo/internal/services"
|
||||||
"context"
|
"context"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@@ -42,10 +43,11 @@ func main() {
|
|||||||
helpers.AddMigrations(db)
|
helpers.AddMigrations(db)
|
||||||
|
|
||||||
// Process posts
|
// Process posts
|
||||||
var existingPostIds = helpers.GetExistingPostIds(db)
|
var postService = services.NewPostService(db)
|
||||||
var existingAccountIds = helpers.GetExistingAccountIds(db)
|
var existingPostIds = postService.GetExistingPostIds()
|
||||||
var newPosts = helpers.GetNewPosts(existingPostIds, posts)
|
var existingAccountIds = postService.GetExistingAccountIds()
|
||||||
var newAccounts = helpers.GetNewAccounts(existingAccountIds, newPosts)
|
var newPosts = postService.GetNewPosts(existingPostIds, posts)
|
||||||
|
var newAccounts = postService.GetNewAccounts(existingAccountIds, newPosts)
|
||||||
|
|
||||||
// Save to database
|
// Save to database
|
||||||
log.Println("Number of existing posts: ", len(existingPostIds))
|
log.Println("Number of existing posts: ", len(existingPostIds))
|
||||||
@@ -55,12 +57,12 @@ func main() {
|
|||||||
|
|
||||||
// Additional logging
|
// Additional logging
|
||||||
if newAccounts != nil {
|
if newAccounts != nil {
|
||||||
log.Println("Number of inserted accounts: ", helpers.InsertNewAccounts(db, newAccounts))
|
log.Println("Number of inserted accounts: ", postService.InsertNewAccounts(newAccounts))
|
||||||
} else {
|
} else {
|
||||||
log.Println("No new accounts inserted")
|
log.Println("No new accounts inserted")
|
||||||
}
|
}
|
||||||
if newPosts != nil {
|
if newPosts != nil {
|
||||||
log.Println("Number of inserted posts: ", helpers.InsertNewPosts(db, newPosts))
|
log.Println("Number of inserted posts: ", postService.InsertNewPosts(newPosts))
|
||||||
} else {
|
} else {
|
||||||
log.Println("No new posts inserted")
|
log.Println("No new posts inserted")
|
||||||
}
|
}
|
||||||
|
@@ -10,50 +10,4 @@ func AddMigrations(db *gorm.DB) {
|
|||||||
db.AutoMigrate(&models.Post{}, &models.MediaAttachment{}, &models.Account{})
|
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