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

:recycle: clean up response model

neo
toj 2 лет назад
Родитель
Сommit
8b110a6a91
4 измененных файлов: 16 добавлений и 30 удалений
  1. 0
    13
      backend/lib/models/user.js
  2. 3
    3
      backend/lib/routes/user/get-user.js
  3. 0
    11
      backend/lib/schemas/api.js
  4. 13
    3
      backend/lib/schemas/users.js

+ 0
- 13
backend/lib/models/user.js Просмотреть файл

1
 const Schwifty = require('@hapipal/schwifty')
1
 const Schwifty = require('@hapipal/schwifty')
2
 
2
 
3
-const ProfileModel = require('./profile')
4
 const userSchema = require('../schemas/users')
3
 const userSchema = require('../schemas/users')
5
 
4
 
6
 /**
5
 /**
10
     static get tableName() {
9
     static get tableName() {
11
         return 'users'
10
         return 'users'
12
     }
11
     }
13
-    static get relationMappings() {
14
-        return {
15
-            profiles: {
16
-                relation: Schwifty.Model.HasManyRelation,
17
-                modelClass: ProfileModel,
18
-                join: {
19
-                    from: 'user.profile_id',
20
-                    to: 'profiles.profile_id',
21
-                },
22
-            },
23
-        }
24
-    }
25
     static get joiSchema() {
12
     static get joiSchema() {
26
         return userSchema.single
13
         return userSchema.single
27
     }
14
     }

+ 3
- 3
backend/lib/routes/user/get-user.js Просмотреть файл

1
-const { responseWrapper } = require('../../schemas/api.js')
2
-const { validator } = require('../../schemas/users.js')
1
+const Joi = require('joi')
2
+const { validator, response } = require('../../schemas/users.js')
3
 const userHandlers = require('../handlers/user')
3
 const userHandlers = require('../handlers/user')
4
 
4
 
5
 const docs = {
5
 const docs = {
22
             failAction: 'log',
22
             failAction: 'log',
23
         },
23
         },
24
         response: {
24
         response: {
25
-            schema: responseWrapper.label('password_res'),
25
+            schema: response,
26
             failAction: 'log',
26
             failAction: 'log',
27
         },
27
         },
28
     },
28
     },

+ 0
- 11
backend/lib/schemas/api.js Просмотреть файл

1
-const Joi = require('joi')
2
-
3
-const responseWrapper = Joi.object({
4
-    ok: Joi.bool(),
5
-    handler: Joi.string(),
6
-    data: Joi.object(),
7
-})
8
-
9
-module.exports = {
10
-    responseWrapper,
11
-}

+ 13
- 3
backend/lib/schemas/users.js Просмотреть файл

5
  * A user record is tied to a single person.
5
  * A user record is tied to a single person.
6
  */
6
  */
7
 
7
 
8
+// common fields
9
+const user_email = Joi.string().required()
10
+
8
 // validator is used to validate route input/output
11
 // validator is used to validate route input/output
9
 const validator = Joi.object({
12
 const validator = Joi.object({
10
-    user_id: Joi.number(),
11
-    user_email: Joi.string().required(),
13
+    user_email,
12
 }).label('user__single_validator')
14
 }).label('user__single_validator')
13
 
15
 
14
 // single is used to define database models
16
 // single is used to define database models
15
 const single = Joi.object({
17
 const single = Joi.object({
16
     user_id: Joi.number(),
18
     user_id: Joi.number(),
17
-    user_email: Joi.string().required(),
19
+    user_email,
18
     is_poster: Joi.number().required(),
20
     is_poster: Joi.number().required(),
19
     is_verified: Joi.number().required(),
21
     is_verified: Joi.number().required(),
20
 }).label('user__single')
22
 }).label('user__single')
21
 
23
 
24
+// response is used to wrap and validate api responses
25
+const response = Joi.object({
26
+    ok: Joi.bool(),
27
+    handler: Joi.string(),
28
+    data: single,
29
+}).label('user__single_response')
30
+
22
 module.exports = {
31
 module.exports = {
23
     single,
32
     single,
24
     validator,
33
     validator,
34
+    response,
25
 }
35
 }

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