| 1 | import { InjectModel } from '@nestjs/mongoose' |
| 2 | import { Pagination } from '@yikart/common' |
| 3 | import { FilterQuery, Model } from 'mongoose' |
| 4 | import { Blog } from '../schemas' |
| 5 | import { BaseRepository } from './base.repository' |
| 6 | |
| 7 | export interface ListBlogParams extends Pagination { |
| 8 | keyword?: string |
| 9 | createdAt?: Date[] |
| 10 | } |
| 11 | |
| 12 | export class BlogRepository extends BaseRepository<Blog> { |
| 13 | constructor( |
| 14 | @InjectModel(Blog.name) blogModel: Model<Blog>, |
| 15 | ) { |
| 16 | super(blogModel) |
| 17 | } |
| 18 | |
| 19 | async listWithPagination(params: ListBlogParams) { |
| 20 | const { page, pageSize, keyword, createdAt } = params |
| 21 | |
| 22 | const filter: FilterQuery<Blog> = {} |
| 23 | if (createdAt) { |
| 24 | filter.createdAt = { |
| 25 | $gte: createdAt[0], |
| 26 | $lte: createdAt[1], |
| 27 | } |
| 28 | } |
| 29 | if (keyword) { |
| 30 | filter.content = { $regex: keyword, $options: 'i' } |
| 31 | } |
| 32 | |
| 33 | return await this.findWithPagination({ |
| 34 | page, |
| 35 | pageSize, |
| 36 | filter, |
| 37 | options: { sort: { createdAt: -1 } }, |
| 38 | }) |
| 39 | } |
| 40 | } |
| 41 |