Skip to content

Commit

Permalink
refactor: replace the folders and files by the matters
Browse files Browse the repository at this point in the history
  • Loading branch information
saltbo committed Feb 9, 2021
1 parent d6ad3c3 commit da3c3f5
Show file tree
Hide file tree
Showing 25 changed files with 578 additions and 553 deletions.
11 changes: 2 additions & 9 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ package cmd
import (
"fmt"

"github.com/fsnotify/fsnotify"
"github.com/gin-gonic/gin"
"github.com/saltbo/gopkg/ginutil"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -57,19 +56,13 @@ func serverRun() {
dao.Init(viper.GetString("database.driver"), viper.GetString("database.dsn"))
}

viper.WatchConfig()
viper.OnConfigChange(func(in fsnotify.Event) {
dao.Init(viper.GetString("database.driver"), viper.GetString("database.dsn"))
})

ge := gin.Default()
api.SetupRoutes(ge)

//if conf.TLS.Enabled {
// //go startTls(ge, tlsAddr, conf.TLS.Auto, conf.TLS.CacheDir, conf.Server.Domain, conf.TLS.CertPath, conf.TLS.CertkeyPath)
// go startTls(ge, conf)
//}

ge := gin.Default()
api.SetupRoutes(ge)
addr := fmt.Sprintf(":%d", viper.GetInt("port"))
ginutil.Startup(ge, addr)
}
Expand Down
111 changes: 0 additions & 111 deletions internal/app/api/folders.go

This file was deleted.

34 changes: 12 additions & 22 deletions internal/app/api/file.go → internal/app/api/matter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,30 @@ import (
"github.com/saltbo/gopkg/ginutil"

"github.com/saltbo/zpan/internal/app/dao"
"github.com/saltbo/zpan/internal/app/dao/matter"
"github.com/saltbo/zpan/internal/app/service"
"github.com/saltbo/zpan/internal/pkg/authed"
"github.com/saltbo/zpan/internal/pkg/bind"
"github.com/saltbo/zpan/internal/pkg/fakefs"
)

type FileResource struct {
fs *service.File
fs *fakefs.FakeFS
}

func NewFileResource() ginutil.Resource {
return &FileResource{
fs: service.NewFile(),
fs: fakefs.New(),
}
}

func (rs *FileResource) Register(router *gin.RouterGroup) {
router.POST("/files", rs.create)
router.GET("/files", rs.findAll)
router.GET("/files/:alias", rs.find)
router.PATCH("/files/:alias/uploaded", rs.uploaded)
router.PATCH("/files/:alias/name", rs.rename)
router.PATCH("/files/:alias/location", rs.move)
router.PATCH("/files/:alias/duplicate", rs.copy)
router.DELETE("/files/:alias", rs.delete)
router.POST("/matters", rs.create)
router.GET("/matters", rs.findAll)
router.GET("/matters/:alias", rs.find)
router.PATCH("/matters/:alias/uploaded", rs.uploaded)
router.PATCH("/matters/:alias/name", rs.rename)
router.PATCH("/matters/:alias/location", rs.move)
router.PATCH("/matters/:alias/duplicate", rs.copy)
router.DELETE("/matters/:alias", rs.delete)
}

