From 1b7fcffcd6267ee6c826efd3cb67afe9c40251e3 Mon Sep 17 00:00:00 2001 From: gaoshuaixing Date: Fri, 8 Jan 2021 14:35:31 +0800 Subject: [PATCH] example data operation --- app/const/storageKey.js | 1 + app/controller/v1/example.js | 47 ++++++++++++++++++++++++++++++ app/router/example.js | 8 ++++++ app/service/storage.js | 56 ++++++++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+) diff --git a/app/const/storageKey.js b/app/const/storageKey.js index 2ebb8bb..5f831c8 100644 --- a/app/const/storageKey.js +++ b/app/const/storageKey.js @@ -3,4 +3,5 @@ module.exports = { EGG_CONFIG: 'egg_config', ELECTRON_IPC: 'electron_ipc', + TEST_DATA: 'test_data' }; \ No newline at end of file diff --git a/app/controller/v1/example.js b/app/controller/v1/example.js index 3c05ce8..7760339 100644 --- a/app/controller/v1/example.js +++ b/app/controller/v1/example.js @@ -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; diff --git a/app/router/example.js b/app/router/example.js index 6e6bfb8..0c6aab8 100644 --- a/app/router/example.js +++ b/app/router/example.js @@ -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); }; \ No newline at end of file diff --git a/app/service/storage.js b/app/service/storage.js index b4156c1..d4f9893 100644 --- a/app/service/storage.js +++ b/app/service/storage.js @@ -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;