返回 AiToEarn
publish.service.ts
根目录 / project / aitoearn-electron / server / src / modules / publish / publish.service.ts
1 /*
2 * @Author: nevin
3 * @Date: 2024-06-17 19:19:15
4 * @LastEditTime: 2024-09-05 15:19:25
5 * @LastEditors: nevin
6 * @Description: PubRecord
7 */
8 import { Injectable } from '@nestjs/common';
9 import { InjectModel } from '@nestjs/mongoose';
10 import { Model, RootFilterQuery } from 'mongoose';
11 import { PubRecord, PubStatus } from 'src/db/schema/pubRecord.schema';
12 import { TableDto } from 'src/global/dto/table.dto';
13 import { PubRecordListDto } from './dto/publish.dto';
14
15 @Injectable()
16 export class PublishService {
17 constructor(
18 @InjectModel(PubRecord.name)
19 private readonly PubRecordModel: Model<PubRecord>,
20 ) {}
21
22 async createPubRecord(newData: Partial<PubRecord>) {
23 // 获取当前最大的 id
24 const maxRecord = await this.PubRecordModel.findOne().sort({ id: -1 });
25 const newId = maxRecord ? maxRecord.id + 1 : 1;
26
27 return await this.PubRecordModel.create({
28 ...newData,
29 id: newId,
30 });
31 }
32
33 /**
34 * 获取发布记录列表
35 * @param userId
36 * @param page
37 * @returns
38 */
39 async getPubRecordList(
40 userId: string,
41 page: TableDto,
42 query: PubRecordListDto,
43 ): Promise<{
44 list: PubRecord[];
45 totalCount: number;
46 }> {
47 const filters: RootFilterQuery<PubRecord> = {
48 userId,
49 ...(query.type !== undefined && { type: query.type }),
50 ...(query.time !== undefined &&
51 query.time.length === 2 && {
52 createdAt: { $gte: query.time[0], $lte: query.time[1] },
53 }),
54 };
55 const list = await this.PubRecordModel.find(filters)
56 .skip((page.pageNo - 1) * page.pageSize)
57 .limit(page.pageSize)
58 .sort({ createdAt: -1 });
59
60 const totalCount = await this.PubRecordModel.countDocuments(filters);
61
62 return {
63 list,
64 totalCount,
65 };
66 }
67
68 /**
69 * 获取发布记录列表
70 * @param userId
71 * @param page
72 * @returns
73 */
74 async getPubRecordDraftsList(
75 userId: string,
76 page: TableDto,
77 query: PubRecordListDto,
78 ): Promise<{
79 list: PubRecord[];
80 totalCount: number;
81 }> {
82 const filters: RootFilterQuery<PubRecord> = {
83 userId,
84 status: PubStatus.UNPUBLISH,
85 ...(query.type !== undefined && { type: query.type }),
86 ...(query.time !== undefined &&
87 query.time.length === 2 && {
88 createdAt: { $gte: query.time[0], $lte: query.time[1] },
89 }),
90 };
91 const list = await this.PubRecordModel.find(filters)
92 .skip((page.pageNo - 1) * page.pageSize)
93 .limit(page.pageSize)
94 .sort({ createdAt: -1 });
95
96 const totalCount = await this.PubRecordModel.countDocuments(filters);
97
98 return {
99 list,
100 totalCount,
101 };
102 }
103
104 // 获取发布记录信息
105 async getPubRecordInfo(id: number) {
106 return await this.PubRecordModel.findOne({ id });
107 }
108
109 // 更新发布记录的状态
110 async updatePubRecordStatus(id: number, status: PubStatus): Promise<boolean> {
111 const res = await this.PubRecordModel.updateOne({ id }, { status });
112 return res.modifiedCount > 0;
113 }
114
115 // 删除发布记录
116 async deletePubRecordById(id: number): Promise<boolean> {
117 const res = await this.PubRecordModel.deleteOne({ id });
118 return res.deletedCount > 0;
119 }
120 }
121
121 lines TYPESCRIPT