Преглед изворни кода

:recycle: integrating entity system with db connector | integrating entity schemas with entity system | moved some docs | added grouping, survey, and response entities | altered Joi standard import

tags/0.0.1
TOJ пре 4 година
родитељ
комит
bdfe31e224

+ 8
- 1
frontend/src/App.vue Прегледај датотеку

@@ -13,6 +13,8 @@ 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'
17
+
16 18
 import helloWorld from '@/components/HelloWorld.vue'
17 19
 import card from '@/components/card.vue'
18 20
 import form from '@/components/form.vue'
@@ -54,7 +56,12 @@ export default {
54 56
         }
55 57
         onMounted(compileForm)
56 58
 
57
-        return { 
59
+        const t = async () => {
60
+            const tempProfiles = await fetchProfilesByUserId(1)
61
+            console.log(tempProfiles)
62
+        }
63
+        t()
64
+        return {
58 65
             cards,
59 66
             remove,
60 67
             completedForm

+ 2
- 2
frontend/src/components/HelloWorld.vue Прегледај датотеку

@@ -36,8 +36,8 @@ state.profile = new Profile({
36 36
     state: 'lollipop',
37 37
     zip: 90002
38 38
 })
39
-console.log(state.profile)
40
-console.log(state.profile.isValid())
39
+// console.log(state.profile)
40
+// console.log(state.profile.isValid())
41 41
 </script>
42 42
 
43 43
 <style lang="postcss">

+ 49
- 0
frontend/src/entities/grouping/grouping.js Прегледај датотеку

@@ -0,0 +1,49 @@
1
+/** @module entities/grouping */
2
+
3
+import { _baseRecord } from '..'
4
+import { groupingSchema } from './grouping.schema'
5
+
6
+/** Class representing a grouping */
7
+class Grouping extends _baseRecord {
8
+    /**
9
+     * Create the grouping.
10
+     * @extends _baseRecord
11
+     * @param {string} email
12
+     * @param {object} grouping - spread destructured args
13
+     * @return {Grouping} the grouping instance object
14
+     */
15
+    constructor({ ...grouping }) {
16
+        super()
17
+
18
+        this.type = this.constructor.name.toLowerCase()
19
+
20
+        /**  Fields */
21
+        this.groupingId = groupingId
22
+        this.groupingName = groupingName
23
+        this.groupingType = groupingType
24
+
25
+        return this
26
+    }
27
+    /**
28
+     * validate this record
29
+     * @return {boolean} is it valid or not?
30
+     */
31
+    isValid() {
32
+        const validate = groupingSchema.validate(this)
33
+
34
+        /**
35
+         * Log out some useful error messages
36
+         * TODO: Send validate.error to logging error handler
37
+         */
38
+        if(validate.error) {
39
+            console.error(
40
+                `error: ${validate.error} - ${this.type} validation`
41
+            )
42
+        }
43
+
44
+        /** validate(this) always returns something so force it to a bool */
45
+        return !validate.error ? true : false
46
+    }
47
+}
48
+
49
+export { Grouping }

+ 31
- 0
frontend/src/entities/grouping/grouping.schema.js Прегледај датотеку

@@ -0,0 +1,31 @@
1
+/** @module entities/groupingSchema */
2
+import Joi from 'joi'
3
+
4
+/**
5
+ * grouping schema object
6
+ * uses the module system to use common fields
7
+ * but sets fields with our validation types
8
+ * @constructor
9
+*/
10
+const groupingSchema = {
11
+    type: 'object',
12
+    properties: Joi.object().keys({
13
+        /** _baseRecord fields */
14
+        createdAt: Joi.string(),
15
+        _id: Joi.string(),
16
+        lastUpdatedAt: Joi.string(),
17
+        type: Joi.string(),
18
+
19
+        /** our fields */
20
+        groupingId: Joi.number().required(),
21
+        groupingName: Joi.string().required(),
22
+        groupingType: Joi.string().required()
23
+    }),
24
+    /** fields required before saving */
25
+    required: [ 'groupingId', 'groupingName', 'groupingType' ],
26
+    validate(instance) {
27
+        return this.properties.validate(instance)
28
+    }
29
+}
30
+
31
+export { groupingSchema }

+ 0
- 0
frontend/src/entities/grouping/grouping.spec.js Прегледај датотеку


+ 2
- 0
frontend/src/entities/grouping/index.js Прегледај датотеку

@@ -0,0 +1,2 @@
1
+export * from './grouping'
2
+export * from './grouping.schema'

+ 3
- 5
frontend/src/entities/index.js Прегледај датотеку

@@ -1,10 +1,8 @@
1 1
 export * from './_modules'
2 2
 
3
-/** Single instance of your schema handler */
4
-import Joi from 'joi'
5
-
6
-export { Joi }
7
-
8 3
 /** Your different entities */
4
+export * from './response'
9 5
 export * from './profile'
6
+export * from './survey'
7
+export * from './grouping'
10 8
 

+ 3
- 4
frontend/src/entities/profile/profile.js Прегледај датотеку

@@ -1,6 +1,5 @@
1 1
 /** @module entities/profile */
2
-
3
-import { _baseRecord, allModules, Joi } from '..'
2
+import { _baseRecord, allModules } from '..'
4 3
 import { profileSchema } from './profile.schema'
5 4
 
6 5
 /** Class representing a profile */
@@ -12,7 +11,7 @@ class Profile extends _baseRecord {
12 11
      * @param {object} profile - spread destructured args
13 12
      * @return {Profile} the profile instance object
14 13
      */
15
-    constructor({ email, ...profile }) {
14
+    constructor({ email, ...profileData }) {
16 15
         super()
17 16
 
18 17
         this.type = this.constructor.name.toLowerCase()
@@ -21,7 +20,7 @@ class Profile extends _baseRecord {
21 20
         this.email = email // ! required
22 21
 
23 22
         /** Pass destructured data to the module system */
24
-        Object.assign(this, allModules.location(profile))
23
+        Object.assign(this, profileData)
25 24
 
26 25
         return this
27 26
     }

+ 12
- 3
frontend/src/entities/profile/profile.schema.js Прегледај датотеку

@@ -1,5 +1,6 @@
1 1
 /** @module entities/profileSchema */
2
-import { allModules, Joi } from '..'
2
+import Joi from 'joi'
3
+import { allModules, responseSchema } from '..'
3 4
 
4 5
 /**
5 6
  * profile schema object
@@ -20,7 +21,15 @@ const profileSchema = {
20 21
         email: Joi.string().email({
21 22
             minDomainSegments: 2,
22 23
             tlds: { allow: ['com', 'net'] }
23
-        }).required(),
24
+        }),
25
+
26
+        /** fields that should match backend service*/
27
+        user_id: Joi.number(),
28
+        profile_id: Joi.number(),
29
+        responses: Joi.array().items(
30
+            responseSchema
31
+        ), // response entity schema goes here
32
+        user_type: Joi.string(),
24 33
 
25 34
         /** tricky module system fields */
26 35
         ...allModules.location({
@@ -32,7 +41,7 @@ const profileSchema = {
32 41
         })
33 42
     }),
34 43
     /** fields required before saving */
35
-    required: [ 'email' ],
44
+    required: [ ],
36 45
     validate(instance) {
37 46
         return this.properties.validate(instance)
38 47
     }

+ 1
- 0
frontend/src/entities/response/index.js Прегледај датотеку

@@ -0,0 +1 @@
1
+export * from './response.schema'

+ 14
- 0
frontend/src/entities/response/response.schema.js Прегледај датотеку

@@ -0,0 +1,14 @@
1
+/** @module entities/responseSchema */
2
+import Joi from 'joi'
3
+
4
+/**
5
+ * response schema object
6
+*/
7
+const responseSchema = Joi.object({
8
+    profile_id: Joi.number(),
9
+    response_id: Joi.number(),
10
+    response_key_id: Joi.number(),
11
+    val:  Joi.string(),
12
+})
13
+
14
+export { responseSchema }

+ 2
- 0
frontend/src/entities/survey/index.js Прегледај датотеку

@@ -0,0 +1,2 @@
1
+export * from './survey'
2
+export * from './survey.schema'

+ 0
- 0
frontend/src/entities/survey/survey.js Прегледај датотеку


+ 33
- 0
frontend/src/entities/survey/survey.schema.js Прегледај датотеку

@@ -0,0 +1,33 @@
1
+/** @module entities/surveySchema */
2
+import Joi from 'joi'
3
+import { allModules } from '..'
4
+
5
+/**
6
+ * membership schema object
7
+ * uses the module system to use common fields
8
+ * but sets fields with our validation types
9
+ * @constructor
10
+*/
11
+const surveySchema = {
12
+    type: 'object',
13
+    properties: Joi.object().keys({
14
+        /** _baseRecord fields */
15
+        createdAt: Joi.string(),
16
+        _id: Joi.string(),
17
+        lastUpdatedAt: Joi.string(),
18
+        type: Joi.string(),
19
+
20
+        /** our fields */
21
+        email: Joi.string().email({
22
+            minDomainSegments: 2,
23
+            tlds: { allow: ['com', 'net'] }
24
+        }).required(),
25
+    }),
26
+    /** fields required before saving */
27
+    required: [ 'email' ],
28
+    validate(instance) {
29
+        return this.properties.validate(instance)
30
+    }
31
+}
32
+
33
+export { surveySchema }

+ 0
- 0
frontend/src/entities/survey/survey.spec.js Прегледај датотеку


+ 0
- 0
frontend/src/services/grouping.service.js Прегледај датотеку


+ 1
- 0
frontend/src/services/index.js Прегледај датотеку

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

+ 22
- 0
frontend/src/services/profile.service.js Прегледај датотеку

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

+ 8
- 5
frontend/src/utils/db.js Прегледај датотеку

@@ -1,6 +1,8 @@
1 1
 const domain = 'localhost'
2 2
 const port = 3001
3
-const remote = `http://${domain}:${port}/api`
3
+const httpProtocol = `http`
4
+const prefix = 'api'
5
+const remote = `${httpProtocol}://${domain}:${port}/${prefix}`
4 6
 
5 7
 const headerTemplate = {
6 8
     method: null,
@@ -18,12 +20,13 @@ const headerTemplate = {
18 20
 
19 21
 class Connector {
20 22
     constructor() {
21
-        this.db = null
23
+        this.apiPrefix = prefix
22 24
     }
23 25
     async get(endpoint) {
24 26
         const header = headerTemplate
25 27
         header.method = 'GET'
26 28
         try {
29
+            console.log(`${remote}${endpoint}`)
27 30
             let res = await fetch(`${remote}${endpoint}`, header)
28 31
             if (!res.ok) {
29 32
                 throw Error(res.statusText)
@@ -31,7 +34,7 @@ class Connector {
31 34
             const jsonRes = await res.json()
32 35
             return jsonRes.data
33 36
         } catch (error) {
34
-            console.log(error)
37
+            console.error(error)
35 38
         }
36 39
     }
37 40
     async put(endpoint, entry) {
@@ -50,5 +53,5 @@ class Connector {
50 53
     /** !: DEV ONLY */
51 54
     // async removeAll() { }
52 55
 }
53
-
54
-export { Connector }
56
+const db = new Connector()
57
+export { Connector, db }

Loading…
Откажи
Сачувај