diff --git a/app/http/formatters/bannerformatter/banner.go b/app/http/formatters/bannerformatter/banner.go deleted file mode 100644 index 032d521..0000000 --- a/app/http/formatters/bannerformatter/banner.go +++ /dev/null @@ -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 -} \ No newline at end of file diff --git a/app/http/formatters/bannerformatter/banner_test.go b/app/http/formatters/bannerformatter/banner_test.go deleted file mode 100644 index d126229..0000000 --- a/app/http/formatters/bannerformatter/banner_test.go +++ /dev/null @@ -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") - } - } -} diff --git a/app/models/base.go b/app/models/base.go new file mode 100644 index 0000000..f5438db --- /dev/null +++ b/app/models/base.go @@ -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 +} diff --git a/bootstrap/bootstrap.go b/bootstrap/bootstrap.go index 19c7906..6e22df4 100644 --- a/bootstrap/bootstrap.go +++ b/bootstrap/bootstrap.go @@ -28,6 +28,7 @@ func Bootstrap(conf *config.Config) (err error) { //注册db服务 //第一个参数为注入别名,第二个参数为配置,第三个参数可选为是否懒加载 err = db.Pr.Register(db.SingletonMain, conf.Db) + if err != nil { return }