Add user management and improve code organization with context structs
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Get environment variables
|
||||
var tag = os.Getenv("COM_TAG")
|
||||
if tag == "" {
|
||||
tag = "catsofmastodon"
|
||||
@@ -24,6 +25,7 @@ func main() {
|
||||
instance = "https://haminoa.net"
|
||||
}
|
||||
|
||||
// Receive posts
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
err, posts := helpers.GetPosts(ctx, tag, instance)
|
||||
@@ -32,23 +34,26 @@ func main() {
|
||||
}
|
||||
log.Println("Number of fetched posts: ", len(posts))
|
||||
|
||||
// Connect to database
|
||||
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{Logger: logger.Default.LogMode(logger.Warn)})
|
||||
if err != nil {
|
||||
panic("failed to connect database")
|
||||
}
|
||||
|
||||
helpers.AddMigrations(db)
|
||||
|
||||
// Process posts
|
||||
var existingPostIds = helpers.GetExistingPostIds(db)
|
||||
var existingAccountIds = helpers.GetExistingAccountIds(db)
|
||||
var newPosts = helpers.GetNewPosts(existingPostIds, posts)
|
||||
var newAccounts = helpers.GetNewAccounts(existingAccountIds, newPosts)
|
||||
|
||||
// Save to database
|
||||
log.Println("Number of existing posts: ", len(existingPostIds))
|
||||
log.Println("Number of existing accounts: ", len(existingAccountIds))
|
||||
log.Println("Number of new posts: ", len(newPosts))
|
||||
log.Println("Number of new accounts: ", len(newAccounts))
|
||||
|
||||
// Additional logging
|
||||
if newAccounts != nil {
|
||||
log.Println("Number of inserted accounts: ", helpers.InsertNewAccounts(db, newAccounts))
|
||||
} else {
|
||||
|
8
internal/AppContext.go
Normal file
8
internal/AppContext.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package internal
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
|
||||
type AppContext struct {
|
||||
Db *gorm.DB
|
||||
}
|
7
internal/AppWebContext.go
Normal file
7
internal/AppWebContext.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package internal
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
type AppWebContext struct {
|
||||
GinEngine *gin.Engine
|
||||
}
|
10
internal/models/comUser.go
Normal file
10
internal/models/comUser.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package models
|
||||
|
||||
// Funny you are
|
||||
type ComUser struct {
|
||||
Id string
|
||||
Username string
|
||||
Password string
|
||||
Email string
|
||||
IsVerified bool
|
||||
}
|
1
internal/repositories/userRepo.go
Normal file
1
internal/repositories/userRepo.go
Normal file
@@ -0,0 +1 @@
|
||||
package repositories
|
1
internal/services/userService.go
Normal file
1
internal/services/userService.go
Normal file
@@ -0,0 +1 @@
|
||||
package services
|
Reference in New Issue
Block a user