@@ -17,13 +17,12 @@ const DEFAULT_PAGINATION_CONTENT = {
1717 data : [ ] ,
1818} ;
1919
20-
2120const handleUsersPaginationResponse = ( response ) => {
2221 if ( ! response . docs || response . docs . length <= 0 ) {
2322 return DEFAULT_PAGINATION_CONTENT ;
2423 }
2524 const postsList = {
26- data : response . docs . map ( doc => mapper . toDomainModel ( doc , PostDomainModel ) ) ,
25+ data : response . docs . map ( ( doc ) => mapper . toDomainModel ( doc , PostDomainModel ) ) ,
2726 pagination : {
2827 total : response . total ,
2928 limit : response . limit ,
@@ -34,14 +33,13 @@ const handleUsersPaginationResponse = (response) => {
3433 return postsList ;
3534} ;
3635
37- const getPaginationOptions = options => ( {
36+ const getPaginationOptions = ( options ) => ( {
3837 lean : true ,
3938 page : options . page || 1 ,
4039 limit : options . limit || 25 ,
4140 sort : { created : - 1 } ,
4241} ) ;
4342
44-
4543const getQueryObject = ( options ) => {
4644 const queries = {
4745 userId : options . userId ,
@@ -55,52 +53,37 @@ const getQueryObject = (options) => {
5553 return queries ;
5654} ;
5755
58-
5956const postStore = {
6057 async listUserPosts ( options ) {
61- try {
62- const { Post : postSchema } = this . getSchemas ( ) ;
63- const docs = await postSchema . paginate ( getQueryObject ( options ) , getPaginationOptions ( options ) ) ;
64- return handleUsersPaginationResponse ( docs ) ;
65- } catch ( error ) {
66- throw error ;
67- }
58+ const { Post : postSchema } = this . getSchemas ( ) ;
59+ const docs = await postSchema . paginate ( getQueryObject ( options ) , getPaginationOptions ( options ) ) ;
60+ return handleUsersPaginationResponse ( docs ) ;
6861 } ,
6962 async createUserPost ( options ) {
70- try {
71- const { Post : postSchema } = this . getSchemas ( ) ;
72- const newPost = new postSchema ( {
73- userId : options . userId ,
74- imageUrl : options . imageUrl ,
75- description : options . description ,
76- publisher : options . publisher ,
77- } ) ;
78- const doc = await newPost . save ( ) ;
79- return mapper . toDomainModel ( doc , PostDomainModel ) ;
80- } catch ( error ) {
81- throw error ;
82- }
63+ const { Post : postSchema } = this . getSchemas ( ) ;
64+ const newPost = new postSchema ( {
65+ userId : options . userId ,
66+ imageUrl : options . imageUrl ,
67+ description : options . description ,
68+ publisher : options . publisher ,
69+ } ) ;
70+ const doc = await newPost . save ( ) ;
71+ return mapper . toDomainModel ( doc , PostDomainModel ) ;
8372 } ,
8473 async getUserPost ( options ) {
85- try {
86- const { Post : postSchema } = this . getSchemas ( ) ;
87- const doc = await postSchema . findOne ( { userId : options . userId , _id : options . postId } ) . lean ( ) . exec ( ) ;
88- if ( ! doc ) {
89- throw new errors . NotFound ( `Post with id ${ options . postId } not found.` ) ;
90- }
91- return mapper . toDomainModel ( doc , PostDomainModel ) ;
92- } catch ( error ) {
93- throw error ;
74+ const { Post : postSchema } = this . getSchemas ( ) ;
75+ const doc = await postSchema . findOne ( { userId : options . userId , _id : options . postId } ) . lean ( ) . exec ( ) ;
76+ if ( ! doc ) {
77+ throw new errors . NotFound ( `Post with id ${ options . postId } not found.` ) ;
9478 }
79+ return mapper . toDomainModel ( doc , PostDomainModel ) ;
9580 } ,
9681} ;
9782
98-
9983module . exports . init = ( { Post } ) => Object . assign ( Object . create ( postStore ) , {
10084 getSchemas ( ) {
10185 return {
10286 Post,
10387 } ;
10488 } ,
10589} ) ;
106-
0 commit comments