File tree Expand file tree Collapse file tree 6 files changed +69
-34
lines changed Expand file tree Collapse file tree 6 files changed +69
-34
lines changed Original file line number Diff line number Diff line change
1
+ import { HookContext } from '@feathersjs/feathers' ;
2
+ import bluebird from 'bluebird' ;
3
+ import _ from 'lodash' ;
4
+
5
+ import { Poll , PollSchema } from '../models/polls/poll.schema' ;
6
+ import { User } from '../models/users/user.schema' ;
7
+ import UserModel from '../models/users/user.model' ;
8
+
9
+ const expandAuthor = async ( poll : PollSchema ) : Promise < Poll | null > => {
10
+ return UserModel . findById ( poll . authorId )
11
+ . lean < User > ( )
12
+ . exec ( )
13
+ . then ( ( author : User | null ) : Poll | null => {
14
+ return author && _ . merge ( _ . omit ( poll , 'authorId' ) , { author } ) ;
15
+ } )
16
+ . catch ( err => {
17
+ console . error ( err ) ;
18
+ return err ;
19
+ } ) ;
20
+ } ;
21
+
22
+ export const expandAuthorHook = async ( context : HookContext ) : Promise < HookContext > => {
23
+ context . result = await expandAuthor ( context . result ) ;
24
+ return context ;
25
+ } ;
26
+
27
+ export const expandAuthorManyHook = async ( context : HookContext ) : Promise < HookContext > => {
28
+ const polls = await bluebird . map ( context . result , ( poll : any ) => expandAuthor ( poll ) ) ;
29
+ context . result = _ . compact ( polls ) ;
30
+ return context ;
31
+ } ;
32
+
Original file line number Diff line number Diff line change 1
1
import { Application } from '@feathersjs/express' ;
2
2
import Users from './users/users.service' ;
3
3
import Polls from './polls/polls.service' ;
4
+ import Profiles from './profiles/profiles.service' ;
4
5
5
6
export default ( app : Application ) : void => {
6
7
app . configure ( Users ) ;
7
8
app . configure ( Polls ) ;
9
+ app . configure ( Profiles ) ;
8
10
} ;
9
11
Original file line number Diff line number Diff line change 1
- import { HookContext } from '@feathersjs/feathers' ;
2
- import bluebird from 'bluebird' ;
3
- import _ from 'lodash' ;
4
-
5
- import { Poll , PollSchema } from '../../models/polls/poll.schema' ;
6
- import { User } from '../../models/users/user.schema' ;
7
- import UserModel from '../../models/users/user.model' ;
8
-
9
-
10
- const expandAuthor = async ( poll : PollSchema ) : Promise < Poll | null > => {
11
- return UserModel . findById ( poll . authorId )
12
- . lean < User > ( )
13
- . exec ( )
14
- . then ( ( author : User | null ) : Poll | null => {
15
- return author && _ . merge ( _ . omit ( poll , 'authorId' ) , { author } ) ;
16
- } )
17
- . catch ( err => {
18
- console . error ( err ) ;
19
- return err ;
20
- } ) ;
21
- } ;
22
-
23
- const expandAuthorHook = async ( context : HookContext ) : Promise < HookContext > => {
24
- context . result = await expandAuthor ( context . result ) ;
25
- return context ;
26
- } ;
27
-
28
- const expandAuthorManyHook = async ( context : HookContext ) : Promise < HookContext > => {
29
- const polls = await bluebird . map ( context . result , ( poll : any ) => expandAuthor ( poll ) ) ;
30
- context . result = _ . compact ( polls ) ;
31
- return context ;
32
- } ;
33
-
1
+ import {
2
+ expandAuthorHook ,
3
+ expandAuthorManyHook
4
+ } from '../../hooks/expandAuthor' ;
34
5
35
6
export default {
36
7
after : {
37
8
get : [ expandAuthorHook ] ,
38
9
find : [ expandAuthorManyHook ]
39
10
}
40
- }
11
+ } ;
12
+
Original file line number Diff line number Diff line change
1
+ import { Poll , PollSchema } from "../../models/polls/poll.schema" ;
2
+ import PollModel from '../../models/polls/poll.model' ;
3
+
4
+ export default class Profiles {
5
+ async get ( id : string , params : any ) : Promise < PollSchema [ ] > {
6
+ return PollModel . find ( { authorId : id } ) . lean < Poll > ( ) ;
7
+ }
8
+ } ;
9
+
Original file line number Diff line number Diff line change
1
+ import {
2
+ expandAuthorManyHook ,
3
+ } from '../../hooks/expandAuthor' ;
4
+
5
+ export default {
6
+ after : {
7
+ get : [ expandAuthorManyHook ] ,
8
+ }
9
+ } ;
10
+
Original file line number Diff line number Diff line change
1
+ import { Application } from "@feathersjs/express" ;
2
+ import Profiles from './profiles.class' ;
3
+
4
+ import hooks from './profiles.hooks' ;
5
+
6
+ export default ( app : Application ) : void => {
7
+ app . use ( '/profiles' , new Profiles ( ) ) ;
8
+ app . service ( 'profiles' ) . hooks ( hooks ) ;
9
+ } ;
10
+
You can’t perform that action at this time.
0 commit comments