瀏覽代碼

:pencil2: Edited filter service to reflect code style of other plugins

tags/0.0.4
tomit4 2 年之前
父節點
當前提交
8fe99958e3
共有 2 個檔案被更改,包括 53 行新增45 行删除
  1. 6
    2
      backend/lib/plugins/filter.js
  2. 47
    43
      backend/lib/services/filter.js

+ 6
- 2
backend/lib/plugins/filter.js 查看文件

@@ -1,9 +1,13 @@
1
+const Schmervice = require('@hapipal/schmervice')
1 2
 const FilterRoute = require('../routes/filter/get')
3
+const FilterService = require('../services/filter')
2 4
 
3 5
 module.exports = {
4 6
     name: 'filter-plugin',
5 7
     version: '1.0.0',
6
-    register: async(server, options) => {
8
+    register: async (server, options) => {
9
+        await server.register(Schmervice)
10
+        await server.registerService(FilterService)
7 11
         await server.route(FilterRoute)
8 12
     },
9
-}
13
+}

+ 47
- 43
backend/lib/services/filter.js 查看文件

@@ -1,48 +1,52 @@
1
-const zipcoder = require ('./profile/zipcoder')
1
+const zipcoder = require('./profile/zipcoder')
2
+const Schmervice = require('@hapipal/schmervice')
2 3
 
3
-const byProfileType = (profileList, userProfile) => {
4
-    const isUserOpposite = userProfile.is_poster == 1 ? 0 : 1
5
-    return profileList.filter(profile => (profile.user.is_poster == isUserOpposite))
6
-}
7
-const byNullZip = (profileList) => {
8
-    return profileList.filter(profile => {
9
-        return zipcoder.getZipCodeFromProfile(profile) ? true : false
10
-    })
11
-}
12
-const byDistance = (profileList, max) => {
13
-    return profileList.filter(profile => {
14
-        const profileDistance = Math.floor(parseFloat(profile.distance) * 100)
15
-        const adjustedMaxDistance = Math.floor(parseFloat(max) * 100)
16
-        return profileDistance <= adjustedMaxDistance
17
-    })
18
-}
4
+module.exports = class FilterService extends Schmervice.Service {
5
+    constructor(...args) {
6
+        super(...args)
7
+    }
19 8
 
20
-const byDuration = (profileList, duration) => {
21
-    return profileList.filter(profile => {
22
-        // TODO find duration 
23
-        return profile.duration == duration
24
-    })
25
-}
9
+    byProfileType(profileList, userProfile) {
10
+        const isUserOpposite = userProfile.is_poster === 1 ? 0 : 1
11
+        return profileList.filter(
12
+            profile => profile.user.is_poster === isUserOpposite,
13
+        )
14
+    }
26 15
 
27
-const byPresence = (profileList, presence) => {
28
-    return profileList.filter(profile => {
29
-        // TODO find presence 
30
-        return profile.presence == presence
31
-    })
32
-}
16
+    byNullZip(profileList) {
17
+        return profileList.filter(profile => {
18
+            return zipcoder.getZipCodeFromProfile(profile) ? true : false
19
+        })
20
+    }
33 21
 
34
-const byCertifications = (profileList, certifications) => {
35
-    return profileList.filter(profile => {
36
-        // TODO find certifications 
37
-        return profile.certifications == certifications
38
-    })
39
-}
22
+    byDistance(profileList, max) {
23
+        return profileList.filter(profile => {
24
+            const profileDistance = Math.floor(
25
+                parseFloat(profile.distance) * 100,
26
+            )
27
+            const adjustedMaxDistance = Math.floor(parseFloat(max) * 100)
28
+            return profileDistance <= adjustedMaxDistance
29
+        })
30
+    }
31
+
32
+    byDuration(profileList, duration) {
33
+        return profileList.filter(profile => {
34
+            // TODO find duration
35
+            return profile.duration === duration
36
+        })
37
+    }
40 38
 
41
-module.exports = {
42
-    byProfileType,
43
-    byNullZip,
44
-    byDistance,
45
-    byDuration,
46
-    byPresence,
47
-    byCertifications,
48
-}
39
+    byPresence(profileList, presence) {
40
+        return profileList.filter(profile => {
41
+            // TODO find presence
42
+            return profile.presence === presence
43
+        })
44
+    }
45
+
46
+    byCertifications(profileList, certifications) {
47
+        return profileList.filter(profile => {
48
+            // TODO find certifications
49
+            return profile.certifications === certifications
50
+        })
51
+    }
52
+}

Loading…
取消
儲存