Przeglądaj źródła

:recycle: added missing files; whoops | using grouping schema to validate incoming membership records

tags/0.0.1
TOJ 4 lat temu
rodzic
commit
5efa3fd0c4

+ 1
- 1
backend/lib/routes/membership/active.js Wyświetl plik

@@ -40,7 +40,7 @@ module.exports = {
40 40
         tags: ['api'],
41 41
         /** Protect this route with authentication? */
42 42
         auth: false,
43
-
43
+        cors: true,
44 44
         handler: async function (request, h) {
45 45
             const { membershipService } = request.services()
46 46
             const membershipType = request.query.type

+ 1
- 1
backend/lib/routes/user/list-profiles.js Wyświetl plik

@@ -52,7 +52,7 @@ module.exports = {
52 52
         tags: ['api'],
53 53
         /** Protect this route with authentication? */
54 54
         auth: false,
55
-
55
+        cors: true,
56 56
         handler: async function (request, h) {
57 57
             const { userService, profileService } = request.server.services()
58 58
             const userId = request.params.user_id

docs/routes.md → docs/frontend/routes.md Wyświetl plik


+ 10
- 2
frontend/src/App.vue Wyświetl plik

@@ -13,7 +13,10 @@ import { ref, onMounted } from 'vue'
13 13
 import { profileForm } from '@/utils/forms'
14 14
 import { Connector } from '@/utils/db'
15 15
 
16
-import { fetchProfilesByUserId } from '@/services'
16
+import {
17
+    fetchProfilesByUserId,
18
+    fetchMembershipsByProfileId,
19
+} from '@/services'
17 20
 
18 21
 import helloWorld from '@/components/HelloWorld.vue'
19 22
 import card from '@/components/card.vue'
@@ -58,7 +61,12 @@ export default {
58 61
 
59 62
         const t = async () => {
60 63
             const tempProfiles = await fetchProfilesByUserId(1)
61
-            console.log(tempProfiles)
64
+            const groupings = []
65
+            for(let p of tempProfiles) {
66
+                const memberships = await fetchMembershipsByProfileId(p.profile_id)
67
+                groupings.push(memberships)
68
+            }
69
+            console.log(groupings)
62 70
         }
63 71
         t()
64 72
         return {

+ 5
- 8
frontend/src/entities/grouping/grouping.js Wyświetl plik

@@ -6,21 +6,18 @@ import { groupingSchema } from './grouping.schema'
6 6
 /** Class representing a grouping */
7 7
 class Grouping extends _baseRecord {
8 8
     /**
9
-     * Create the grouping.
9
+     * Create the grouping based on membership
10 10
      * @extends _baseRecord
11
-     * @param {string} email
12
-     * @param {object} grouping - spread destructured args
11
+     * @param {object} membership - spread destructured args
13 12
      * @return {Grouping} the grouping instance object
14 13
      */
15
-    constructor({ ...grouping }) {
14
+    constructor({ ...membership }) {
16 15
         super()
17 16
 
18 17
         this.type = this.constructor.name.toLowerCase()
19 18
 
20
-        /**  Fields */
21
-        this.groupingId = groupingId
22
-        this.groupingName = groupingName
23
-        this.groupingType = groupingType
19
+        /** Pass destructured data to the module system */
20
+        Object.assign(this, membership)
24 21
 
25 22
         return this
26 23
     }

+ 4
- 4
frontend/src/entities/grouping/grouping.schema.js Wyświetl plik

@@ -17,12 +17,12 @@ const groupingSchema = {
17 17
         type: Joi.string(),
18 18
 
19 19
         /** our fields */
20
-        groupingId: Joi.number().required(),
21
-        groupingName: Joi.string().required(),
22
-        groupingType: Joi.string().required()
20
+        grouping_id: Joi.number().required(),
21
+        grouping_name: Joi.string(),
22
+        grouping_type: Joi.string()
23 23
     }),
24 24
     /** fields required before saving */
25
-    required: [ 'groupingId', 'groupingName', 'groupingType' ],
25
+    required: [ 'grouping_id', 'grouping_name', 'grouping_type' ],
26 26
     validate(instance) {
27 27
         return this.properties.validate(instance)
28 28
     }

+ 23
- 0
frontend/src/services/grouping.service.js Wyświetl plik

@@ -0,0 +1,23 @@
1
+import { db } from '../utils/db'
2
+import { Grouping } from '../entities'
3
+
4
+/**
5
+ * Get Memberships associated with a single Profile from the database and
6
+ * create a class from the data and
7
+ * validate the incoming against the schema
8
+ * @param {number} profileId
9
+ * @returns {array} instantiated Profile objects (see: /entites/profile)
10
+ */
11
+const fetchMembershipsByProfileId = async profileId => {
12
+    const membershipsForProfileId = await db.get(`/membership/${profileId}`)
13
+    const validGroupingInstances = []
14
+    for(let membership of membershipsForProfileId) {
15
+        const grouping = new Grouping(membership)
16
+        if(grouping.isValid()) {
17
+            validGroupingInstances.push(grouping)
18
+        }
19
+    }
20
+    return validGroupingInstances
21
+}
22
+
23
+export { fetchMembershipsByProfileId }

+ 2
- 1
frontend/src/services/index.js Wyświetl plik

@@ -1 +1,2 @@
1
-export * from './profile.service'
1
+export * from './profile.service'
2
+export * from './grouping.service'

+ 8
- 7
frontend/src/services/profile.service.js Wyświetl plik

@@ -2,21 +2,22 @@ import { db } from '../utils/db'
2 2
 import { Profile } from '../entities/profile'
3 3
 
4 4
 /**
5
- * Get a single Profile from the database and
5
+ * Get Profiles associated with a single user from the database and
6 6
  * create a class from the data and
7
- * validate the incoming
7
+ * validate the incoming against the schema
8 8
  * @param {number} userId
9
+ * @returns {array} instantiated Profile objects (see: /entites/profile)
9 10
  */
10 11
 const fetchProfilesByUserId = async userId => {
11 12
     const profilesForUserId = await db.get(`/user/${userId}/profiles`)
12
-    const profileInstances = []
13
-    profilesForUserId.map(profileData => {
13
+    const validProfileInstances = []
14
+    for(let profileData of profilesForUserId) {
14 15
         const profile = new Profile(profileData)
15 16
         if(profile.isValid()) {
16
-            profileInstances.push(profile)
17
+            validProfileInstances.push(profile)
17 18
         }
18
-    })
19
-    return profileInstances
19
+    }
20
+    return validProfileInstances
20 21
 }
21 22
 
22 23
 export { fetchProfilesByUserId }

Ładowanie…
Anuluj
Zapisz