2020-04-01 00:16:03 +13:00
|
|
|
import api from "builderStore/api"
|
|
|
|
import { getNewRecord, getNewInstance } from "components/common/core"
|
2020-03-24 03:26:38 +13:00
|
|
|
|
2020-03-26 00:28:45 +13:00
|
|
|
export async function createUser(password, user, { appname, instanceId }) {
|
2020-03-25 05:17:10 +13:00
|
|
|
const CREATE_USER_URL = `/_builder/instance/${appname}/${instanceId}/api/createUser`
|
2020-03-26 00:28:45 +13:00
|
|
|
const response = await api.post(CREATE_USER_URL, { user, password })
|
2020-03-25 05:17:10 +13:00
|
|
|
return await response.json()
|
|
|
|
}
|
|
|
|
|
2020-03-24 03:26:38 +13:00
|
|
|
export async function createDatabase(appname, instanceName) {
|
|
|
|
const CREATE_DATABASE_URL = `/_builder/instance/_master/0/api/record/`
|
|
|
|
const database = getNewInstance(appname, instanceName)
|
|
|
|
const response = await api.post(CREATE_DATABASE_URL, database)
|
|
|
|
return await response.json()
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function deleteRecord(record, { appname, instanceId }) {
|
|
|
|
const DELETE_RECORDS_URL = `/_builder/instance/${appname}/${instanceId}/api/record${record.key}`
|
|
|
|
const response = await api.delete(DELETE_RECORDS_URL)
|
|
|
|
return response
|
2020-03-28 00:28:30 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function loadRecord(key, { appname, instanceId }) {
|
|
|
|
const LOAD_RECORDS_URL = `/_builder/instance/${appname}/${instanceId}/api/record${key}`
|
|
|
|
const response = await api.get(LOAD_RECORDS_URL)
|
|
|
|
return await response.json()
|
2020-03-24 03:26:38 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function saveRecord(record, { appname, instanceId }) {
|
|
|
|
let recordBase = { ...record }
|
|
|
|
|
|
|
|
// brand new record
|
2020-03-26 05:59:32 +13:00
|
|
|
// car-model-id or name/specific-car-id/manus
|
2020-03-24 03:26:38 +13:00
|
|
|
if (record.collectionName) {
|
|
|
|
const collectionKey = `/${record.collectionName}`
|
|
|
|
recordBase = getNewRecord(recordBase, collectionKey)
|
|
|
|
recordBase = overwritePresentProperties(recordBase, record)
|
2020-03-22 09:39:37 +13:00
|
|
|
}
|
2020-03-13 03:23:29 +13:00
|
|
|
|
2020-03-24 03:26:38 +13:00
|
|
|
const SAVE_RECORDS_URL = `/_builder/instance/${appname}/${instanceId}/api/record/`
|
|
|
|
const response = await api.post(SAVE_RECORDS_URL, recordBase)
|
|
|
|
return await response.json()
|
|
|
|
}
|
2020-03-21 07:47:01 +13:00
|
|
|
|
2020-03-24 03:26:38 +13:00
|
|
|
export async function fetchDataForView(viewName, { appname, instanceId }) {
|
|
|
|
const FETCH_RECORDS_URL = `/_builder/instance/${appname}/${instanceId}/api/listRecords/${viewName}`
|
2020-03-13 03:23:29 +13:00
|
|
|
|
2020-03-24 03:26:38 +13:00
|
|
|
const response = await api.get(FETCH_RECORDS_URL)
|
|
|
|
return await response.json()
|
|
|
|
}
|
2020-03-13 03:23:29 +13:00
|
|
|
|
2020-03-24 03:26:38 +13:00
|
|
|
function overwritePresentProperties(baseObj, overwrites) {
|
|
|
|
const base = { ...baseObj }
|
2020-03-13 03:23:29 +13:00
|
|
|
|
2020-03-24 03:26:38 +13:00
|
|
|
for (let key in base) {
|
|
|
|
if (overwrites[key]) base[key] = overwrites[key]
|
2020-03-23 00:35:35 +13:00
|
|
|
}
|
2020-03-24 03:26:38 +13:00
|
|
|
return base
|
|
|
|
}
|