add content

This commit is contained in:
liuyi 2025-05-19 14:38:28 +08:00
parent c2f263ae82
commit a198ca62b4

View File

@ -1,58 +1,83 @@
import { Injectable, NotFoundException } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { isNil } from '@nestjs/common/utils/shared.utils'; import { isNil } from '@nestjs/common/utils/shared.utils';
import { CreatePostDto } from '@/modules/content/dtos/create-post.dto'; import { isFunction, omit } from 'lodash';
import { UpdatePostDto } from '@/modules/content/dtos/update-post.dto'; import { EntityNotFoundError, IsNull, Not, SelectQueryBuilder } from 'typeorm';
import { PostEntity } from '@/modules/content/types';
import { PostOrder } from '@/modules/content/constants';
import { PostEntity } from '@/modules/content/entities/post.entity';
import { PostRepository } from '@/modules/content/repositories/post.repository';
import { PaginateOptions, QueryHook } from '@/modules/database/types';
import { paginate } from '@/modules/database/utils';
@Injectable() @Injectable()
export class PostService { export class PostService {
protected posts: PostEntity[] = [ constructor(protected repositopry: PostRepository) {}
{ title: '第一篇文章标题', body: '第一篇文章内容' },
{ title: '第二篇文章标题', body: '第二篇文章内容' },
{ title: '第三篇文章标题', body: '第三篇文章内容' },
{ title: '第四篇文章标题', body: '第四篇文章内容' },
{ title: '第五篇文章标题', body: '第五篇文章内容' },
{ title: '第六篇文章标题', body: '第六篇文章内容' },
].map((v, id) => ({ ...v, id }));
async findAll() { async paginate(options: PaginateOptions, callback?: QueryHook<PostEntity>) {
return this.posts; const qb = await this.buildListQuery(this.repositopry.buildBaseQB(), options, callback);
return paginate(qb, options);
} }
async findOne(id: number) { async detail(id: string, callback?: QueryHook<PostEntity>) {
const post = this.posts.find((item) => item.id === id); let qb = this.repositopry.buildBaseQB();
if (isNil(post)) { qb.where(`post.id = :id`, { id });
throw new NotFoundException(`the post with id ${id} not exits!`); qb = !isNil(callback) && isFunction(callback) ? await callback(qb) : qb;
const item = await qb.getOne();
if (!item) {
throw new EntityNotFoundError(PostEntity, `The post ${id} not exists!`);
} }
return post; return item;
} }
async create(data: CreatePostDto) { async create(data: RecordAny) {
const newPost: PostEntity = { const item = await this.repositopry.save(data);
id: Math.max(...this.posts.map(({ id }) => id + 1)), return this.detail(item.id);
...data,
};
this.posts.push(newPost);
return newPost;
} }
async update(data: UpdatePostDto) { async update(data: RecordAny) {
let toUpdate = this.posts.find((item) => item.id === data.id); await this.repositopry.update(data.id, omit(data, ['id']));
if (isNil(toUpdate)) { return this.delete(data.id);
throw new NotFoundException(`the post with id ${data.id} not exits!`); }
async delete(id: string) {
const item = await this.repositopry.findOneByOrFail({ id });
return this.repositopry.remove(item);
}
protected async buildListQuery(
qb: SelectQueryBuilder<PostEntity>,
options: RecordAny,
callback?: QueryHook<PostEntity>,
) {
const { orderBy, isPublished } = options;
if (typeof isPublished === 'boolean') {
isPublished
? qb.where({ publishedAt: Not(IsNull) })
: qb.where({ publishedAt: IsNull() });
} }
toUpdate = { ...toUpdate, ...data }; this.queryOrderBy(qb, orderBy);
this.posts = this.posts.filter((item) => (item.id === data.id ? toUpdate : item)); if (callback) {
return toUpdate; return callback(qb);
}
return qb;
} }
async delete(id: number) { protected queryOrderBy(qb: SelectQueryBuilder<PostEntity>, orderBy?: PostOrder) {
const toDelete = this.posts.find((item) => item.id === id); switch (orderBy) {
if (isNil(toDelete)) { case PostOrder.CREATED:
throw new NotFoundException(`the post with id ${id} not exits!`); return qb.orderBy('post.createdAt', 'DESC');
case PostOrder.UPDATED:
return qb.orderBy('post.updatedAt', 'DESC');
case PostOrder.PUBLISHED:
return qb.orderBy('post.publishedAt', 'DESC');
case PostOrder.CUSTOM:
return qb.orderBy('post.custom', 'DESC');
default:
return qb
.orderBy('post.createdAt', 'DESC')
.addOrderBy('post.updatedAt', 'DESC')
.addOrderBy('post.publishedAt', 'DESC');
} }
this.posts = this.posts.filter((item) => item.id !== id);
return toDelete;
} }
} }