Advertisement
viettiennguyen029

Untitled

Jan 24th, 2022
1,073
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Check if contract address is existed
  2.     existedContract, err := s.repos.GetContractRepo().
  3.         GetUnscopedContractByAddr(contract.ContractAddr)
  4.     if err != nil {
  5.         // Not existed
  6.         if err == consts.ErrRecordNotFound {
  7.             // Add new imported contract to db
  8.             err = s.repos.GetContractRepo().Create(contract)
  9.             if err != nil {
  10.                 return err
  11.             }
  12.         } else {
  13.             return err
  14.         }
  15.     } else {
  16.         if existedContract.DeletedAt != 0 {
  17.             fmt.Println("Restore")
  18.             // Restore contract address
  19.  
  20.             err = s.repos.GetContractRepo().Restore(existedContract.ContractAddr)
  21.             if err != nil {
  22.                 return err
  23.             }
  24.             contract = existedContract
  25.         } else {
  26.             fmt.Println("Duplicated")
  27.             return consts.ErrDuplicatedContractAddr
  28.         }
  29.     }
  30.  
  31.  
  32.  
  33.  
  34. func (repo *ContractRepo) GetUnscopedContractByAddr(contractAddr string) (Contract, error) {
  35.     var contract Contract
  36.  
  37.     if res := repo.db.Unscoped().Where("contract_addr = ?", contractAddr).First(&contract); res.Error != nil {
  38.         return Contract{}, res.Error
  39.     }
  40.  
  41.     return contract, nil
  42. }
  43.  
  44. func (repo *ContractRepo) Restore(addr string) error {
  45.     // Update contract info
  46.     return repo.db.Exec("UPDATE contracts SET deleted_at = ? WHERE contract_addr = ?", 0, addr).Error
  47.  
  48.     //resturn repo.db.Debug().Unscoped().Where("contract_addr = ?", addr).Update("deleted_at = ?", 0).Error
  49.  
  50. }
  51.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement