2018-05-10 17:45:23 +08:00
|
|
|
package dao
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
"github.com/goodrain/rainbond/db/model"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AppDaoImpl struct {
|
|
|
|
DB *gorm.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppDaoImpl) AddModel(mo model.Interface) error {
|
2018-05-10 18:54:32 +08:00
|
|
|
app, ok := mo.(*model.AppStatus)
|
2018-05-10 17:45:23 +08:00
|
|
|
if !ok {
|
|
|
|
return errors.New("Failed to convert interface to AppStatus")
|
|
|
|
}
|
|
|
|
|
2018-05-10 18:54:32 +08:00
|
|
|
var old model.AppStatus
|
2018-05-10 17:45:23 +08:00
|
|
|
if ok := a.DB.Where("group_key = ? and version = ?", app.GroupKey, app.Version).Find(&old).RecordNotFound(); ok {
|
|
|
|
if err := a.DB.Create(app).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppDaoImpl) UpdateModel(mo model.Interface) error {
|
2018-05-10 18:54:32 +08:00
|
|
|
app, ok := mo.(*model.AppStatus)
|
2018-05-10 17:45:23 +08:00
|
|
|
if !ok {
|
|
|
|
return errors.New("Failed to convert interface to AppStatus")
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.DB.Table(app.TableName()).
|
|
|
|
Where("group_key = ? and version = ?", app.GroupKey, app.Version).
|
|
|
|
Update(app).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppDaoImpl) DeleteModel(groupKey string, arg ...interface{}) error {
|
|
|
|
if len(arg) < 1 {
|
|
|
|
return errors.New("Must define version for delete AppStatus in mysql.")
|
|
|
|
}
|
|
|
|
|
|
|
|
version, ok := arg[0].(string)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("Failed to convert interface to string")
|
|
|
|
}
|
|
|
|
|
2018-05-10 18:54:32 +08:00
|
|
|
var app model.AppStatus
|
2018-05-11 23:49:02 +08:00
|
|
|
if ok := a.DB.Where("group_key = ? and version = ?", app.GroupKey, app.Version).Find(&app).RecordNotFound(); ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-10 17:45:23 +08:00
|
|
|
return a.DB.Where("group_key = ? and version = ?", groupKey, version).Delete(&app).Error
|
|
|
|
}
|
|
|
|
|
2018-05-15 11:03:03 +08:00
|
|
|
func (a *AppDaoImpl) DeleteModelByEventId(eventId string) error {
|
|
|
|
var app model.AppStatus
|
|
|
|
if ok := a.DB.Where("event_id = ?", eventId).Find(&app).RecordNotFound(); ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.DB.Where("event_id = ?", eventId).Delete(&app).Error
|
|
|
|
}
|
|
|
|
|
2018-05-10 17:45:23 +08:00
|
|
|
func (a *AppDaoImpl) Get(groupKey, version string) (interface{}, error) {
|
2018-05-10 18:54:32 +08:00
|
|
|
var app model.AppStatus
|
2018-05-10 17:45:23 +08:00
|
|
|
err := a.DB.Where("group_key = ? and version = ?", groupKey, version).First(&app).Error
|
|
|
|
|
|
|
|
return &app, err
|
|
|
|
}
|
2018-05-14 14:30:32 +08:00
|
|
|
|
|
|
|
func (a *AppDaoImpl) GetByEventId(eventId string) (interface{}, error) {
|
|
|
|
var app model.AppStatus
|
|
|
|
err := a.DB.Where("event_id = ?", eventId).First(&app).Error
|
|
|
|
|
|
|
|
return &app, err
|
|
|
|
}
|