mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
Finished off BREAD for snippets
This commit is contained in:
parent
7862871203
commit
ee78fd1b14
3 changed files with 77 additions and 4 deletions
|
@ -334,8 +334,10 @@ module.exports = function apiRoutes() {
|
|||
router.get('/snippets/:id', mw.authAdminApi, labs.skipMiddleware('nestSnippetsAPI', http(api.snippets.read)));
|
||||
//router.post('/snippets', mw.authAdminApi, http(api.snippets.add));
|
||||
router.post('/snippets', mw.authAdminApi, labs.skipMiddleware('nestSnippetsAPI', http(api.snippets.add)));
|
||||
router.put('/snippets/:id', mw.authAdminApi, http(api.snippets.edit));
|
||||
router.del('/snippets/:id', mw.authAdminApi, http(api.snippets.destroy));
|
||||
//router.put('/snippets/:id', mw.authAdminApi, http(api.snippets.edit));
|
||||
router.put('/snippets/:id', mw.authAdminApi, labs.skipMiddleware('nestSnippetsAPI', http(api.snippets.edit)));
|
||||
//router.del('/snippets/:id', mw.authAdminApi, http(api.snippets.destroy));
|
||||
router.del('/snippets/:id', mw.authAdminApi, labs.skipMiddleware('nestSnippetsAPI', http(api.snippets.destroy)));
|
||||
|
||||
// ## Custom theme settings
|
||||
router.get('/custom_theme_settings', mw.authAdminApi, http(api.customThemeSettings.browse));
|
||||
|
|
|
@ -9,7 +9,7 @@ export class SnippetsService {
|
|||
@Inject('SnippetsRepository') private readonly repository: SnippetsRepository
|
||||
) {}
|
||||
|
||||
async create(data: any): Promise<Snippet> {
|
||||
async create(data: {name: string, lexical: string, mobiledoc: string}): Promise<Snippet> {
|
||||
const snippet = Snippet.create(data);
|
||||
|
||||
await this.repository.save(snippet);
|
||||
|
@ -17,6 +17,44 @@ export class SnippetsService {
|
|||
return snippet;
|
||||
}
|
||||
|
||||
async delete(id: ObjectID) {
|
||||
const snippet = await this.repository.getOne(id);
|
||||
|
||||
if (!snippet) {
|
||||
return null;
|
||||
}
|
||||
|
||||
snippet.delete();
|
||||
|
||||
await this.repository.save(snippet);
|
||||
|
||||
return snippet;
|
||||
}
|
||||
|
||||
async update(id: ObjectID, data: {name?: string, lexical?: string, mobiledoc?: string}) {
|
||||
const snippet = await this.repository.getOne(id);
|
||||
|
||||
if (!snippet) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (data.name) {
|
||||
snippet.name = data.name;
|
||||
}
|
||||
|
||||
if (data.lexical) {
|
||||
snippet.lexical = data.lexical;
|
||||
}
|
||||
|
||||
if (data.mobiledoc) {
|
||||
snippet.mobiledoc = data.mobiledoc;
|
||||
}
|
||||
|
||||
await this.repository.save(snippet);
|
||||
|
||||
return snippet;
|
||||
}
|
||||
|
||||
async getOne(id: ObjectID) {
|
||||
return this.repository.getOne(id);
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import {Body, Controller, Get, NotFoundException, Param, Post, Query, UseFilters, UseInterceptors} from '@nestjs/common';
|
||||
import {Body, Controller, Delete, Get, HttpCode, Param, Post, Put, Query, UseFilters, UseInterceptors} from '@nestjs/common';
|
||||
import {SnippetsService} from '../../core/snippets/snippets.service';
|
||||
import {SnippetDTO} from './snippet.dto';
|
||||
import {Pagination} from '../../common/pagination.type';
|
||||
|
@ -31,6 +31,39 @@ export class SnippetsController {
|
|||
};
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@HttpCode(204)
|
||||
async destroy(
|
||||
@Param('id') id: 'string'
|
||||
) {
|
||||
const snippet = await this.service.delete(ObjectID.createFromHexString(id));
|
||||
if (snippet === null) {
|
||||
throw new NotFoundError({
|
||||
context: 'Resource could not be found.',
|
||||
message: 'Resource not found error, cannot delete snippet.'
|
||||
});
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
async edit(
|
||||
@Param('id') id: 'string',
|
||||
@Body() body: any,
|
||||
@Query('formats') formats?: 'mobiledoc' | 'lexical'
|
||||
): Promise<{snippets: [SnippetDTO]}> {
|
||||
const snippet = await this.service.update(ObjectID.createFromHexString(id), body.snippets[0]);
|
||||
if (snippet === null) {
|
||||
throw new NotFoundError({
|
||||
context: 'Snippet not found.',
|
||||
message: 'Resource not found error, cannot read snippet.'
|
||||
});
|
||||
}
|
||||
return {
|
||||
snippets: [new SnippetDTO(snippet, {formats})]
|
||||
};
|
||||
}
|
||||
|
||||
@Post('')
|
||||
async add(
|
||||
@Body() body: any,
|
||||
|
|
Loading…
Add table
Reference in a new issue