Skip to content

Commit 0ecf99e

Browse files
authored
Merge pull request #5 from eug-vs/profile
Create profile endpoint
2 parents 8fc9f8d + 24ff209 commit 0ecf99e

File tree

6 files changed

+69
-34
lines changed

6 files changed

+69
-34
lines changed

hooks/expandAuthor.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+

services/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { Application } from '@feathersjs/express';
22
import Users from './users/users.service';
33
import Polls from './polls/polls.service';
4+
import Profiles from './profiles/profiles.service';
45

56
export default (app: Application): void => {
67
app.configure(Users);
78
app.configure(Polls);
9+
app.configure(Profiles);
810
};
911

services/polls/polls.hooks.ts

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,12 @@
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';
345

356
export default {
367
after: {
378
get: [expandAuthorHook],
389
find: [expandAuthorManyHook]
3910
}
40-
}
11+
};
12+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {
2+
expandAuthorManyHook,
3+
} from '../../hooks/expandAuthor';
4+
5+
export default {
6+
after: {
7+
get: [expandAuthorManyHook],
8+
}
9+
};
10+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+

0 commit comments

Comments
 (0)