Skip to content

Commit 46f2e31

Browse files
committed
migrate static class to modules export
1 parent 46e42ac commit 46f2e31

File tree

5 files changed

+347
-309
lines changed

5 files changed

+347
-309
lines changed

src/database/repository/ApiKeyRepo.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import ApiKey, { ApiKeyModel } from '../model/ApiKey';
22

3-
export default class ApiRepo {
4-
public static findByKey(key: string): Promise<ApiKey | null> {
5-
return ApiKeyModel.findOne({ key: key, status: true }).lean().exec();
6-
}
3+
async function findByKey(key: string): Promise<ApiKey | null> {
4+
return ApiKeyModel.findOne({ key: key, status: true }).lean().exec();
75
}
6+
7+
export default {
8+
findByKey,
9+
};

src/database/repository/BlogRepo.ts

Lines changed: 195 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -2,180 +2,201 @@ import Blog, { BlogModel } from '../model/Blog';
22
import { Types } from 'mongoose';
33
import User from '../model/User';
44

5-
export default class BlogRepo {
6-
private static AUTHOR_DETAIL = 'name profilePicUrl';
7-
8-
public static async create(blog: Blog): Promise<Blog> {
9-
const now = new Date();
10-
blog.createdAt = now;
11-
blog.updatedAt = now;
12-
const createdBlog = await BlogModel.create(blog);
13-
return createdBlog.toObject();
14-
}
15-
16-
public static update(blog: Blog): Promise<Blog | null> {
17-
blog.updatedAt = new Date();
18-
return BlogModel.findByIdAndUpdate(blog._id, blog, { new: true }).lean().exec();
19-
}
20-
21-
public static findInfoById(id: Types.ObjectId): Promise<Blog | null> {
22-
return BlogModel.findOne({ _id: id, status: true })
23-
.populate('author', this.AUTHOR_DETAIL)
24-
.lean()
25-
.exec();
26-
}
27-
28-
public static findInfoWithTextById(id: Types.ObjectId): Promise<Blog | null> {
29-
return BlogModel.findOne({ _id: id, status: true })
30-
.select('+text')
31-
.populate('author', this.AUTHOR_DETAIL)
32-
.lean()
33-
.exec();
34-
}
35-
36-
public static findInfoWithTextAndDraftTextById(id: Types.ObjectId): Promise<Blog | null> {
37-
return BlogModel.findOne({ _id: id, status: true })
38-
.select('+text +draftText +isSubmitted +isDraft +isPublished +status')
39-
.populate('author', this.AUTHOR_DETAIL)
40-
.lean()
41-
.exec();
42-
}
43-
44-
public static findBlogAllDataById(id: Types.ObjectId): Promise<Blog | null> {
45-
return BlogModel.findOne({ _id: id, status: true })
46-
.select('+text +draftText +isSubmitted +isDraft +isPublished +status +createdBy +updatedBy')
47-
.populate('author', this.AUTHOR_DETAIL)
48-
.lean()
49-
.exec();
50-
}
51-
52-
public static findByUrl(blogUrl: string): Promise<Blog | null> {
53-
return BlogModel.findOne({ blogUrl: blogUrl, status: true })
54-
.select('+text')
55-
.populate('author', this.AUTHOR_DETAIL)
56-
.lean()
57-
.exec();
58-
}
59-
60-
public static findUrlIfExists(blogUrl: string): Promise<Blog | null> {
61-
return BlogModel.findOne({ blogUrl: blogUrl }).lean().exec();
62-
}
63-
64-
public static findByTagAndPaginated(
65-
tag: string,
66-
pageNumber: number,
67-
limit: number,
68-
): Promise<Blog[]> {
69-
return BlogModel.find({ tags: tag, status: true, isPublished: true })
70-
.skip(limit * (pageNumber - 1))
71-
.limit(limit)
72-
.populate('author', this.AUTHOR_DETAIL)
73-
.sort({ updatedAt: -1 })
74-
.lean()
75-
.exec();
76-
}
77-
78-
public static findAllPublishedForAuthor(user: User): Promise<Blog[]> {
79-
return BlogModel.find({ author: user, status: true, isPublished: true })
80-
.populate('author', this.AUTHOR_DETAIL)
81-
.sort({ updatedAt: -1 })
82-
.lean()
83-
.exec();
84-
}
85-
86-
public static findAllDrafts(): Promise<Blog[]> {
87-
return this.findDetailedBlogs({ isDraft: true, status: true });
88-
}
89-
90-
public static findAllSubmissions(): Promise<Blog[]> {
91-
return this.findDetailedBlogs({ isSubmitted: true, status: true });
92-
}
93-
94-
public static findAllPublished(): Promise<Blog[]> {
95-
return this.findDetailedBlogs({ isPublished: true, status: true });
96-
}
97-
98-
public static findAllSubmissionsForWriter(user: User): Promise<Blog[]> {
99-
return this.findDetailedBlogs({ author: user, status: true, isSubmitted: true });
100-
}
101-
102-
public static findAllPublishedForWriter(user: User): Promise<Blog[]> {
103-
return this.findDetailedBlogs({ author: user, status: true, isPublished: true });
104-
}
105-
106-
public static findAllDraftsForWriter(user: User): Promise<Blog[]> {
107-
return this.findDetailedBlogs({ author: user, status: true, isDraft: true });
108-
}
109-
110-
private static findDetailedBlogs(query: Record<string, unknown>): Promise<Blog[]> {
111-
return BlogModel.find(query)
112-
.select('+isSubmitted +isDraft +isPublished +createdBy +updatedBy')
113-
.populate('author', this.AUTHOR_DETAIL)
114-
.populate('createdBy', this.AUTHOR_DETAIL)
115-
.populate('updatedBy', this.AUTHOR_DETAIL)
116-
.sort({ updatedAt: -1 })
117-
.lean()
118-
.exec();
119-
}
120-
121-
public static findLatestBlogs(pageNumber: number, limit: number): Promise<Blog[]> {
122-
return BlogModel.find({ status: true, isPublished: true })
123-
.skip(limit * (pageNumber - 1))
124-
.limit(limit)
125-
.populate('author', this.AUTHOR_DETAIL)
126-
.sort({ publishedAt: -1 })
127-
.lean()
128-
.exec();
129-
}
130-
131-
public static searchSimilarBlogs(blog: Blog, limit: number): Promise<Blog[]> {
132-
return BlogModel.find(
133-
{
134-
$text: { $search: blog.title, $caseSensitive: false },
135-
status: true,
136-
isPublished: true,
137-
_id: { $ne: blog._id },
138-
},
139-
{
140-
similarity: { $meta: 'textScore' },
141-
},
142-
)
143-
.populate('author', this.AUTHOR_DETAIL)
144-
.sort({ updatedAt: -1 })
145-
.limit(limit)
146-
.sort({ similarity: { $meta: 'textScore' } })
147-
.lean()
148-
.exec();
149-
}
150-
151-
public static search(query: string, limit: number): Promise<Blog[]> {
152-
return BlogModel.find(
153-
{
154-
$text: { $search: query, $caseSensitive: false },
155-
status: true,
156-
isPublished: true,
157-
},
158-
{
159-
similarity: { $meta: 'textScore' },
160-
},
161-
)
162-
.select('-status -description')
163-
.limit(limit)
164-
.sort({ similarity: { $meta: 'textScore' } })
165-
.lean()
166-
.exec();
167-
}
168-
169-
public static searchLike(query: string, limit: number): Promise<Blog[]> {
170-
return BlogModel.find({
171-
title: { $regex: `.*${query}.*`, $options: 'i' },
5+
const AUTHOR_DETAIL = 'name profilePicUrl';
6+
7+
async function create(blog: Blog): Promise<Blog> {
8+
const now = new Date();
9+
blog.createdAt = now;
10+
blog.updatedAt = now;
11+
const createdBlog = await BlogModel.create(blog);
12+
return createdBlog.toObject();
13+
}
14+
15+
async function update(blog: Blog): Promise<Blog | null> {
16+
blog.updatedAt = new Date();
17+
return BlogModel.findByIdAndUpdate(blog._id, blog, { new: true }).lean().exec();
18+
}
19+
20+
async function findInfoById(id: Types.ObjectId): Promise<Blog | null> {
21+
return BlogModel.findOne({ _id: id, status: true })
22+
.populate('author', AUTHOR_DETAIL)
23+
.lean()
24+
.exec();
25+
}
26+
27+
async function findInfoWithTextById(id: Types.ObjectId): Promise<Blog | null> {
28+
return BlogModel.findOne({ _id: id, status: true })
29+
.select('+text')
30+
.populate('author', AUTHOR_DETAIL)
31+
.lean()
32+
.exec();
33+
}
34+
35+
async function findInfoWithTextAndDraftTextById(id: Types.ObjectId): Promise<Blog | null> {
36+
return BlogModel.findOne({ _id: id, status: true })
37+
.select('+text +draftText +isSubmitted +isDraft +isPublished +status')
38+
.populate('author', AUTHOR_DETAIL)
39+
.lean()
40+
.exec();
41+
}
42+
43+
async function findBlogAllDataById(id: Types.ObjectId): Promise<Blog | null> {
44+
return BlogModel.findOne({ _id: id, status: true })
45+
.select('+text +draftText +isSubmitted +isDraft +isPublished +status +createdBy +updatedBy')
46+
.populate('author', AUTHOR_DETAIL)
47+
.lean()
48+
.exec();
49+
}
50+
51+
async function findByUrl(blogUrl: string): Promise<Blog | null> {
52+
return BlogModel.findOne({ blogUrl: blogUrl, status: true })
53+
.select('+text')
54+
.populate('author', AUTHOR_DETAIL)
55+
.lean()
56+
.exec();
57+
}
58+
59+
async function findUrlIfExists(blogUrl: string): Promise<Blog | null> {
60+
return BlogModel.findOne({ blogUrl: blogUrl }).lean().exec();
61+
}
62+
63+
async function findByTagAndPaginated(
64+
tag: string,
65+
pageNumber: number,
66+
limit: number,
67+
): Promise<Blog[]> {
68+
return BlogModel.find({ tags: tag, status: true, isPublished: true })
69+
.skip(limit * (pageNumber - 1))
70+
.limit(limit)
71+
.populate('author', AUTHOR_DETAIL)
72+
.sort({ updatedAt: -1 })
73+
.lean()
74+
.exec();
75+
}
76+
77+
async function findAllPublishedForAuthor(user: User): Promise<Blog[]> {
78+
return BlogModel.find({ author: user, status: true, isPublished: true })
79+
.populate('author', AUTHOR_DETAIL)
80+
.sort({ updatedAt: -1 })
81+
.lean()
82+
.exec();
83+
}
84+
85+
async function findAllDrafts(): Promise<Blog[]> {
86+
return findDetailedBlogs({ isDraft: true, status: true });
87+
}
88+
89+
async function findAllSubmissions(): Promise<Blog[]> {
90+
return findDetailedBlogs({ isSubmitted: true, status: true });
91+
}
92+
93+
async function findAllPublished(): Promise<Blog[]> {
94+
return findDetailedBlogs({ isPublished: true, status: true });
95+
}
96+
97+
async function findAllSubmissionsForWriter(user: User): Promise<Blog[]> {
98+
return findDetailedBlogs({ author: user, status: true, isSubmitted: true });
99+
}
100+
101+
async function findAllPublishedForWriter(user: User): Promise<Blog[]> {
102+
return findDetailedBlogs({ author: user, status: true, isPublished: true });
103+
}
104+
105+
async function findAllDraftsForWriter(user: User): Promise<Blog[]> {
106+
return findDetailedBlogs({ author: user, status: true, isDraft: true });
107+
}
108+
109+
async function findDetailedBlogs(query: Record<string, unknown>): Promise<Blog[]> {
110+
return BlogModel.find(query)
111+
.select('+isSubmitted +isDraft +isPublished +createdBy +updatedBy')
112+
.populate('author', AUTHOR_DETAIL)
113+
.populate('createdBy', AUTHOR_DETAIL)
114+
.populate('updatedBy', AUTHOR_DETAIL)
115+
.sort({ updatedAt: -1 })
116+
.lean()
117+
.exec();
118+
}
119+
120+
async function findLatestBlogs(pageNumber: number, limit: number): Promise<Blog[]> {
121+
return BlogModel.find({ status: true, isPublished: true })
122+
.skip(limit * (pageNumber - 1))
123+
.limit(limit)
124+
.populate('author', AUTHOR_DETAIL)
125+
.sort({ publishedAt: -1 })
126+
.lean()
127+
.exec();
128+
}
129+
130+
async function searchSimilarBlogs(blog: Blog, limit: number): Promise<Blog[]> {
131+
return BlogModel.find(
132+
{
133+
$text: { $search: blog.title, $caseSensitive: false },
134+
status: true,
135+
isPublished: true,
136+
_id: { $ne: blog._id },
137+
},
138+
{
139+
similarity: { $meta: 'textScore' },
140+
},
141+
)
142+
.populate('author', AUTHOR_DETAIL)
143+
.sort({ updatedAt: -1 })
144+
.limit(limit)
145+
.sort({ similarity: { $meta: 'textScore' } })
146+
.lean()
147+
.exec();
148+
}
149+
150+
async function search(query: string, limit: number): Promise<Blog[]> {
151+
return BlogModel.find(
152+
{
153+
$text: { $search: query, $caseSensitive: false },
172154
status: true,
173155
isPublished: true,
174-
})
175-
.select('-status -description')
176-
.limit(limit)
177-
.sort({ score: -1 })
178-
.lean()
179-
.exec();
180-
}
156+
},
157+
{
158+
similarity: { $meta: 'textScore' },
159+
},
160+
)
161+
.select('-status -description')
162+
.limit(limit)
163+
.sort({ similarity: { $meta: 'textScore' } })
164+
.lean()
165+
.exec();
181166
}
167+
168+
async function searchLike(query: string, limit: number): Promise<Blog[]> {
169+
return BlogModel.find({
170+
title: { $regex: `.*${query}.*`, $options: 'i' },
171+
status: true,
172+
isPublished: true,
173+
})
174+
.select('-status -description')
175+
.limit(limit)
176+
.sort({ score: -1 })
177+
.lean()
178+
.exec();
179+
}
180+
181+
export default {
182+
create,
183+
update,
184+
findInfoById,
185+
findInfoWithTextById,
186+
findInfoWithTextAndDraftTextById,
187+
findBlogAllDataById,
188+
findByUrl,
189+
findUrlIfExists,
190+
findByTagAndPaginated,
191+
findAllPublishedForAuthor,
192+
findAllDrafts,
193+
findAllSubmissions,
194+
findAllPublished,
195+
findAllSubmissionsForWriter,
196+
findAllPublishedForWriter,
197+
findAllDraftsForWriter,
198+
findLatestBlogs,
199+
searchSimilarBlogs,
200+
search,
201+
searchLike,
202+
};

0 commit comments

Comments
 (0)