1- import Blog , { IBlog } from '../model/Blog' ;
1+ import Blog , { BlogModel } from '../model/Blog' ;
22import { Types } from 'mongoose' ;
3- import { IUser } from '../model/User' ;
3+ import User from '../model/User' ;
44
55export default class BlogRepo {
66
77 private static AUTHOR_DETAIL = 'name profilePicUrl' ;
88 private static BLOG_INFO_ADDITIONAL = '+isSubmitted +isDraft +isPublished +createdBy +updatedBy' ;
99 private static BLOG_ALL_DATA = '+text +draftText +isSubmitted +isDraft +isPublished +status +createdBy +updatedBy' ;
1010
11- public static async create ( blog : IBlog ) : Promise < IBlog > {
11+ public static async create ( blog : Blog ) : Promise < Blog > {
1212 const now = new Date ( ) ;
1313 blog . createdAt = now ;
1414 blog . updatedAt = now ;
15- const createdBlog = await Blog . create ( blog ) ;
15+ const createdBlog = await BlogModel . create ( blog ) ;
1616 return createdBlog . toObject ( ) ;
1717 }
1818
19- public static update ( blog : IBlog ) : Promise < any > {
19+ public static update ( blog : Blog ) : Promise < any > {
2020 blog . updatedAt = new Date ( ) ;
21- return Blog . updateOne ( { _id : blog . _id } , { $set : { ...blog } } ) . lean < IBlog > ( ) . exec ( ) ;
21+ return BlogModel . updateOne ( { _id : blog . _id } , { $set : { ...blog } } ) . lean < Blog > ( ) . exec ( ) ;
2222 }
2323
24- public static findInfoById ( id : Types . ObjectId ) : Promise < IBlog > {
25- return Blog . findOne ( { _id : id , status : true } )
24+ public static findInfoById ( id : Types . ObjectId ) : Promise < Blog > {
25+ return BlogModel . findOne ( { _id : id , status : true } )
2626 . populate ( 'author' , this . AUTHOR_DETAIL )
27- . lean < IBlog > ( )
27+ . lean < Blog > ( )
2828 . exec ( ) ;
2929 }
3030
31- public static findInfoWithTextById ( id : Types . ObjectId ) : Promise < IBlog > {
32- return Blog . findOne ( { _id : id , status : true } )
31+ public static findInfoWithTextById ( id : Types . ObjectId ) : Promise < Blog > {
32+ return BlogModel . findOne ( { _id : id , status : true } )
3333 . select ( '+text' )
3434 . populate ( 'author' , this . AUTHOR_DETAIL )
35- . lean < IBlog > ( )
35+ . lean < Blog > ( )
3636 . exec ( ) ;
3737 }
3838
39- public static findInfoWithTextAndDraftTextById ( id : Types . ObjectId ) : Promise < IBlog > {
40- return Blog . findOne ( { _id : id , status : true } )
39+ public static findInfoWithTextAndDraftTextById ( id : Types . ObjectId ) : Promise < Blog > {
40+ return BlogModel . findOne ( { _id : id , status : true } )
4141 . select ( '+text +draftText +isSubmitted +isDraft +isPublished +status' )
4242 . populate ( 'author' , this . AUTHOR_DETAIL )
43- . lean < IBlog > ( )
43+ . lean < Blog > ( )
4444 . exec ( ) ;
4545 }
4646
47- public static findBlogAllDataById ( id : Types . ObjectId ) : Promise < IBlog > {
48- return Blog . findOne ( { _id : id , status : true } )
47+ public static findBlogAllDataById ( id : Types . ObjectId ) : Promise < Blog > {
48+ return BlogModel . findOne ( { _id : id , status : true } )
4949 . select ( this . BLOG_ALL_DATA )
5050 . populate ( 'author' , this . AUTHOR_DETAIL )
51- . lean < IBlog > ( )
51+ . lean < Blog > ( )
5252 . exec ( ) ;
5353 }
5454
55- public static findByUrl ( blogUrl : string ) : Promise < IBlog > {
56- return Blog . findOne ( { blogUrl : blogUrl , status : true } )
55+ public static findByUrl ( blogUrl : string ) : Promise < Blog > {
56+ return BlogModel . findOne ( { blogUrl : blogUrl , status : true } )
5757 . select ( '+text' )
5858 . populate ( 'author' , this . AUTHOR_DETAIL )
59- . lean < IBlog > ( )
59+ . lean < Blog > ( )
6060 . exec ( ) ;
6161 }
6262
63- public static findUrlIfExists ( blogUrl : string ) : Promise < IBlog > {
64- return Blog . findOne ( { blogUrl : blogUrl } ) . lean < IBlog > ( ) . exec ( ) ;
63+ public static findUrlIfExists ( blogUrl : string ) : Promise < Blog > {
64+ return BlogModel . findOne ( { blogUrl : blogUrl } ) . lean < Blog > ( ) . exec ( ) ;
6565 }
6666
67- public static findByTagAndPaginated ( tag : string , pageNumber : number , limit : number ) : Promise < IBlog [ ] > {
68- return Blog . find ( { tags : tag , status : true , isPublished : true } )
67+ public static findByTagAndPaginated ( tag : string , pageNumber : number , limit : number ) : Promise < Blog [ ] > {
68+ return BlogModel . find ( { tags : tag , status : true , isPublished : true } )
6969 . skip ( limit * ( pageNumber - 1 ) )
7070 . limit ( limit )
7171 . populate ( 'author' , this . AUTHOR_DETAIL )
7272 . sort ( { updatedAt : - 1 } )
73- . lean < IBlog > ( )
73+ . lean < Blog > ( )
7474 . exec ( ) ;
7575 }
7676
77- public static findAllPublishedForAuthor ( user : IUser ) : Promise < IBlog [ ] > {
78- return Blog . find ( { author : user , status : true , isPublished : true } )
77+ public static findAllPublishedForAuthor ( user : User ) : Promise < Blog [ ] > {
78+ return BlogModel . find ( { author : user , status : true , isPublished : true } )
7979 . populate ( 'author' , this . AUTHOR_DETAIL )
8080 . sort ( { updatedAt : - 1 } )
81- . lean < IBlog > ( )
81+ . lean < Blog > ( )
8282 . exec ( ) ;
8383 }
8484
85- public static findAllDrafts ( ) : Promise < IBlog [ ] > {
85+ public static findAllDrafts ( ) : Promise < Blog [ ] > {
8686 return this . findDetailedBlogs ( { isDraft : true , status : true } ) ;
8787 }
8888
89- public static findAllSubmissions ( ) : Promise < IBlog [ ] > {
89+ public static findAllSubmissions ( ) : Promise < Blog [ ] > {
9090 return this . findDetailedBlogs ( { isSubmitted : true , status : true } ) ;
9191 }
9292
93- public static findAllPublished ( ) : Promise < IBlog [ ] > {
93+ public static findAllPublished ( ) : Promise < Blog [ ] > {
9494 return this . findDetailedBlogs ( { isPublished : true , status : true } ) ;
9595 }
9696
97- public static findAllSubmissionsForWriter ( user : IUser ) : Promise < IBlog [ ] > {
97+ public static findAllSubmissionsForWriter ( user : User ) : Promise < Blog [ ] > {
9898 return this . findDetailedBlogs ( { author : user , status : true , isSubmitted : true } ) ;
9999 }
100100
101- public static findAllPublishedForWriter ( user : IUser ) : Promise < IBlog [ ] > {
101+ public static findAllPublishedForWriter ( user : User ) : Promise < Blog [ ] > {
102102 return this . findDetailedBlogs ( { author : user , status : true , isPublished : true } ) ;
103103 }
104104
105- public static findAllDraftsForWriter ( user : IUser ) : Promise < IBlog [ ] > {
105+ public static findAllDraftsForWriter ( user : User ) : Promise < Blog [ ] > {
106106 return this . findDetailedBlogs ( { author : user , status : true , isDraft : true } ) ;
107107 }
108108
109- private static findDetailedBlogs ( query : Object ) : Promise < IBlog [ ] > {
110- return Blog . find ( query )
109+ private static findDetailedBlogs ( query : Object ) : Promise < Blog [ ] > {
110+ return BlogModel . find ( query )
111111 . select ( this . BLOG_INFO_ADDITIONAL )
112112 . populate ( 'author' , this . AUTHOR_DETAIL )
113113 . populate ( 'createdBy' , this . AUTHOR_DETAIL )
114114 . populate ( 'updatedBy' , this . AUTHOR_DETAIL )
115115 . sort ( { updatedAt : - 1 } )
116- . lean < IBlog > ( )
116+ . lean < Blog > ( )
117117 . exec ( ) ;
118118 }
119119
120- public static findLatestBlogs ( pageNumber : number , limit : number ) : Promise < IBlog [ ] > {
121- return Blog . find ( { status : true , isPublished : true } )
120+ public static findLatestBlogs ( pageNumber : number , limit : number ) : Promise < Blog [ ] > {
121+ return BlogModel . find ( { status : true , isPublished : true } )
122122 . skip ( limit * ( pageNumber - 1 ) )
123123 . limit ( limit )
124124 . populate ( 'author' , this . AUTHOR_DETAIL )
125125 . sort ( { publishedAt : - 1 } )
126- . lean < IBlog > ( )
126+ . lean < Blog > ( )
127127 . exec ( ) ;
128128 }
129129
130- public static searchSimilarBlogs ( blog : IBlog , limit : number )
131- : Promise < IBlog [ ] > {
132- return Blog . find (
130+ public static searchSimilarBlogs ( blog : Blog , limit : number )
131+ : Promise < Blog [ ] > {
132+ return BlogModel . find (
133133 {
134134 $text : { $search : blog . title , $caseSensitive : false } ,
135135 status : true ,
@@ -143,12 +143,12 @@ export default class BlogRepo {
143143 . sort ( { updatedAt : - 1 } )
144144 . limit ( limit )
145145 . sort ( { similarity : { $meta : 'textScore' } } )
146- . lean < IBlog > ( )
146+ . lean < Blog > ( )
147147 . exec ( ) ;
148148 }
149149
150- public static search ( query : string , limit : number ) : Promise < IBlog [ ] > {
151- return Blog . find (
150+ public static search ( query : string , limit : number ) : Promise < Blog [ ] > {
151+ return BlogModel . find (
152152 {
153153 $text : { $search : query , $caseSensitive : false } ,
154154 status : true ,
@@ -160,12 +160,12 @@ export default class BlogRepo {
160160 . select ( '-status -description' )
161161 . limit ( limit )
162162 . sort ( { similarity : { $meta : 'textScore' } } )
163- . lean < IBlog > ( )
163+ . lean < Blog > ( )
164164 . exec ( ) ;
165165 }
166166
167- public static searchLike ( query : string , limit : number ) : Promise < IBlog [ ] > {
168- return Blog . find (
167+ public static searchLike ( query : string , limit : number ) : Promise < Blog [ ] > {
168+ return BlogModel . find (
169169 {
170170 title : { $regex : `.*${ query } .*` , $options : 'i' } ,
171171 status : true ,
@@ -174,7 +174,7 @@ export default class BlogRepo {
174174 . select ( '-status -description' )
175175 . limit ( limit )
176176 . sort ( { score : - 1 } )
177- . lean < IBlog > ( )
177+ . lean < Blog > ( )
178178 . exec ( ) ;
179179 }
180180}
0 commit comments