1
- import Blog , { IBlog } from '../model/Blog' ;
1
+ import Blog , { BlogModel } from '../model/Blog' ;
2
2
import { Types } from 'mongoose' ;
3
- import { IUser } from '../model/User' ;
3
+ import User from '../model/User' ;
4
4
5
5
export default class BlogRepo {
6
6
7
7
private static AUTHOR_DETAIL = 'name profilePicUrl' ;
8
8
private static BLOG_INFO_ADDITIONAL = '+isSubmitted +isDraft +isPublished +createdBy +updatedBy' ;
9
9
private static BLOG_ALL_DATA = '+text +draftText +isSubmitted +isDraft +isPublished +status +createdBy +updatedBy' ;
10
10
11
- public static async create ( blog : IBlog ) : Promise < IBlog > {
11
+ public static async create ( blog : Blog ) : Promise < Blog > {
12
12
const now = new Date ( ) ;
13
13
blog . createdAt = now ;
14
14
blog . updatedAt = now ;
15
- const createdBlog = await Blog . create ( blog ) ;
15
+ const createdBlog = await BlogModel . create ( blog ) ;
16
16
return createdBlog . toObject ( ) ;
17
17
}
18
18
19
- public static update ( blog : IBlog ) : Promise < any > {
19
+ public static update ( blog : Blog ) : Promise < any > {
20
20
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 ( ) ;
22
22
}
23
23
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 } )
26
26
. populate ( 'author' , this . AUTHOR_DETAIL )
27
- . lean < IBlog > ( )
27
+ . lean < Blog > ( )
28
28
. exec ( ) ;
29
29
}
30
30
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 } )
33
33
. select ( '+text' )
34
34
. populate ( 'author' , this . AUTHOR_DETAIL )
35
- . lean < IBlog > ( )
35
+ . lean < Blog > ( )
36
36
. exec ( ) ;
37
37
}
38
38
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 } )
41
41
. select ( '+text +draftText +isSubmitted +isDraft +isPublished +status' )
42
42
. populate ( 'author' , this . AUTHOR_DETAIL )
43
- . lean < IBlog > ( )
43
+ . lean < Blog > ( )
44
44
. exec ( ) ;
45
45
}
46
46
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 } )
49
49
. select ( this . BLOG_ALL_DATA )
50
50
. populate ( 'author' , this . AUTHOR_DETAIL )
51
- . lean < IBlog > ( )
51
+ . lean < Blog > ( )
52
52
. exec ( ) ;
53
53
}
54
54
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 } )
57
57
. select ( '+text' )
58
58
. populate ( 'author' , this . AUTHOR_DETAIL )
59
- . lean < IBlog > ( )
59
+ . lean < Blog > ( )
60
60
. exec ( ) ;
61
61
}
62
62
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 ( ) ;
65
65
}
66
66
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 } )
69
69
. skip ( limit * ( pageNumber - 1 ) )
70
70
. limit ( limit )
71
71
. populate ( 'author' , this . AUTHOR_DETAIL )
72
72
. sort ( { updatedAt : - 1 } )
73
- . lean < IBlog > ( )
73
+ . lean < Blog > ( )
74
74
. exec ( ) ;
75
75
}
76
76
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 } )
79
79
. populate ( 'author' , this . AUTHOR_DETAIL )
80
80
. sort ( { updatedAt : - 1 } )
81
- . lean < IBlog > ( )
81
+ . lean < Blog > ( )
82
82
. exec ( ) ;
83
83
}
84
84
85
- public static findAllDrafts ( ) : Promise < IBlog [ ] > {
85
+ public static findAllDrafts ( ) : Promise < Blog [ ] > {
86
86
return this . findDetailedBlogs ( { isDraft : true , status : true } ) ;
87
87
}
88
88
89
- public static findAllSubmissions ( ) : Promise < IBlog [ ] > {
89
+ public static findAllSubmissions ( ) : Promise < Blog [ ] > {
90
90
return this . findDetailedBlogs ( { isSubmitted : true , status : true } ) ;
91
91
}
92
92
93
- public static findAllPublished ( ) : Promise < IBlog [ ] > {
93
+ public static findAllPublished ( ) : Promise < Blog [ ] > {
94
94
return this . findDetailedBlogs ( { isPublished : true , status : true } ) ;
95
95
}
96
96
97
- public static findAllSubmissionsForWriter ( user : IUser ) : Promise < IBlog [ ] > {
97
+ public static findAllSubmissionsForWriter ( user : User ) : Promise < Blog [ ] > {
98
98
return this . findDetailedBlogs ( { author : user , status : true , isSubmitted : true } ) ;
99
99
}
100
100
101
- public static findAllPublishedForWriter ( user : IUser ) : Promise < IBlog [ ] > {
101
+ public static findAllPublishedForWriter ( user : User ) : Promise < Blog [ ] > {
102
102
return this . findDetailedBlogs ( { author : user , status : true , isPublished : true } ) ;
103
103
}
104
104
105
- public static findAllDraftsForWriter ( user : IUser ) : Promise < IBlog [ ] > {
105
+ public static findAllDraftsForWriter ( user : User ) : Promise < Blog [ ] > {
106
106
return this . findDetailedBlogs ( { author : user , status : true , isDraft : true } ) ;
107
107
}
108
108
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 )
111
111
. select ( this . BLOG_INFO_ADDITIONAL )
112
112
. populate ( 'author' , this . AUTHOR_DETAIL )
113
113
. populate ( 'createdBy' , this . AUTHOR_DETAIL )
114
114
. populate ( 'updatedBy' , this . AUTHOR_DETAIL )
115
115
. sort ( { updatedAt : - 1 } )
116
- . lean < IBlog > ( )
116
+ . lean < Blog > ( )
117
117
. exec ( ) ;
118
118
}
119
119
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 } )
122
122
. skip ( limit * ( pageNumber - 1 ) )
123
123
. limit ( limit )
124
124
. populate ( 'author' , this . AUTHOR_DETAIL )
125
125
. sort ( { publishedAt : - 1 } )
126
- . lean < IBlog > ( )
126
+ . lean < Blog > ( )
127
127
. exec ( ) ;
128
128
}
129
129
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 (
133
133
{
134
134
$text : { $search : blog . title , $caseSensitive : false } ,
135
135
status : true ,
@@ -143,12 +143,12 @@ export default class BlogRepo {
143
143
. sort ( { updatedAt : - 1 } )
144
144
. limit ( limit )
145
145
. sort ( { similarity : { $meta : 'textScore' } } )
146
- . lean < IBlog > ( )
146
+ . lean < Blog > ( )
147
147
. exec ( ) ;
148
148
}
149
149
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 (
152
152
{
153
153
$text : { $search : query , $caseSensitive : false } ,
154
154
status : true ,
@@ -160,12 +160,12 @@ export default class BlogRepo {
160
160
. select ( '-status -description' )
161
161
. limit ( limit )
162
162
. sort ( { similarity : { $meta : 'textScore' } } )
163
- . lean < IBlog > ( )
163
+ . lean < Blog > ( )
164
164
. exec ( ) ;
165
165
}
166
166
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 (
169
169
{
170
170
title : { $regex : `.*${ query } .*` , $options : 'i' } ,
171
171
status : true ,
@@ -174,7 +174,7 @@ export default class BlogRepo {
174
174
. select ( '-status -description' )
175
175
. limit ( limit )
176
176
. sort ( { score : - 1 } )
177
- . lean < IBlog > ( )
177
+ . lean < Blog > ( )
178
178
. exec ( ) ;
179
179
}
180
180
}
0 commit comments