| 1 | import { Injectable } from '@nestjs/common' |
| 2 | import { InjectModel } from '@nestjs/mongoose' |
| 3 | import { AccountType, TableDto } from '@yikart/common' |
| 4 | import { Model, RootFilterQuery } from 'mongoose' |
| 5 | import { DB_CONNECTION_NAME } from '../common' |
| 6 | import { ReplyCommentRecord } from '../schemas' |
| 7 | import { BaseRepository } from './base.repository' |
| 8 | |
| 9 | @Injectable() |
| 10 | export class ReplyCommentRecordRepository extends BaseRepository<ReplyCommentRecord> { |
| 11 | constructor( |
| 12 | @InjectModel(ReplyCommentRecord.name, DB_CONNECTION_NAME) private replyCommentRecordModel: Model<ReplyCommentRecord>, |
| 13 | ) { |
| 14 | super(replyCommentRecordModel) |
| 15 | } |
| 16 | |
| 17 | async add(data: Partial<ReplyCommentRecord>): Promise<ReplyCommentRecord> { |
| 18 | const createdRecord = new this.replyCommentRecordModel(data) |
| 19 | const saved = await createdRecord.save() |
| 20 | return saved.toObject() |
| 21 | } |
| 22 | |
| 23 | async getList( |
| 24 | filters: { |
| 25 | userId: string |
| 26 | accountId?: string |
| 27 | type?: AccountType |
| 28 | worksId?: string |
| 29 | time?: [Date?, Date?, ...unknown[]] |
| 30 | }, |
| 31 | page: TableDto, |
| 32 | ): Promise<{ |
| 33 | total: number |
| 34 | list: ReplyCommentRecord[] |
| 35 | }> { |
| 36 | const filter: RootFilterQuery<ReplyCommentRecord> = { |
| 37 | userId: filters.userId, |
| 38 | ...(filters.time && filters.time.length === 2 && { |
| 39 | createdAt: { $gte: filters.time[0], $lte: filters.time[1] }, |
| 40 | }), |
| 41 | ...(filters.accountId && { accountId: filters.accountId }), |
| 42 | ...(filters.type && { type: filters.type }), |
| 43 | ...(filters.worksId && { worksId: filters.worksId }), |
| 44 | } |
| 45 | |
| 46 | const [list, total] = await Promise.all([ |
| 47 | this.replyCommentRecordModel |
| 48 | .find(filter) |
| 49 | .sort({ createdAt: -1 }) |
| 50 | .skip(((page.pageNo || 1) - 1) * page.pageSize) |
| 51 | .limit(page.pageSize) |
| 52 | .lean({ virtuals: true }) |
| 53 | .exec(), |
| 54 | this.replyCommentRecordModel.countDocuments(filter), |
| 55 | ]) |
| 56 | |
| 57 | return { total, list } |
| 58 | } |
| 59 | |
| 60 | async delete(id: string): Promise<{ deleted: boolean }> { |
| 61 | const result = await this.replyCommentRecordModel.deleteOne({ _id: id }).exec() |
| 62 | return { deleted: result.deletedCount > 0 } |
| 63 | } |
| 64 | } |
| 65 |