github.com/condensat/bank-core@v0.1.0/database/query/accountstate.go (about) 1 // Copyright 2020 Condensat Tech. All rights reserved. 2 // Use of this source code is governed by a MIT 3 // license that can be found in the LICENSE file. 4 5 package query 6 7 import ( 8 "errors" 9 10 "github.com/condensat/bank-core/database" 11 "github.com/condensat/bank-core/database/model" 12 13 "github.com/jinzhu/gorm" 14 ) 15 16 var ( 17 ErrAccountStateNotFound = errors.New("Account State Not Found") 18 ErrInvalidAccountID = errors.New("Invalid AccountID") 19 ErrInvalidReferenceID = errors.New("Invalid ReferenceID") 20 ErrInvalidAccountState = errors.New("Invalid Account State") 21 ErrAccountIsDisabled = errors.New("Account Is Disabled") 22 ) 23 24 // AddOrUpdateAccountState 25 func AddOrUpdateAccountState(db database.Context, accountSate model.AccountState) (model.AccountState, error) { 26 var result model.AccountState 27 gdb := db.DB().(*gorm.DB) 28 if db == nil { 29 return result, database.ErrInvalidDatabase 30 } 31 32 if accountSate.AccountID == 0 { 33 return result, ErrInvalidAccountID 34 } 35 36 if !accountSate.State.Valid() { 37 return result, ErrInvalidAccountState 38 } 39 40 err := gdb. 41 Where(model.AccountState{ 42 AccountID: accountSate.AccountID, 43 }). 44 Assign(accountSate). 45 FirstOrCreate(&result).Error 46 47 return result, err 48 } 49 50 func GetAccountStatusByAccountID(db database.Context, accountID model.AccountID) (model.AccountState, error) { 51 var result model.AccountState 52 53 gdb := db.DB().(*gorm.DB) 54 if gdb == nil { 55 return result, database.ErrInvalidDatabase 56 } 57 58 if accountID == 0 { 59 return result, ErrInvalidAccountID 60 } 61 62 err := gdb. 63 Where(model.AccountState{ 64 AccountID: accountID, 65 }). 66 First(&result).Error 67 68 return result, err 69 }