2018-05-10 17:45:23 +08:00
package dao
import (
2018-05-23 11:08:44 +08:00
"fmt"
2018-05-10 17:45:23 +08:00
"github.com/goodrain/rainbond/db/model"
2018-05-23 11:08:44 +08:00
"github.com/jinzhu/gorm"
2018-05-10 17:45:23 +08:00
"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-15 12:26:17 +08:00
if ok := a . DB . Where ( "event_id = ?" , app . EventID ) . Find ( & old ) . RecordNotFound ( ) ; ok {
2018-05-10 17:45:23 +08:00
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 ( ) ) .
2018-05-15 12:26:17 +08:00
Where ( "event_id = ?" , app . EventID ) .
2018-05-10 17:45:23 +08:00
Update ( app ) . Error
}
2018-05-21 22:15:46 +08:00
func ( a * AppDaoImpl ) DeleteModelByEventId ( eventID string ) error {
2018-05-15 11:03:03 +08:00
var app model . AppStatus
2018-05-21 22:15:46 +08:00
if ok := a . DB . Where ( "event_id = ?" , eventID ) . Find ( & app ) . RecordNotFound ( ) ; ok {
2018-05-15 11:03:03 +08:00
return nil
}
2018-05-21 22:15:46 +08:00
return a . DB . Where ( "event_id = ?" , eventID ) . Delete ( & app ) . Error
2018-05-15 11:03:03 +08:00
}
2018-05-21 22:15:46 +08:00
func ( a * AppDaoImpl ) GetByEventId ( eventID string ) ( * model . AppStatus , error ) {
2018-05-14 14:30:32 +08:00
var app model . AppStatus
2018-05-21 22:15:46 +08:00
err := a . DB . Where ( "event_id = ?" , eventID ) . First ( & app ) . Error
2018-05-14 14:30:32 +08:00
return & app , err
}
2018-05-23 11:08:44 +08:00
//AppBackupDaoImpl group app backup info store mysql impl
type AppBackupDaoImpl struct {
DB * gorm . DB
}
//AddModel AddModel
func ( a * AppBackupDaoImpl ) AddModel ( mo model . Interface ) error {
app , ok := mo . ( * model . AppBackup )
if ! ok {
return errors . New ( "Failed to convert interface to AppStatus" )
}
var old model . AppBackup
if ok := a . DB . Where ( "backup_id = ?" , app . BackupID ) . Find ( & old ) . RecordNotFound ( ) ; ok {
if err := a . DB . Create ( app ) . Error ; err != nil {
return err
}
2018-05-23 12:17:34 +08:00
return nil
2018-05-23 11:08:44 +08:00
}
return fmt . Errorf ( "backup info exist with id %s" , app . BackupID )
}
//UpdateModel UpdateModel
func ( a * AppBackupDaoImpl ) UpdateModel ( mo model . Interface ) error {
app , ok := mo . ( * model . AppBackup )
if ! ok {
return errors . New ( "Failed to convert interface to AppStatus" )
}
if app . ID == 0 {
return errors . New ( "Primary id can not be 0 when update" )
}
2018-05-23 12:38:23 +08:00
return a . DB . Table ( app . TableName ( ) ) . Where ( "backup_id = ?" , app . BackupID ) . Update ( app ) . Error
2018-05-23 11:08:44 +08:00
}
//CheckHistory CheckHistory
func ( a * AppBackupDaoImpl ) CheckHistory ( groupID , version string ) bool {
var app model . AppBackup
2018-06-07 13:18:53 +08:00
exist := a . DB . Where ( "((group_id = ? and status in (?)) or version=?) and deleted=? " , groupID , [ ] string { "starting" , "restore" } , version , false ) . Find ( & app ) . RecordNotFound ( )
2018-05-23 12:17:34 +08:00
return ! exist
2018-05-23 11:08:44 +08:00
}
//GetAppBackups GetAppBackups
func ( a * AppBackupDaoImpl ) GetAppBackups ( groupID string ) ( [ ] * model . AppBackup , error ) {
var apps [ ] * model . AppBackup
2018-06-07 13:18:53 +08:00
if err := a . DB . Where ( "group_id = ? and deleted=?" , groupID , false ) . Find ( & apps ) . Error ; err != nil {
2018-05-23 11:08:44 +08:00
return nil , err
}
return apps , nil
}
//DeleteAppBackup DeleteAppBackup
func ( a * AppBackupDaoImpl ) DeleteAppBackup ( backupID string ) error {
var app model . AppBackup
if err := a . DB . Where ( "backup_id = ?" , backupID ) . Delete ( & app ) . Error ; err != nil {
return err
}
return nil
}
//GetAppBackup GetAppBackup
func ( a * AppBackupDaoImpl ) GetAppBackup ( backupID string ) ( * model . AppBackup , error ) {
var app model . AppBackup
2018-06-07 13:18:53 +08:00
if err := a . DB . Where ( "backup_id = ? and deleted=?" , backupID , false ) . Find ( & app ) . Error ; err != nil {
2018-05-23 11:08:44 +08:00
return nil , err
}
return & app , nil
}
2018-06-07 13:18:53 +08:00
//GetDeleteAppBackup GetDeleteAppBackup
func ( a * AppBackupDaoImpl ) GetDeleteAppBackup ( backupID string ) ( * model . AppBackup , error ) {
var app model . AppBackup
if err := a . DB . Where ( "backup_id = ? and deleted=?" , backupID , true ) . Find ( & app ) . Error ; err != nil {
return nil , err
}
return & app , nil
}
//GetDeleteAppBackups GetDeleteAppBackups
func ( a * AppBackupDaoImpl ) GetDeleteAppBackups ( ) ( [ ] * model . AppBackup , error ) {
var apps [ ] * model . AppBackup
if err := a . DB . Where ( "deleted=?" , true ) . Find ( & apps ) . Error ; err != nil {
return nil , err
}
return apps , nil
}