0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

Added add method for snippets controller

We force the updatedAt here because there is a bug in the original API
This commit is contained in:
Fabien "egg" O'Carroll 2023-11-29 13:37:49 +00:00
parent e1d2905d21
commit 1abc99cab3
2 changed files with 27 additions and 1 deletions

View file

@ -9,6 +9,14 @@ export class SnippetsService {
@Inject('SnippetsRepository') private readonly repository: SnippetsRepository
) {}
async create(data: any): Promise<Snippet> {
const snippet = Snippet.create(data);
await this.repository.save(snippet);
return snippet;
}
async getOne(id: ObjectID) {
return this.repository.getOne(id);
}

View file

@ -1,10 +1,13 @@
import {Controller, Get, Param, Query} from '@nestjs/common';
import {Body, Controller, Get, Param, Post, Query, UseInterceptors} from '@nestjs/common';
import {SnippetsService} from '../../core/snippets/snippets.service';
import {SnippetDTO} from './snippet.dto';
import {Pagination} from '../../common/pagination.type';
import ObjectID from 'bson-objectid';
import {now} from '../../common/date';
import {LocationHeaderInterceptor} from './interceptors/location-header.interceptor';
@Controller('snippets')
@UseInterceptors(LocationHeaderInterceptor)
export class SnippetsController {
constructor(private readonly service: SnippetsService) {}
@ -22,6 +25,21 @@ export class SnippetsController {
};
}
@Post('')
async add(
@Body() body: any,
@Query('formats') formats?: 'mobiledoc' | 'lexical'
): Promise<{snippets: [SnippetDTO]}> {
const snippet = await this.service.create({
...body.snippets[0],
updatedAt: now()
});
return {
snippets: [new SnippetDTO(snippet, {formats})]
};
}
@Get('')
async browse(
@Query('formats') formats?: 'mobiledoc' | 'lexical',