feat: add cmds

This commit is contained in:
Rogee
2024-09-05 15:12:53 +08:00
parent 7d1c9764b5
commit 8666db6318
10 changed files with 230 additions and 28 deletions

View File

@@ -23,10 +23,30 @@ type DBChannel struct {
UUID int64
Username string
Title string
OffsetID int
MinID int
ExportMedia bool
}
func NewDBChannelFromDB(id int64) (*DBChannel, error) {
var m model.Channels
tbl := table.Channels
err := tbl.SELECT(tbl.AllColumns).WHERE(tbl.ID.EQ(Int64(id))).QueryContext(context.Background(), db, &m)
if err != nil {
return nil, errors.Wrapf(err, "find channel by id(%d) failed", id)
}
return &DBChannel{
UUID: m.UUID,
Username: m.Username,
Title: m.Title,
OffsetID: int(m.Offset),
MinID: int(m.MinID),
ExportMedia: m.ExportMedia,
}, nil
}
func NewDBChannel(uuid int64, username, title string) *DBChannel {
if uuid == 0 {
panic("channel id is required")
@@ -59,7 +79,7 @@ func (c *DBChannel) Asset(assetID int64, ext string) string {
return assetFile
}
func (c *DBChannel) Get(ctx context.Context) error {
func (c *DBChannel) GetOrCreate(ctx context.Context) error {
tbl := table.Channels
var m model.Channels
@@ -74,7 +94,7 @@ func (c *DBChannel) Get(ctx context.Context) error {
Title: c.Title,
MinID: 0,
Offset: 0,
ExportMedia: false,
ExportMedia: c.ExportMedia,
CreatedAt: lo.ToPtr(time.Now()),
UpdatedAt: lo.ToPtr(time.Now()),
}
@@ -87,20 +107,45 @@ func (c *DBChannel) Get(ctx context.Context) error {
}
}
c.MinID = int(m.MinID)
c.OffsetID = int(m.Offset)
c.ExportMedia = m.ExportMedia
return nil
}
func (c *DBChannel) SetOffset(ctx context.Context, offset int) error {
if c.OffsetID < offset {
c.OffsetID = offset
}
tbl := table.Channels
_, err := tbl.UPDATE().
SET(
tbl.Offset.SET(Int64(int64(c.OffsetID))),
).
WHERE(tbl.UUID.EQ(Int64(c.UUID))).
ExecContext(ctx, db)
if err != nil {
return errors.Wrap(err, "update channel")
}
return nil
}
func (c *DBChannel) Update(ctx context.Context, msgID int) error {
if c.MinID < msgID {
c.MinID = msgID
}
if c.OffsetID < msgID {
c.OffsetID = msgID
}
tbl := table.Channels
_, err := tbl.UPDATE().
SET(
tbl.MinID.SET(Int64(int64(c.MinID))),
tbl.Offset.SET(Int64(int64(c.OffsetID))),
).
WHERE(tbl.UUID.EQ(Int64(c.UUID))).
ExecContext(ctx, db)