add code comment

This commit is contained in:
niuhuan 2021-11-25 14:30:57 +08:00
parent f3905d44c3
commit 17f28cfb2f
6 changed files with 32 additions and 104 deletions

View File

@ -16,7 +16,7 @@ func InitApplication(applicationDir string) {
databasesDir = path.Join(applicationDir, "databases") databasesDir = path.Join(applicationDir, "databases")
remoteDir = path.Join(applicationDir, "pictures", "remote") remoteDir = path.Join(applicationDir, "pictures", "remote")
downloadDir = path.Join(applicationDir, "download") downloadDir = path.Join(applicationDir, "download")
tmpDir = path.Join(applicationDir, "download") tmpDir = path.Join(applicationDir, "tmp")
utils.Mkdir(databasesDir) utils.Mkdir(databasesDir)
utils.Mkdir(remoteDir) utils.Mkdir(remoteDir)
utils.Mkdir(downloadDir) utils.Mkdir(downloadDir)

View File

@ -73,26 +73,12 @@ func changeProxyUrl(urlStr string) bool {
return true return true
} }
func cacheable(key string, expire time.Duration, reload func() (interface{}, error)) (string, error) { func userProfile() (string, error) {
// CACHE return serialize(client.UserProfile())
cache := network_cache.LoadCache(key, expire) }
if cache != "" {
return cache, nil func punchIn() (string, error) {
} return serialize(client.PunchIn())
// obj
obj, err := reload()
if err != nil {
return "", err
}
buff, err := json.Marshal(obj)
// push to cache
if err != nil {
return "", err
}
// return
cache = string(buff)
network_cache.SaveCache(key, cache)
return cache, nil
} }
func categories() (string, error) { func categories() (string, error) {
@ -106,24 +92,6 @@ func categories() (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
var dbCategories []comic_center.Category
for _, c := range categories {
dbCategories = append(dbCategories, comic_center.Category{
ID: c.Id,
Title: c.Title,
Description: c.Description,
IsWeb: c.IsWeb,
Active: c.Active,
Link: c.Link,
ThumbOriginalName: c.Thumb.OriginalName,
ThumbFileServer: c.Thumb.FileServer,
ThumbPath: c.Thumb.Path,
})
}
err = comic_center.UpSetCategories(&dbCategories)
if err != nil {
return "", err
}
buff, _ := json.Marshal(&categories) buff, _ := json.Marshal(&categories)
cache = string(buff) cache = string(buff)
network_cache.SaveCache(key, cache) network_cache.SaveCache(key, cache)

View File

@ -3,6 +3,8 @@ package controller
import ( import (
"encoding/json" "encoding/json"
"pikapika/main/database/comic_center" "pikapika/main/database/comic_center"
"pikapika/main/database/network_cache"
"time"
) )
// EventNotify EventChannel 总线 // EventNotify EventChannel 总线
@ -41,6 +43,29 @@ func notifyExport(str string) {
onEvent("EXPORT", str) onEvent("EXPORT", str)
} }
// 缓存接口
func cacheable(key string, expire time.Duration, reload func() (interface{}, error)) (string, error) {
// CACHE
cache := network_cache.LoadCache(key, expire)
if cache != "" {
return cache, nil
}
// obj
obj, err := reload()
if err != nil {
return "", err
}
buff, err := json.Marshal(obj)
// push to cache
if err != nil {
return "", err
}
// return
cache = string(buff)
network_cache.SaveCache(key, cache)
return cache, nil
}
// 将interface序列化成字符串, 方便与flutter通信 // 将interface序列化成字符串, 方便与flutter通信
func serialize(point interface{}, err error) (string, error) { func serialize(point interface{}, err error) (string, error) {
if err != nil { if err != nil {

View File

@ -181,14 +181,6 @@ func clearToken() error {
return nil return nil
} }
func userProfile() (string, error) {
return serialize(client.UserProfile())
}
func punchIn() (string, error) {
return serialize(client.PunchIn())
}
func remoteImageData(params string) (string, error) { func remoteImageData(params string) (string, error) {
var paramsStruct struct { var paramsStruct struct {
FileServer string `json:"fileServer"` FileServer string `json:"fileServer"`

View File

@ -21,7 +21,6 @@ func InitDBConnect(databaseDir string) {
if err != nil { if err != nil {
panic("failed to connect database") panic("failed to connect database")
} }
db.AutoMigrate(&Category{})
db.AutoMigrate(&ComicView{}) db.AutoMigrate(&ComicView{})
db.AutoMigrate(&RemoteImage{}) db.AutoMigrate(&RemoteImage{})
db.AutoMigrate(&ComicDownload{}) db.AutoMigrate(&ComicDownload{})
@ -35,47 +34,6 @@ func Transaction(t func(tx *gorm.DB) error) error {
return db.Transaction(t) return db.Transaction(t)
} }
func UpSetCategories(categories *[]Category) error {
mutex.Lock()
defer mutex.Unlock()
return db.Transaction(func(tx *gorm.DB) error {
var in []string
for _, c := range *categories {
if c.ID == "" {
continue
}
in = append(in, c.ID)
err := tx.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "id"}},
DoUpdates: clause.AssignmentColumns([]string{
"updated_at",
"title",
"description",
"is_web",
"active",
"link",
"thumb_original_name",
"thumb_file_server",
"thumb_path",
}),
}).Create(&c).Error
if err != nil {
return err
}
}
err := tx.Unscoped().Model(&Category{}).Where(" id in ?", in).Update("deleted_at", gorm.DeletedAt{
Valid: false,
}).Error
if err != nil {
return err
}
return tx.Unscoped().Model(&Category{}).Where(" id not in ?", in).Update("deleted_at", gorm.DeletedAt{
Time: time.Now(),
Valid: true,
}).Error
})
}
func NoLockActionViewComicUpdateInfoDB(view *ComicView, db *gorm.DB) error { func NoLockActionViewComicUpdateInfoDB(view *ComicView, db *gorm.DB) error {
view.LastViewTime = time.Now() view.LastViewTime = time.Now()
return db.Clauses(clause.OnConflict{ return db.Clauses(clause.OnConflict{

View File

@ -5,21 +5,6 @@ import (
"time" "time"
) )
type Category struct {
ID string `gorm:"primarykey"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
Title string `json:"title"`
Description string `json:"description"`
IsWeb bool `json:"isWeb"`
Active bool `json:"active"`
Link string `json:"link"`
ThumbOriginalName string
ThumbFileServer string
ThumbPath string
}
type RemoteImage struct { type RemoteImage struct {
gorm.Model gorm.Model
FileServer string `gorm:"index:uk_fp,unique" json:"fileServer"` FileServer string `gorm:"index:uk_fp,unique" json:"fileServer"`