Przeglądaj źródła

:sparkles: created method to join a group from frontend

tags/0.0.1
TOJ 4 lat temu
rodzic
commit
818561c738

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

11
 const validators = {
11
 const validators = {
12
     payload: Joi.object({
12
     payload: Joi.object({
13
         profile_id: Joi.number().required(),
13
         profile_id: Joi.number().required(),
14
-        target_id: Joi.number().allow(null),
14
+        target_id: Joi.number().required(),
15
         grouping_id: Joi.number().allow(null),
15
         grouping_id: Joi.number().allow(null),
16
         grouping_name: Joi.string().allow(null),
16
         grouping_name: Joi.string().allow(null),
17
         grouping_type: Joi.string().allow(null),
17
         grouping_type: Joi.string().allow(null),
36
         ...pluginConfig.docs,
36
         ...pluginConfig.docs,
37
         tags: ['api'],
37
         tags: ['api'],
38
         auth: false,
38
         auth: false,
39
+        cors: true,
39
 
40
 
40
         /**
41
         /**
41
          * Join a grouping by creating a membership record
42
          * Join a grouping by creating a membership record

+ 9
- 1
frontend/src/App.vue Wyświetl plik

16
 import {
16
 import {
17
     fetchProfilesByUserId,
17
     fetchProfilesByUserId,
18
     fetchMembershipsByProfileId,
18
     fetchMembershipsByProfileId,
19
+    postMembershipByProfileId
19
 } from '@/services'
20
 } from '@/services'
20
 
21
 
21
 import helloWorld from '@/components/HelloWorld.vue'
22
 import helloWorld from '@/components/HelloWorld.vue'
61
 
62
 
62
         const t = async () => {
63
         const t = async () => {
63
             const tempProfiles = await fetchProfilesByUserId(1)
64
             const tempProfiles = await fetchProfilesByUserId(1)
64
-            const groupings = []
65
+            let groupings = []
66
+            for(let p of tempProfiles) {
67
+                const memberships = await fetchMembershipsByProfileId(p.profile_id)
68
+                groupings.push(memberships)
69
+            }
70
+            console.log(groupings)
71
+            groupings = []
72
+            await postMembershipByProfileId({ profileId: 1, targetId: 20 })
65
             for(let p of tempProfiles) {
73
             for(let p of tempProfiles) {
66
                 const memberships = await fetchMembershipsByProfileId(p.profile_id)
74
                 const memberships = await fetchMembershipsByProfileId(p.profile_id)
67
                 groupings.push(memberships)
75
                 groupings.push(memberships)

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

22
         grouping_type: Joi.string()
22
         grouping_type: Joi.string()
23
     }),
23
     }),
24
     /** fields required before saving */
24
     /** fields required before saving */
25
-    required: [ 'grouping_id', 'grouping_name', 'grouping_type' ],
25
+    required: [ 'grouping_id' ],
26
     validate(instance) {
26
     validate(instance) {
27
         return this.properties.validate(instance)
27
         return this.properties.validate(instance)
28
     }
28
     }

+ 12
- 1
frontend/src/services/grouping.service.js Wyświetl plik

20
     return validGroupingInstances
20
     return validGroupingInstances
21
 }
21
 }
22
 
22
 
23
-export { fetchMembershipsByProfileId }
23
+const postMembershipByProfileId = async ({ profileId, targetId, groupingType = 'match' }) => {
24
+    const membership = {
25
+        profile_id: profileId,
26
+        target_id: targetId,
27
+        grouping_type: groupingType,
28
+        grouping_name: `delete_${profileId}_${targetId}`
29
+    }
30
+    const createdMembershipRecord = await db.post(`/membership/join`, membership)
31
+    console.log(createdMembershipRecord)
32
+    return createdMembershipRecord
33
+}
34
+export { fetchMembershipsByProfileId, postMembershipByProfileId }

+ 16
- 1
frontend/src/utils/db.js Wyświetl plik

23
         this.apiPrefix = prefix
23
         this.apiPrefix = prefix
24
     }
24
     }
25
     async get(endpoint) {
25
     async get(endpoint) {
26
-        const header = headerTemplate
26
+        const header = { headerTemplate }
27
         header.method = 'GET'
27
         header.method = 'GET'
28
         try {
28
         try {
29
             console.log(`${remote}${endpoint}`)
29
             console.log(`${remote}${endpoint}`)
37
             console.error(error)
37
             console.error(error)
38
         }
38
         }
39
     }
39
     }
40
+    async post(endpoint, payload = {}) {
41
+        const header = { ...headerTemplate }
42
+        header.method = 'POST'
43
+        header.body = JSON.stringify(payload)
44
+        try {
45
+            let res = await fetch(`${remote}${endpoint}`, header)
46
+            if (!res.ok) {
47
+                throw Error(res.statusText)
48
+            }
49
+            const jsonRes = await res.json()
50
+            return jsonRes.data
51
+        } catch (error) {
52
+            console.error(error)
53
+        }
54
+    }
40
     async put(endpoint, entry) {
55
     async put(endpoint, entry) {
41
         /** from ajv schema validation */
56
         /** from ajv schema validation */
42
         if (entry.isValid()) {
57
         if (entry.isValid()) {

Ładowanie…
Anuluj
Zapisz