Skip to content

Commit

Permalink
fix: cannot save store records (#203)
Browse files Browse the repository at this point in the history
Co-authored-by: rick <[email protected]>
  • Loading branch information
LinuxSuRen and LinuxSuRen authored Sep 6, 2023
1 parent ada9ca0 commit 51bfd94
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 29 deletions.
59 changes: 42 additions & 17 deletions console/atest-ui/src/views/StoreManager.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,28 @@ import { useI18n } from 'vue-i18n'
const { t } = useI18n()
const emptyStore = {
kind: {},
const emptyStore = function() {
return {
name: '',
url: '',
username: '',
password: '',
kind: {
name: '',
url: ''
},
properties: [{
key: '',
value: ''
}]
} as Store
} as Store
}
const stores = ref([] as Store[])
const dialogVisible = ref(false)
const creatingLoading = ref(false)
const storeFormRef = ref<FormInstance>()
const store = ref(emptyStore)
const createAction = ref(true)
const storeForm = reactive(store)
const storeForm = reactive(emptyStore())
interface Store {
name: string
Expand All @@ -30,6 +38,7 @@ interface Store {
password: string
ready: boolean
kind: {
name: string
url: string
}
properties: Pair[]
Expand Down Expand Up @@ -75,14 +84,23 @@ function editStore(name: string) {
dialogVisible.value = true
stores.value.forEach((e: Store) => {
if (e.name === name) {
store.value = e
setStoreForm(e)
}
})
createAction.value = false
}
function setStoreForm(store: Store) {
storeForm.name = store.name
storeForm.url = store.url
storeForm.username = store.username
storeForm.password = store.password
storeForm.kind = store.kind
storeForm.properties = store.properties
}
function addStore() {
store.value = emptyStore
setStoreForm(emptyStore())
dialogVisible.value = true
createAction.value = true
}
Expand All @@ -96,14 +114,9 @@ const submitForm = async (formEl: FormInstance | undefined) => {
if (valid) {
creatingLoading.value = true
// remove empty pair
store.value.properties = store.value.properties.filter(
(e) => e.key !== ''
)
const requestOptions = {
method: 'POST',
body: JSON.stringify(store.value)
body: JSON.stringify(storeForm)
}
let api = '/server.Runner/CreateStore'
Expand All @@ -112,22 +125,31 @@ const submitForm = async (formEl: FormInstance | undefined) => {
}
fetch(api, requestOptions)
.then((response) => response.json())
.then((response) => {
if (!response.ok) {
throw new Error(response.statusText)
} else {
response.json()
}
})
.then(() => {
creatingLoading.value = false
loadStores()
dialogVisible.value = false
formEl.resetFields()
})
.catch((e) => {
ElMessage.error('Oops, ' + e)
})
creatingLoading.value = false
}
})
}
function updateKeys() {
const props = store.value.properties
const props = storeForm.properties
let lastItem = props[props.length - 1]
if (lastItem.key !== '') {
store.value.properties.push({
storeForm.properties.push({
key: '',
value: ''
})
Expand Down Expand Up @@ -209,6 +231,9 @@ function updateKeys() {
<el-form-item :label="t('field.password')" prop="password">
<el-input v-model="storeForm.password" type="password" test-id="store-form-password" />
</el-form-item>
<el-form-item :label="t('field.plugin')" prop="pluginName">
<el-input v-model="storeForm.kind.name" test-id="store-form-plugin-name" />
</el-form-item>
<el-form-item :label="t('field.plugin')" prop="plugin">
<el-input v-model="storeForm.kind.url" test-id="store-form-plugin" />
</el-form-item>
Expand Down
25 changes: 13 additions & 12 deletions pkg/testing/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,7 @@ func (s *storeFactory) DeleteStore(name string) (err error) {
break
}
}
var data []byte
if data, err = yaml.Marshal(storeConfig); err == nil {
err = os.WriteFile(path.Join(s.configDir, "stores.yaml"), data, 0644)
}
err = s.save(storeConfig)
}
return
}
Expand All @@ -171,10 +168,7 @@ func (s *storeFactory) UpdateStore(store Store) (err error) {
}

if exist {
var data []byte
if data, err = yaml.Marshal(storeConfig); err == nil {
err = os.WriteFile(path.Join(s.configDir, "stores.yaml"), data, 0644)
}
err = s.save(storeConfig)
} else {
err = fmt.Errorf("store %s is not exists", store.Name)
}
Expand All @@ -196,17 +190,24 @@ func (s *storeFactory) CreateStore(store Store) (err error) {

if !exist {
storeConfig.Stores = append(storeConfig.Stores, store)
var data []byte
if data, err = yaml.Marshal(storeConfig); err == nil {
err = os.WriteFile(path.Join(s.configDir, "stores.yaml"), data, 0644)
}
err = s.save(storeConfig)
} else {
err = fmt.Errorf("store %s already exists", store.Name)
}
}
return
}

func (s *storeFactory) save(storeConfig *StoreConfig) (err error) {
if err = os.MkdirAll(s.configDir, 0755);err==nil{
var data []byte
if data, err = yaml.Marshal(storeConfig); err == nil {
err = os.WriteFile(path.Join(s.configDir, "stores.yaml"), data, 0644)
}
}
return
}

func (s *storeFactory) GetStoreKinds() (kinds []StoreKind, err error) {
var storeConfig *StoreConfig
if storeConfig, err = s.getStoreConfig(); err == nil {
Expand Down

0 comments on commit 51bfd94

Please sign in to comment.