Просмотр исходного кода

: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 лет назад
Родитель
Сommit
bdfe31e224

+ 8
- 1
frontend/src/App.vue Просмотреть файл

13
 import { profileForm } from '@/utils/forms'
13
 import { profileForm } from '@/utils/forms'
14
 import { Connector } from '@/utils/db'
14
 import { Connector } from '@/utils/db'
15
 
15
 
16
+import { fetchProfilesByUserId } from '@/services'
17
+
16
 import helloWorld from '@/components/HelloWorld.vue'
18
 import helloWorld from '@/components/HelloWorld.vue'
17
 import card from '@/components/card.vue'
19
 import card from '@/components/card.vue'
18
 import form from '@/components/form.vue'
20
 import form from '@/components/form.vue'
54
         }
56
         }
55
         onMounted(compileForm)
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
             cards,
65
             cards,
59
             remove,
66
             remove,
60
             completedForm
67
             completedForm

+ 2
- 2
frontend/src/components/HelloWorld.vue Просмотреть файл

36
     state: 'lollipop',
36
     state: 'lollipop',
37
     zip: 90002
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
 </script>
41
 </script>
42
 
42
 
43
 <style lang="postcss">
43
 <style lang="postcss">

+ 49
- 0
frontend/src/entities/grouping/grouping.js Просмотреть файл

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 Просмотреть файл

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 Просмотреть файл

1
+export * from './grouping'
2
+export * from './grouping.schema'

+ 3
- 5
frontend/src/entities/index.js Просмотреть файл

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

+ 3
- 4
frontend/src/entities/profile/profile.js Просмотреть файл

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

+ 12
- 3
frontend/src/entities/profile/profile.schema.js Просмотреть файл

1
 /** @module entities/profileSchema */
1
 /** @module entities/profileSchema */
2
-import { allModules, Joi } from '..'
2
+import Joi from 'joi'
3
+import { allModules, responseSchema } from '..'
3
 
4
 
4
 /**
5
 /**
5
  * profile schema object
6
  * profile schema object
20
         email: Joi.string().email({
21
         email: Joi.string().email({
21
             minDomainSegments: 2,
22
             minDomainSegments: 2,
22
             tlds: { allow: ['com', 'net'] }
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
         /** tricky module system fields */
34
         /** tricky module system fields */
26
         ...allModules.location({
35
         ...allModules.location({
32
         })
41
         })
33
     }),
42
     }),
34
     /** fields required before saving */
43
     /** fields required before saving */
35
-    required: [ 'email' ],
44
+    required: [ ],
36
     validate(instance) {
45
     validate(instance) {
37
         return this.properties.validate(instance)
46
         return this.properties.validate(instance)
38
     }
47
     }

+ 1
- 0
frontend/src/entities/response/index.js Просмотреть файл

1
+export * from './response.schema'

+ 14
- 0
frontend/src/entities/response/response.schema.js Просмотреть файл

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 Просмотреть файл

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 Просмотреть файл

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 Просмотреть файл

1
+export * from './profile.service'

+ 22
- 0
frontend/src/services/profile.service.js Просмотреть файл

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

Загрузка…
Отмена
Сохранить