Browse Source

example data operation

gaoshuaixing 5 years ago
parent
commit
1b7fcffcd6
4 changed files with 112 additions and 0 deletions
  1. 1 0
      app/const/storageKey.js
  2. 47 0
      app/controller/v1/example.js
  3. 8 0
      app/router/example.js
  4. 56 0
      app/service/storage.js

+ 1 - 0
app/const/storageKey.js

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

+ 47 - 0
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;

+ 8 - 0
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);
 };

+ 56 - 0
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;