feat: 完成基本的 model

This commit is contained in:
loveuer
2025-07-10 22:39:41 +08:00
parent 84348f9eaf
commit 99cbe8c523
13 changed files with 400 additions and 14 deletions

View File

@ -11,8 +11,10 @@ var (
root *cobra.Command
)
func Init(ctx context.Context) {
root = rootCmd()
func Init() {
root = rootCmd(
svcCmd(),
)
}
func Execute(ctx context.Context) {

View File

@ -26,6 +26,7 @@ func rootCmd(subCmds ...*cobra.Command) *cobra.Command {
}
cmd.PersistentFlags().BoolVar(&opt.Cfg.Debug, "debug", false, "debug mode")
cmd.PersistentFlags().StringVar(&opt.Cfg.Name, "name", "utodo", "app name")
cmd.AddCommand(subCmds...)

55
internal/cmd/svc.go Normal file
View File

@ -0,0 +1,55 @@
package cmd
import (
"context"
"loveuer/utodo/internal/model"
"loveuer/utodo/internal/opt"
g_api "loveuer/utodo/pkg/api"
"gitea.loveuer.com/yizhisec/packages/database/db"
"gitea.loveuer.com/yizhisec/packages/tool"
"github.com/spf13/cobra"
)
func svcCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "svc",
Short: "run todo service",
RunE: svcRun,
}
cmd.Flags().StringVar(&opt.Cfg.Svc.Address, "address", ":9119", "address to listen on")
cmd.Flags().StringVar(&opt.Cfg.Svc.DBFile, "db", "/data/data.db", "database file")
return cmd
}
func svcRun(cmd *cobra.Command, args []string) error {
var (
err error
stopApi func(context.Context) error
)
if err = db.Init(db.WithSqlite(opt.Cfg.Svc.DBFile)); err != nil {
return err
}
if err = model.Init(cmd.Context(), db.Default.Session(tool.Timeout(60))); err != nil {
return err
}
if stopApi, err = g_api.Start(
cmd.Context(),
g_api.WithAddress(opt.Cfg.Svc.Address),
g_api.WithVersion(opt.Cfg.Version),
g_api.WithName(opt.Cfg.Name),
); err != nil {
return err
}
<-cmd.Context().Done()
tool.MustStop(tool.Timeout(3), stopApi)
return nil
}

13
internal/model/model.go Normal file
View File

@ -0,0 +1,13 @@
package model
import (
"context"
"gorm.io/gorm"
)
func Init(ctx context.Context, tx *gorm.DB) error {
return tx.AutoMigrate(
&Todo{},
)
}

46
internal/model/todo.go Normal file
View File

@ -0,0 +1,46 @@
package model
import "encoding/json"
type TodoStatus string
const (
TodoStatusPlaned TodoStatus = "planned"
TodoStatusPaused TodoStatus = "paused"
TodoStatusDoing TodoStatus = "doing"
TodoStatusDone TodoStatus = "done"
TodoStatusCancel TodoStatus = "cancel"
)
var (
TodoStatusMap = map[TodoStatus]string{
TodoStatusPlaned: "已计划",
TodoStatusPaused: "暂停中",
TodoStatusDoing: "进行中",
TodoStatusDone: "已完成",
TodoStatusCancel: "已取消",
}
)
func (t TodoStatus) MarshalJSON() ([]byte, error) {
return json.Marshal(TodoStatusMap[t])
}
type Todo struct {
Id int64 `json:"id" gorm:"column:id,primaryKey;autoIncrement"`
CreatedAt int64 `json:"created_at" gorm:"column:created_at,autoCreateTime:milli"`
UpdatedAt int64 `json:"updated_at" gorm:"column:updated_at,autoUpdateTime:milli"`
DeletedAt int64 `json:"deleted_at" gorm:"column:deleted_at"`
ShouldDoneAt int64 `json:"should_done_at" gorm:"column:should_done_at"`
DoneAt int64 `json:"done_at" gorm:"column:done_at"`
Title string `json:"title" gorm:"column:title;type:varchar(255);not null"`
Content string `json:"content" gorm:"column:content;type:text;not null"`
Comment string `json:"comment" gorm:"column:comment;type:text;not null"`
Progress int64 `json:"progress" gorm:"column:progress"`
Level int64 `json:"level" gorm:"column:level,default:0"`
Status TodoStatus `json:"status" gorm:"column:status"`
PId int64 `json:"pid" gorm:"column:pid"`
UserId int64 `json:"user_id" gorm:"column:user_id"`
CollectionId int64 `json:"collection_id" gorm:"column:collection_id"`
Weight int64 `json:"weight" gorm:"column:weight"`
}

View File

@ -1,7 +1,17 @@
package opt
type config struct {
Debug bool
Debug bool
Version string
Name string
Svc struct {
Address string
DBFile string
}
}
var Cfg = &config{}
func Init() {
Cfg.Version = Version
}