func (rs *FileResource) findAll(c *gin.Context) {
Expand All @@ -41,16 +40,7 @@ func (rs *FileResource) findAll(c *gin.Context) {
return
}

opts := make([]matter.QueryOption, 0)
if p.Type != "" {
opts = append(opts, matter.WithType(p.Type))
} else if p.Keyword != "" {
opts = append(opts, matter.WithKeyword(p.Keyword))
} else {
opts = append(opts, matter.WithDir(p.Dir))
}

list, total, err := rs.fs.FindAll(authed.UidGet(c), p.Sid, p.Offset, p.Limit, opts...)
list, total, err := rs.fs.List(authed.UidGet(c), p)
if err != nil {
ginutil.JSONServerError(c, err)
return
Expand Down
1 change: 0 additions & 1 deletion internal/app/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func SetupRoutes(ge *gin.Engine) {
NewTokenResource(),
NewStorageResource(),
NewFileResource(),
NewFolderResource(),
NewShareResource(),
NewRecycleBinResource(),
)
Expand Down
27 changes: 13 additions & 14 deletions internal/app/api/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import (
"gorm.io/gorm"

"github.com/saltbo/zpan/internal/app/dao"
"github.com/saltbo/zpan/internal/app/dao/matter"
"github.com/saltbo/zpan/internal/app/model"
"github.com/saltbo/zpan/internal/pkg/authed"
"github.com/saltbo/zpan/internal/pkg/gormutil"
"github.com/saltbo/zpan/internal/pkg/bind"
)

Expand All @@ -26,13 +24,13 @@ type ShareResource struct {
jwtutil.JWTUtil

dShare *dao.Share
dMatter *matter.Matter
dMatter *dao.Matter
}

func NewShareResource() ginutil.Resource {
return &ShareResource{
dShare: dao.NewShare(),
dMatter: matter.NewMatter(),
dMatter: dao.NewMatter(),
}
}

Expand Down Expand Up @@ -69,12 +67,8 @@ func (rs *ShareResource) findAll(c *gin.Context) {
return
}

var total int64
list := make([]model.Share, 0)
sn := gormutil.DB().Where("uid=?", authed.UidGet(c))
sn.Model(model.Share{}).Count(&total)
sn = sn.Order("id desc")
if err := sn.Limit(p.Limit).Offset(p.Offset).Find(&list).Error; err != nil {
list, total, err := rs.dShare.FindAll(authed.UidGet(c))
if err != nil {
ginutil.JSONBadRequest(c, err)
return
}
Expand Down Expand Up @@ -106,7 +100,7 @@ func (rs *ShareResource) create(c *gin.Context) {
if p.Private {
m.Secret = strutil.RandomText(5)
}
if err := gormutil.DB().Create(m).Error; err != nil {
if err := rs.dShare.Create(m); err != nil {
ginutil.JSONServerError(c, err)
return
}
Expand All @@ -131,7 +125,7 @@ func (rs *ShareResource) update(c *gin.Context) {
share.Secret = strutil.RandomText(5)
}

if err := gormutil.DB().Save(share).Error; err != nil {
if err := rs.dShare.Update(p.Id, share); err != nil {
ginutil.JSONServerError(c, err)
return
}
Expand All @@ -146,7 +140,7 @@ func (rs *ShareResource) delete(c *gin.Context) {
return
}

if err := gormutil.DB().Delete(share, "id=?", share.Id).Error; err != nil {
if err := rs.dShare.Delete(share.Id); err != nil {
ginutil.JSONServerError(c, err)
return
}
Expand Down Expand Up @@ -232,7 +226,12 @@ func (rs *ShareResource) findMatters(c *gin.Context) {
}

dir := fmt.Sprintf("%s/%s", mMatter.Name, p.Dir) // 设置父级目录
list, total, err := rs.dMatter.FindAll(mMatter.Uid, p.Offset, p.Limit, matter.WithDir(dir))
query := dao.NewQuery()
query.WithEq("uid", mMatter.Uid)
query.WithEq("parent", dir)
query.Offset = p.Offset
query.Limit = p.Limit
list, total, err := rs.dMatter.FindAll(query)
if err != nil {
ginutil.JSONServerError(c, err)
return
Expand Down
3 changes: 1 addition & 2 deletions internal/app/api/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/saltbo/zpan/internal/app/model"
"github.com/saltbo/zpan/internal/app/service"
"github.com/saltbo/zpan/internal/pkg/bind"
"github.com/saltbo/zpan/internal/pkg/gormutil"
"github.com/saltbo/zpan/internal/pkg/middleware"
"github.com/saltbo/zpan/internal/pkg/provider"
)
Expand Down Expand Up @@ -46,7 +45,7 @@ func (rs *Option) createDatabase(c *gin.Context) {
return
}

if _, err := gormutil.New(gormutil.Config{Driver: p["driver"], DSN: p["dsn"]}); err != nil {
if err := dao.Init(p["driver"], p["dsn"]); err != nil {
ginutil.JSONServerError(c, err)
return
}
Expand Down
30 changes: 26 additions & 4 deletions internal/app/dao/dao.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
package dao

import (
"gorm.io/gorm"

"github.com/saltbo/zpan/internal/app/model"
"github.com/saltbo/zpan/internal/pkg/gormutil"
)

func Init(driver, dsn string) {
gormutil.Init(gormutil.Config{
var gdb *gorm.DB

func Ready() bool {
return gdb != nil
}

func Init(driver, dsn string) error {
conf := gormutil.Config{
Driver: driver,
DSN: dsn,
}, true)
gormutil.AutoMigrate(model.Tables())
}
db, err := gormutil.New(conf)
if err != nil {
return err
}

gdb = db.Debug()
if err := gdb.AutoMigrate(model.Tables()...); err != nil {
return err
}

return nil
}

func Transaction(fc func(tx *gorm.DB) error) error {
return gdb.Transaction(fc)
}
Loading

0 comments on commit da3c3f5

Please sign in to comment.