basic structure

This commit is contained in:
微凉
2021-10-26 22:28:37 +08:00
parent d68a4048da
commit fb7e67724d
25 changed files with 615 additions and 45 deletions

89
model/account.go Normal file
View File

@ -0,0 +1,89 @@
package model
import (
"github.com/Xhofe/alist/conf"
log "github.com/sirupsen/logrus"
)
type Account struct {
Name string `json:"name" gorm:"primaryKey" validate:"required"`
Type string `json:"type"`
Username string `json:"username"`
Password string `json:"password"`
RefreshToken string `json:"refresh_token"`
AccessToken string `json:"access_token"`
RootFolder string `json:"root_folder"`
Status int `json:"status"`
CronId int `json:"cron_id"`
}
var accountsMap = map[string]Account{}
func SaveAccount(account Account) error {
if err := conf.DB.Save(account).Error; err != nil {
return err
}
RegisterAccount(account)
return nil
}
func DeleteAccount(name string) error {
account := Account{
Name: name,
}
if err := conf.DB.Delete(&account).Error; err != nil {
return err
}
delete(accountsMap, name)
return nil
}
func AccountsCount() int {
return len(accountsMap)
}
func RegisterAccount(account Account) {
accountsMap[account.Name] = account
}
func GetAccount(name string) (Account, bool) {
if len(accountsMap) == 1 {
for _, v := range accountsMap {
return v, true
}
}
account, ok := accountsMap[name]
return account, ok
}
func GetAccountFiles() []*File {
files := make([]*File, 0)
for _, v := range accountsMap {
files = append(files, &File{
Name: v.Name,
Size: 0,
Type: conf.FOLDER,
UpdatedAt: nil,
})
}
return files
}
func GetAccounts() []*Account {
accounts := make([]*Account, 0)
for _, v := range accountsMap {
accounts = append(accounts, &v)
}
return accounts
}
func initAccounts() {
var accounts []Account
if err := conf.DB.Find(&accounts).Error; err != nil {
log.Fatalf("failed sync init accounts")
}
for _, account := range accounts {
RegisterAccount(account)
}
log.Debugf("accounts:%+v", accountsMap)
}

View File

@ -1,7 +0,0 @@
package model
type ConfigItem struct {
Key string `json:"key"`
Value string `json:"value"`
Type int `json:"type"`
}

10
model/file.go Normal file
View File

@ -0,0 +1,10 @@
package model
import "time"
type File struct {
Name string `json:"name"`
Size int64 `json:"size"`
Type int `json:"type"`
UpdatedAt *time.Time `json:"updated_at"`
}

View File

@ -64,8 +64,12 @@ func InitModel() {
log.Fatalf("not supported database type: %s", config.Type)
}
log.Infof("auto migrate model")
err := conf.DB.AutoMigrate(&ConfigItem{})
err := conf.DB.AutoMigrate(&SettingItem{},&Account{})
if err != nil {
log.Fatalf("failed to auto migrate")
}
}
// TODO init accounts and filetype
initAccounts()
}

8
model/meta.go Normal file
View File

@ -0,0 +1,8 @@
package model
type Meta struct {
Path string `json:"path" gorm:"primaryKey"`
Password string `json:"password"`
Hide bool `json:"hide"`
Ignore bool `json:"ignore"`
}

27
model/setting.go Normal file
View File

@ -0,0 +1,27 @@
package model
import "github.com/Xhofe/alist/conf"
type SettingItem struct {
Key string `json:"key" gorm:"primaryKey" validate:"required"`
Value string `json:"value"`
Description string `json:"description"`
Type int `json:"type"`
}
func SaveSettings(items []SettingItem) error {
//tx := conf.DB.Begin()
//for _,item := range items{
// tx.Save(item)
//}
//tx.Commit()
return conf.DB.Save(items).Error
}
func GetSettingByType(t int) (*[]SettingItem, error) {
var items []SettingItem
if err := conf.DB.Where("type = ?", t).Find(&items).Error; err != nil {
return nil, err
}
return &items, nil
}