checkpoint
need polishing
This commit is contained in:
1 parent
0351726eeb
commit
5a457fa99f
11 files changed
+1610
-90
No files matched your search
@@ -148,6 +148,19 @@ export class LiveBlogService implements OnModuleInit {
|
||||
return liveBlog;
|
||||
}
|
||||
|
||||
async findOneWithoutIncrement(id: string): Promise<LiveBlog> {
|
||||
const liveBlog = await this.liveBlogRepository.findOne({
|
||||
where: { id },
|
||||
relations: ['author', 'category', 'updates'],
|
||||
});
|
||||
|
||||
if (!liveBlog) {
|
||||
throw new NotFoundException(`Live blog with ID ${id} not found`);
|
||||
}
|
||||
|
||||
return liveBlog;
|
||||
}
|
||||
|
||||
async findBySlug(slug: string): Promise<LiveBlog> {
|
||||
const liveBlog = await this.liveBlogRepository.findOne({
|
||||
where: { slug },
|
||||
@@ -165,20 +178,82 @@ export class LiveBlogService implements OnModuleInit {
|
||||
}
|
||||
|
||||
async update(id: string, dto: UpdateLiveBlogDto): Promise<LiveBlog> {
|
||||
const liveBlog = await this.findOne(id);
|
||||
Object.assign(liveBlog, dto);
|
||||
|
||||
const updatedBlog = await this.liveBlogRepository.save(liveBlog);
|
||||
|
||||
// Emit status change event
|
||||
if (dto.status && dto.status !== liveBlog.status) {
|
||||
this.eventEmitter.emit('live-blog.status-change', {
|
||||
blogId: id,
|
||||
status: dto.status,
|
||||
});
|
||||
// Build SET clause for update
|
||||
const setClauses: string[] = [];
|
||||
const params: any[] = [];
|
||||
|
||||
if (dto.title !== undefined) {
|
||||
setClauses.push('title = ?');
|
||||
params.push(dto.title);
|
||||
}
|
||||
|
||||
if (dto.slug !== undefined) {
|
||||
setClauses.push('slug = ?');
|
||||
params.push(dto.slug);
|
||||
}
|
||||
|
||||
if (dto.description !== undefined) {
|
||||
setClauses.push('description = ?');
|
||||
params.push(dto.description);
|
||||
}
|
||||
|
||||
if (dto.status !== undefined) {
|
||||
setClauses.push('status = ?');
|
||||
params.push(dto.status);
|
||||
}
|
||||
|
||||
if (dto.strapiId !== undefined) {
|
||||
setClauses.push('strapiId = ?');
|
||||
params.push(dto.strapiId);
|
||||
}
|
||||
|
||||
if (dto.authorId !== undefined) {
|
||||
setClauses.push('authorId = ?');
|
||||
params.push(dto.authorId);
|
||||
}
|
||||
|
||||
if (dto.categoryId !== undefined) {
|
||||
setClauses.push('categoryId = ?');
|
||||
params.push(dto.categoryId);
|
||||
}
|
||||
|
||||
// Always update updatedAt
|
||||
setClauses.push('updatedAt = CURRENT_TIMESTAMP');
|
||||
|
||||
if (setClauses.length === 0) {
|
||||
// Nothing to update
|
||||
return this.findOneWithoutIncrement(id);
|
||||
}
|
||||
|
||||
// Add id to params
|
||||
params.push(id);
|
||||
|
||||
// Execute raw SQL update
|
||||
const queryRunner = this.liveBlogRepository.manager.connection.createQueryRunner();
|
||||
await queryRunner.connect();
|
||||
|
||||
try {
|
||||
await queryRunner.query(
|
||||
`UPDATE live_blogs SET ${setClauses.join(', ')} WHERE id = ?`,
|
||||
params
|
||||
);
|
||||
|
||||
// Emit status change event if status changed
|
||||
if (dto.status) {
|
||||
const currentBlog = await this.findOneWithoutIncrement(id);
|
||||
if (currentBlog && dto.status !== currentBlog.status) {
|
||||
this.eventEmitter.emit('live-blog.status-change', {
|
||||
blogId: id,
|
||||
status: dto.status,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Return the updated entity
|
||||
return this.findOneWithoutIncrement(id);
|
||||
} finally {
|
||||
await queryRunner.release();
|
||||
}
|
||||
|
||||
return updatedBlog;
|
||||
}
|
||||
|
||||
async remove(id: string): Promise<void> {
|
||||
|
||||
Reference in new issue
Block a user