140 lines
3.2 KiB
TypeScript
140 lines
3.2 KiB
TypeScript
import {
|
|
Controller,
|
|
Get,
|
|
Post,
|
|
Put,
|
|
Delete,
|
|
Body,
|
|
Param,
|
|
Query,
|
|
ValidationPipe,
|
|
Res,
|
|
Logger,
|
|
Headers,
|
|
} from '@nestjs/common';
|
|
import type { Response as ExpressResponse } from 'express';
|
|
import { LiveBlogService } from './live-blog.service';
|
|
import {
|
|
CreateLiveBlogDto,
|
|
UpdateLiveBlogDto,
|
|
FindLiveBlogsDto,
|
|
CreateLiveBlogUpdateDto,
|
|
UpdateLiveBlogUpdateDto,
|
|
} from './articles.dto';
|
|
|
|
@Controller('live-blogs')
|
|
export class LiveBlogController {
|
|
private readonly logger = new Logger(LiveBlogController.name);
|
|
|
|
constructor(private readonly liveBlogService: LiveBlogService) {}
|
|
|
|
// Live Blog CRUD operations
|
|
@Get('featured')
|
|
getFeatured() {
|
|
this.logger.log('GET /featured called');
|
|
return this.liveBlogService.findPinned();
|
|
}
|
|
|
|
@Get('active')
|
|
getActive() {
|
|
return this.liveBlogService.findActive();
|
|
}
|
|
|
|
@Get('recent')
|
|
getRecent() {
|
|
return this.liveBlogService.getLiveBlogsWithRecentUpdates();
|
|
}
|
|
|
|
@Post()
|
|
create(
|
|
@Body(new ValidationPipe({ transform: true })) dto: CreateLiveBlogDto,
|
|
) {
|
|
return this.liveBlogService.create(dto);
|
|
}
|
|
|
|
@Get()
|
|
findAll(
|
|
@Query(new ValidationPipe({ transform: true })) dto: FindLiveBlogsDto,
|
|
) {
|
|
return this.liveBlogService.findAll(dto);
|
|
}
|
|
|
|
@Get(':id')
|
|
findOne(@Param('id') id: string) {
|
|
return this.liveBlogService.findOne(id);
|
|
}
|
|
|
|
@Get('slug/:slug')
|
|
findBySlug(@Param('slug') slug: string) {
|
|
return this.liveBlogService.findBySlug(slug);
|
|
}
|
|
|
|
@Put(':id')
|
|
update(
|
|
@Param('id') id: string,
|
|
@Body(new ValidationPipe({ transform: true })) dto: UpdateLiveBlogDto,
|
|
) {
|
|
return this.liveBlogService.update(id, dto);
|
|
}
|
|
|
|
@Delete(':id')
|
|
remove(@Param('id') id: string) {
|
|
return this.liveBlogService.remove(id);
|
|
}
|
|
|
|
// Live Blog Updates CRUD operations
|
|
@Post(':id/updates')
|
|
createUpdate(
|
|
@Param('id') liveBlogId: string,
|
|
@Body(new ValidationPipe({ transform: true })) dto: CreateLiveBlogUpdateDto,
|
|
) {
|
|
return this.liveBlogService.createUpdate(dto, liveBlogId);
|
|
}
|
|
|
|
@Get(':id/updates')
|
|
findUpdates(
|
|
@Param('id') liveBlogId: string,
|
|
@Query('page') page = 1,
|
|
@Query('limit') limit = 50,
|
|
) {
|
|
return this.liveBlogService.findUpdates(
|
|
liveBlogId,
|
|
parseInt(page.toString()),
|
|
parseInt(limit.toString()),
|
|
);
|
|
}
|
|
|
|
@Put(':id/updates/:updateId')
|
|
updateUpdate(
|
|
@Param('id') liveBlogId: string,
|
|
@Param('updateId') updateId: string,
|
|
@Body(new ValidationPipe({ transform: true })) dto: UpdateLiveBlogUpdateDto,
|
|
) {
|
|
return this.liveBlogService.updateUpdate(liveBlogId, updateId, dto);
|
|
}
|
|
|
|
@Delete(':id/updates/:updateId')
|
|
removeUpdate(
|
|
@Param('id') liveBlogId: string,
|
|
@Param('updateId') updateId: string,
|
|
) {
|
|
return this.liveBlogService.removeUpdate(liveBlogId, updateId);
|
|
}
|
|
|
|
// SSE endpoint for real-time updates
|
|
@Get(':id/stream')
|
|
stream(
|
|
@Param('id') liveBlogId: string,
|
|
@Res() response: ExpressResponse,
|
|
@Headers('last-event-id') lastEventId?: string,
|
|
) {
|
|
this.logger.log(`SSE connection request for live blog ${liveBlogId}`);
|
|
|
|
if (lastEventId) {
|
|
this.logger.log(`Client resuming with last event ID: ${lastEventId}`);
|
|
}
|
|
|
|
this.liveBlogService.createStream(liveBlogId, response);
|
|
}
|
|
}
|