Newer
Older
import { Controller, Get, HttpException, HttpService, HttpStatus, Logger, Param, Query } from '@nestjs/common';
import { Observable } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { Post } from './schemas/post.schema';
import { PostsService } from './posts.service';
import { Tag } from './schemas/tag.schema';
import { PostWithMeta } from './schemas/postWithMeta.schema';
import { ConfigurationService } from '../configuration/configuration.service';
@Controller('posts')
export class PostsController {
constructor(
private readonly httpService: HttpService,
private readonly postsService: PostsService,
private readonly configService: ConfigurationService
) {}
@Get()
@ApiQuery({ name: 'include', type: String, required: false })
@ApiQuery({ name: 'fields', type: String, required: false })
@ApiQuery({ name: 'formats', type: String, required: false })
@ApiQuery({ name: 'filter', type: String, required: false })
@ApiQuery({ name: 'limit', type: String, required: false })
@ApiQuery({ name: 'page', type: String, required: false })
@ApiQuery({ name: 'order', type: String, required: false })
public async findAll(@Query() query): Promise<PostWithMeta> {
.get(`${process.env.GHOST_HOST_AND_PORT}/ghost/api/v3/content/posts`, {
params: {
key: process.env.GHOST_CONTENT_API_KEY,
include: query.include ? query.include : null,
fields: query.fields ? query.fields : null,
formats: query.formats ? query.formats : null,
filter: query.filter ? query.filter : null,
limit: query.limit ? query.limit : null,
order: query.order ? query.order : null,
page: query.page ? query.page : null,
},
})
.pipe(
map((response) => response.data),
catchError((err) => {
throw new HttpException('Invalid structure id', HttpStatus.BAD_REQUEST);
return new Promise((resolve) => {
result.subscribe((postData: PostWithMeta) => {
postData.posts.forEach((post: Post) => {
// Handle excerpt field in case of no author
if (!post.custom_excerpt) {
post.excerpt = 'Inconnu';
}
// Handle image display. Rewrite image URL to fit ghost infra issue.
if (post.feature_image && !this.configService.isLocalConf()) {
post.feature_image = `https://${this.configService.config.host}/blog/content${
post.feature_image.split('/content')[1]
}`;
}
return post;
});
resolve(postData);
}
@Get('tags')
public async findAllTags(): Promise<{ public: Tag[]; commune: Tag[]; others: Tag[] }> {
return Promise.all([
this.postsService.getLocationTags(),
this.postsService.getPublicTags(),
this.postsService.getRegularTags(),
]).then((data) => {
return { commune: data[0], public: data[1], others: data[2] };
});
@Get(':id')
public async getPostbyId(@Param('id') id: string): Promise<Observable<{ posts: Post }>> {
return this.httpService
.get(`${process.env.GHOST_HOST_AND_PORT}/ghost/api/v3/content/posts/` + id, {
params: {
key: process.env.GHOST_CONTENT_API_KEY,
include: 'tags,authors',
},
})
.pipe(map((response) => response.data));
}