example data operation

This commit is contained in:
gaoshuaixing
2021-01-08 14:35:31 +08:00
parent 3354abe943
commit 1b7fcffcd6
4 changed files with 112 additions and 0 deletions

View File

@@ -3,4 +3,5 @@
module.exports = {
EGG_CONFIG: 'egg_config',
ELECTRON_IPC: 'electron_ipc',
TEST_DATA: 'test_data'
};

View File

@@ -75,6 +75,53 @@ class ExampleController extends BaseController {
self.sendSuccess(data);
}
async addTestData() {
const self = this;
const { service } = this;
const data = {};
const userInfo = {
name: 'jame',
age: 18,
gender: 'man'
}
await service.storage.addTestData(userInfo);
self.sendSuccess(data);
}
async delTestData() {
const self = this;
const { service } = this;
const data = {};
const name = 'jame';
await service.storage.delTestData(name);
self.sendSuccess(data);
}
async updateTestData() {
const self = this;
const { service } = this;
const data = {};
const name = 'jame';
const age = 20;
await service.storage.updateTestData(name, age);
self.sendSuccess(data);
}
async getTestData() {
const self = this;
const { service } = this;
const data = {};
const name = 'jame';
const user = await service.storage.getTestData(name);
data.user = user;
self.sendSuccess(data);
}
}
module.exports = ExampleController;

View File

@@ -11,4 +11,12 @@ module.exports = app => {
router.post('/api/v1/example/uploadFile', controller.v1.example.uploadFile);
// get ws url
router.post('/api/v1/example/getWsUrl', controller.v1.example.getWsUrl);
// add test data
router.post('/api/v1/example/addTestData', controller.v1.example.addTestData);
// delete test data
router.post('/api/v1/example/delTestData', controller.v1.example.delTestData);
// update test data
router.post('/api/v1/example/updateTestData', controller.v1.example.updateTestData);
// get test data
router.post('/api/v1/example/getTestData', controller.v1.example.getTestData);
};

View File

@@ -50,6 +50,62 @@ class StorageService extends BaseService {
return storageDir;
}
/*
* add Test data
*/
async addTestData(user) {
const key = storageKey.TEST_DATA;
if (!this.instance().has(key).value()) {
this.instance().set(key, []).write();
}
const data = this.instance()
.get(key)
.push(user)
.write();
return data;
}
/*
* del Test data
*/
async delTestData(name = '') {
const key = storageKey.TEST_DATA;
const data = this.instance()
.get(key)
.remove({name: name})
.write();
return data;
}
/*
* update Test data
*/
async updateTestData(name= '', age = 0) {
const key = storageKey.TEST_DATA;
const data = this.instance()
.get(key)
.find({name: name})
.assign({ age: age})
.write();
return data;
}
/*
* get Test data
*/
async getTestData(name = '') {
const key = storageKey.TEST_DATA;
const data = this.instance()
.get(key)
.find({name: name})
.value();
return data;
}
}
module.exports = StorageService;