demo.go 1008 Bytes
Newer Older
kzkzzzz's avatar
kzkzzzz committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
package dao

import (
	"context"
	"hilo/app/service/demo/internal/model"
	"time"
)

func (d *Dao) Save(ctx context.Context, m *model.Demo) error {
	err := d.DB.Model(&model.Demo{}).Create(m).Error
	return err
}

func (d *Dao) Update(ctx context.Context, m *model.Demo) error {
	err := d.DB.Where("id = ?", m.Id).Updates(&model.Demo{
		Name:      m.Name,
		Value:     m.Value,
		UpdatedAt: int(time.Now().Unix()),
	}).Error
	return err
}

func (d *Dao) GetByID(ctx context.Context, id int) (res *model.Demo, err error) {
	res = &model.Demo{}
	err = d.DB.Model(&model.Demo{}).Where("id = ?", id).Take(res).Error
	return
}

func (d *Dao) List(ctx context.Context, limit, offset int) (res []*model.Demo, err error) {
	res = make([]*model.Demo, 0)
	err = d.DB.Model(&model.Demo{}).Limit(limit).Offset(offset).Order("id desc").Find(&res).Error
	return
}

func (d *Dao) DeleteByID(ctx context.Context, id int) (err error) {
	err = d.DB.Model(&model.Demo{}).Where("id = ?", id).Delete(&model.Demo{}).Error
	return
}