封装基础数据操作

This commit is contained in:
qiyunfanbo126.com 2024-04-30 17:20:44 +08:00
parent 2f268160bb
commit 60601f3220
4 changed files with 41 additions and 79 deletions

View File

@ -1,34 +0,0 @@
package bannerformatter
import (
"qteam/app/models/bannermodel"
)
type BannerFormatter struct {
Id int `json:"id"`
Title string `json:"title"`
Img string `json:"image"`
Url string `json:"url"`
}
func FormatList(bannerList []*bannermodel.Banner) (res []*BannerFormatter) {
res = make([]*BannerFormatter, len(bannerList))
for k, banner := range bannerList {
one := FormatOne(banner)
res[k] = one
}
return res
}
//单条消息的格式化,
func FormatOne(banner *bannermodel.Banner) (res *BannerFormatter) {
res = &BannerFormatter{
Id: int(banner.Id),
Title: banner.Title,
Img: banner.ImageUrl,
Url: banner.Url,
}
return
}

View File

@ -1,45 +0,0 @@
package bannerformatter
import (
"testing"
"qteam/app/models/bannermodel"
)
func TesFormatOne(t *testing.T) {
a := &bannermodel.Banner{
Id: 1,
Title: "test",
ImageUrl: "http://x/1.jpg",
Url: "http://x",
Status: "1",
}
b := FormatOne(a)
if b.Title != a.Title || b.Img != a.ImageUrl || b.Url != a.Url {
t.Error("FormatOne not same")
}
}
func TesFormatList(t *testing.T) {
a := make([]*bannermodel.Banner, 2)
a[0] = &bannermodel.Banner{
Id: 1,
Title: "test",
ImageUrl: "http://x1/1.jpg",
Url: "http://x1",
Status: "1",
}
a[1] = &bannermodel.Banner{
Id: 2,
Title: "test2",
ImageUrl: "http://x/2.jpg",
Url: "http://x2",
Status: "2",
}
b := FormatList(a)
for k, v := range b {
if v.Title != a[k].Title || v.Img != a[k].ImageUrl || v.Url != a[k].Url {
t.Error("FormatList not same")
}
}
}

40
app/models/base.go Normal file
View File

@ -0,0 +1,40 @@
package models
import (
"xorm.io/xorm"
)
func GetListByPage(page int, pageSize int, where []map[string]interface{}, db xorm.EngineGroup) (count int64, data []interface{}, err error) {
db.ShowSQL(true)
var rs []interface{}
var session = db.Where("1 = 1")
for k, v := range where {
session = session.Where(k, v)
}
count, err = session.Limit(pageSize, (page-1)*page).FindAndCount(&rs)
return
}
func GetOne(where []map[string]interface{}, db xorm.EngineGroup) (rs interface{}, err error) {
var session = db.Where("1 = 1")
for k, v := range where {
session = session.Where(k, v)
}
err = session.Find(&rs)
return
}
func Insert(pb interface{}, db xorm.EngineGroup) (int64, error) {
return db.Insert(pb)
}
func UpdateById(id int64, pb interface{}, db xorm.EngineGroup) error {
_, err := db.Where("id = ?", id).Update(pb)
return err
}
func DetleById(id int64, db xorm.EngineGroup) (err error) {
_, err = db.Where("id = ?", id).Delete()
return
}

View File

@ -28,6 +28,7 @@ func Bootstrap(conf *config.Config) (err error) {
//注册db服务
//第一个参数为注入别名,第二个参数为配置,第三个参数可选为是否懒加载
err = db.Pr.Register(db.SingletonMain, conf.Db)
if err != nil {
return
}