From c9fc9c12b452d9aa8630cdfdaf494381a5dfdd8b Mon Sep 17 00:00:00 2001 From: ilyayudovin Date: Fri, 12 Jun 2020 16:14:56 +0300 Subject: feat: add profile.service --- services/index.ts | 2 ++ services/profile/profile.service.ts | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 services/profile/profile.service.ts diff --git a/services/index.ts b/services/index.ts index 441f9ad..9cd77c3 100644 --- a/services/index.ts +++ b/services/index.ts @@ -1,9 +1,11 @@ import { Application } from '@feathersjs/express'; import Users from './users/users.service'; import Polls from './polls/polls.service'; +import Profile from './profile/profile.service'; export default (app: Application): void => { app.configure(Users); app.configure(Polls); + app.configure(Profile); }; diff --git a/services/profile/profile.service.ts b/services/profile/profile.service.ts new file mode 100644 index 0000000..4a12def --- /dev/null +++ b/services/profile/profile.service.ts @@ -0,0 +1,16 @@ +import {User} from '../../models/users/user.schema'; +import {Poll} from "../../models/polls/poll.schema"; +import {Application} from "@feathersjs/express"; + +export class ProfileService { + user: User = Object; + saved_polls: Poll[] = []; + + async find () { + return [this.user, this.saved_polls]; + } +} + +export default (app: Application): void => { + app.use('/profile', new ProfileService()); +}; \ No newline at end of file -- cgit v1.2.3 From fb889145e2e3414925553152e8aff6a096bc0cae Mon Sep 17 00:00:00 2001 From: eug-vs Date: Fri, 12 Jun 2020 16:46:56 +0300 Subject: refactor: move hooks to global folder --- hooks/expandAuthor.ts | 32 ++++++++++++++++++++++++++++++++ services/polls/polls.hooks.ts | 40 ++++++---------------------------------- 2 files changed, 38 insertions(+), 34 deletions(-) create mode 100644 hooks/expandAuthor.ts diff --git a/hooks/expandAuthor.ts b/hooks/expandAuthor.ts new file mode 100644 index 0000000..1f33ff0 --- /dev/null +++ b/hooks/expandAuthor.ts @@ -0,0 +1,32 @@ +import { HookContext } from '@feathersjs/feathers'; +import bluebird from 'bluebird'; +import _ from 'lodash'; + +import { Poll, PollSchema } from '../models/polls/poll.schema'; +import { User } from '../models/users/user.schema'; +import UserModel from '../models/users/user.model'; + +const expandAuthor = async (poll: PollSchema): Promise => { + return UserModel.findById(poll.authorId) + .lean() + .exec() + .then((author: User | null): Poll | null => { + return author && _.merge(_.omit(poll, 'authorId'), { author }); + }) + .catch(err => { + console.error(err); + return err; + }); +}; + +export const expandAuthorHook = async (context: HookContext): Promise => { + context.result = await expandAuthor(context.result); + return context; +}; + +export const expandAuthorManyHook = async (context: HookContext): Promise => { + const polls = await bluebird.map(context.result, (poll: any) => expandAuthor(poll)); + context.result = _.compact(polls); + return context; +}; + diff --git a/services/polls/polls.hooks.ts b/services/polls/polls.hooks.ts index fc5fa45..0637320 100644 --- a/services/polls/polls.hooks.ts +++ b/services/polls/polls.hooks.ts @@ -1,40 +1,12 @@ -import { HookContext } from '@feathersjs/feathers'; -import bluebird from 'bluebird'; -import _ from 'lodash'; - -import { Poll, PollSchema } from '../../models/polls/poll.schema'; -import { User } from '../../models/users/user.schema'; -import UserModel from '../../models/users/user.model'; - - -const expandAuthor = async (poll: PollSchema): Promise => { - return UserModel.findById(poll.authorId) - .lean() - .exec() - .then((author: User | null): Poll | null => { - return author && _.merge(_.omit(poll, 'authorId'), { author }); - }) - .catch(err => { - console.error(err); - return err; - }); -}; - -const expandAuthorHook = async (context: HookContext): Promise => { - context.result = await expandAuthor(context.result); - return context; -}; - -const expandAuthorManyHook = async (context: HookContext): Promise => { - const polls = await bluebird.map(context.result, (poll: any) => expandAuthor(poll)); - context.result = _.compact(polls); - return context; -}; - +import { + expandAuthorHook, + expandAuthorManyHook +} from '../../hooks/expandAuthor'; export default { after: { get: [expandAuthorHook], find: [expandAuthorManyHook] } -} +}; + -- cgit v1.2.3 From f2573a28748a3c7983b730be2c775e024d0cdaa0 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Fri, 12 Jun 2020 17:00:48 +0300 Subject: refactor: move Profiles class to separate file --- services/profile/profile.class.ts | 9 +++++++++ services/profile/profile.service.ts | 19 +++++-------------- 2 files changed, 14 insertions(+), 14 deletions(-) create mode 100644 services/profile/profile.class.ts diff --git a/services/profile/profile.class.ts b/services/profile/profile.class.ts new file mode 100644 index 0000000..d4e7b02 --- /dev/null +++ b/services/profile/profile.class.ts @@ -0,0 +1,9 @@ +import { Poll, PollSchema } from "../../models/polls/poll.schema"; +import PollModel from '../../models/polls/poll.model'; + +export default class Profiles { + async get(id: string, params: any): Promise { + return PollModel.find({ authorId: id }); + } +}; + diff --git a/services/profile/profile.service.ts b/services/profile/profile.service.ts index 4a12def..752983c 100644 --- a/services/profile/profile.service.ts +++ b/services/profile/profile.service.ts @@ -1,16 +1,7 @@ -import {User} from '../../models/users/user.schema'; -import {Poll} from "../../models/polls/poll.schema"; -import {Application} from "@feathersjs/express"; - -export class ProfileService { - user: User = Object; - saved_polls: Poll[] = []; - - async find () { - return [this.user, this.saved_polls]; - } -} +import { Application } from "@feathersjs/express"; +import Profiles from './profile.class'; export default (app: Application): void => { - app.use('/profile', new ProfileService()); -}; \ No newline at end of file + app.use('/profile', new Profiles()); +}; + -- cgit v1.2.3 From 24ff209193448ee047f428705879cef15e0b737a Mon Sep 17 00:00:00 2001 From: eug-vs Date: Fri, 12 Jun 2020 19:30:00 +0300 Subject: feat: finish profile services --- services/index.ts | 4 ++-- services/profile/profile.class.ts | 9 --------- services/profile/profile.service.ts | 7 ------- services/profiles/profiles.class.ts | 9 +++++++++ services/profiles/profiles.hooks.ts | 10 ++++++++++ services/profiles/profiles.service.ts | 10 ++++++++++ 6 files changed, 31 insertions(+), 18 deletions(-) delete mode 100644 services/profile/profile.class.ts delete mode 100644 services/profile/profile.service.ts create mode 100644 services/profiles/profiles.class.ts create mode 100644 services/profiles/profiles.hooks.ts create mode 100644 services/profiles/profiles.service.ts diff --git a/services/index.ts b/services/index.ts index 9cd77c3..d946e9d 100644 --- a/services/index.ts +++ b/services/index.ts @@ -1,11 +1,11 @@ import { Application } from '@feathersjs/express'; import Users from './users/users.service'; import Polls from './polls/polls.service'; -import Profile from './profile/profile.service'; +import Profiles from './profiles/profiles.service'; export default (app: Application): void => { app.configure(Users); app.configure(Polls); - app.configure(Profile); + app.configure(Profiles); }; diff --git a/services/profile/profile.class.ts b/services/profile/profile.class.ts deleted file mode 100644 index d4e7b02..0000000 --- a/services/profile/profile.class.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Poll, PollSchema } from "../../models/polls/poll.schema"; -import PollModel from '../../models/polls/poll.model'; - -export default class Profiles { - async get(id: string, params: any): Promise { - return PollModel.find({ authorId: id }); - } -}; - diff --git a/services/profile/profile.service.ts b/services/profile/profile.service.ts deleted file mode 100644 index 752983c..0000000 --- a/services/profile/profile.service.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Application } from "@feathersjs/express"; -import Profiles from './profile.class'; - -export default (app: Application): void => { - app.use('/profile', new Profiles()); -}; - diff --git a/services/profiles/profiles.class.ts b/services/profiles/profiles.class.ts new file mode 100644 index 0000000..8954f74 --- /dev/null +++ b/services/profiles/profiles.class.ts @@ -0,0 +1,9 @@ +import { Poll, PollSchema } from "../../models/polls/poll.schema"; +import PollModel from '../../models/polls/poll.model'; + +export default class Profiles { + async get(id: string, params: any): Promise { + return PollModel.find({ authorId: id }).lean(); + } +}; + diff --git a/services/profiles/profiles.hooks.ts b/services/profiles/profiles.hooks.ts new file mode 100644 index 0000000..b022aff --- /dev/null +++ b/services/profiles/profiles.hooks.ts @@ -0,0 +1,10 @@ +import { + expandAuthorManyHook, +} from '../../hooks/expandAuthor'; + +export default { + after: { + get: [expandAuthorManyHook], + } +}; + diff --git a/services/profiles/profiles.service.ts b/services/profiles/profiles.service.ts new file mode 100644 index 0000000..3422352 --- /dev/null +++ b/services/profiles/profiles.service.ts @@ -0,0 +1,10 @@ +import { Application } from "@feathersjs/express"; +import Profiles from './profiles.class'; + +import hooks from './profiles.hooks'; + +export default (app: Application): void => { + app.use('/profiles', new Profiles()); + app.service('profiles').hooks(hooks); +}; + -- cgit v1.2.3