Use db for queue (#498)
This commit is contained in:
@@ -3,16 +3,17 @@ package db
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm/logger"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm/logger"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/thomiceli/opengist/internal/config"
|
||||
"gorm.io/gorm"
|
||||
@@ -154,7 +155,7 @@ func Setup(dbUri string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = db.AutoMigrate(&User{}, &Gist{}, &SSHKey{}, &AdminSetting{}, &Invitation{}, &WebAuthnCredential{}, &TOTP{}, &GistTopic{}, &GistLanguage{}); err != nil {
|
||||
if err = db.AutoMigrate(&User{}, &Gist{}, &SSHKey{}, &AdminSetting{}, &Invitation{}, &WebAuthnCredential{}, &TOTP{}, &GistTopic{}, &GistLanguage{}, &GistInitQueue{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
@@ -613,30 +611,6 @@ func (gist *Gist) TopicsSlice() []string {
|
||||
return topics
|
||||
}
|
||||
|
||||
func (gist *Gist) SerialiseInitRepository() error {
|
||||
var gobBuffer bytes.Buffer
|
||||
encoder := gob.NewEncoder(&gobBuffer)
|
||||
if err := encoder.Encode(gist); err != nil {
|
||||
return fmt.Errorf("gob encoding error: %v", err)
|
||||
}
|
||||
|
||||
return git.SerialiseInitRepository(gist.User.Username, gobBuffer.Bytes())
|
||||
}
|
||||
|
||||
func DeserialiseInitRepository(user string) (*Gist, error) {
|
||||
data, err := git.DeserialiseInitRepository(user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var gist Gist
|
||||
decoder := gob.NewDecoder(bytes.NewReader(data))
|
||||
if err := decoder.Decode(&gist); err != nil {
|
||||
return nil, fmt.Errorf("gob decoding error: %v", err)
|
||||
}
|
||||
return &gist, nil
|
||||
}
|
||||
|
||||
func (gist *Gist) UpdateLanguages() {
|
||||
languages, err := gist.GetLanguagesFromFiles()
|
||||
if err != nil {
|
||||
|
||||
34
internal/db/gist_init_queue.go
Normal file
34
internal/db/gist_init_queue.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package db
|
||||
|
||||
type GistInitQueue struct {
|
||||
GistID uint `gorm:"primaryKey"`
|
||||
Gist Gist `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;foreignKey:GistID"`
|
||||
UserID uint `gorm:"primaryKey"`
|
||||
User User `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;foreignKey:UserID"`
|
||||
}
|
||||
|
||||
func GetInitGistInQueueForUser(userID uint) (*Gist, error) {
|
||||
queue := new(GistInitQueue)
|
||||
err := db.Preload("Gist").Preload("Gist.User").
|
||||
Where("user_id = ?", userID).
|
||||
Order("gist_id asc").
|
||||
First(&queue).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = db.Delete(&queue).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &queue.Gist, nil
|
||||
}
|
||||
|
||||
func AddInitGistToQueue(gistID uint, userID uint) error {
|
||||
queue := &GistInitQueue{
|
||||
GistID: gistID,
|
||||
UserID: userID,
|
||||
}
|
||||
return db.Create(&queue).Error
|
||||
}
|
||||
Reference in New Issue
Block a user