mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
Added minimal repository structure for Snippets
refs https://github.com/TryGhost/Arch/issues/99 - Adds connection between snippets in NestJS application to the models in the legacy Ghost codebase to link snippets to real data - The NestJS controller code can be executed by commenting out `router.get('/snippets', mw.authAdminApi, http(api.snippets.browse));` in the ghost core Admin API routes file
This commit is contained in:
parent
7e152e638f
commit
71495ed22f
8 changed files with 77 additions and 16 deletions
|
@ -0,0 +1,23 @@
|
|||
import {Inject} from '@nestjs/common';
|
||||
import {Snippet} from './Snippet';
|
||||
import {ISnippetsRepository} from './snippets.repository.interface';
|
||||
|
||||
type QueryOptions = {
|
||||
debug?: boolean;
|
||||
}
|
||||
|
||||
type BookshelfModels = {
|
||||
Snippet: {
|
||||
findPage: (options: QueryOptions) => Promise<Snippet[]>;
|
||||
}
|
||||
};
|
||||
|
||||
export class SnippetRepositoryBookshelf implements ISnippetsRepository {
|
||||
constructor(
|
||||
@Inject('models') private readonly models: BookshelfModels
|
||||
) {}
|
||||
|
||||
async findAll(options: QueryOptions): Promise<Snippet[]> {
|
||||
return this.models.Snippet.findPage(options);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
import {Snippet} from './Snippet';
|
||||
import {ISnippetsRepository} from './snippets.repository.interface';
|
||||
|
||||
export class SnippetsRepositoryInMemory implements ISnippetsRepository {
|
||||
snippets: Snippet[];
|
||||
|
||||
constructor(
|
||||
) {
|
||||
this.snippets = [];
|
||||
}
|
||||
|
||||
async findAll(): Promise<Snippet[]> {
|
||||
return this.snippets;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
import {Snippet} from './Snippet';
|
||||
|
||||
export interface ISnippetsRepository {
|
||||
findAll(options: {debug?: boolean}): Promise<Snippet[]>;
|
||||
}
|
15
ghost/ghost/src/core/snippets/snippets.service.ts
Normal file
15
ghost/ghost/src/core/snippets/snippets.service.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
import {Injectable, Inject} from '@nestjs/common';
|
||||
import {Snippet} from './Snippet';
|
||||
import {ISnippetsRepository} from './snippets.repository.interface';
|
||||
|
||||
@Injectable()
|
||||
export class SnippetsService {
|
||||
constructor(
|
||||
@Inject('SnippetsRepository') private readonly repository: ISnippetsRepository
|
||||
) {}
|
||||
|
||||
async browse(): Promise<Snippet[]> {
|
||||
console.log('getting snippets from NEST!');
|
||||
return this.repository.findAll({});
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
import {Test} from '@nestjs/testing';
|
||||
import {SnippetsController} from './snippets.controller';
|
||||
import {SnippetsService} from './snippets.service';
|
||||
import {SnippetsService} from '../../core/snippets/snippets.service';
|
||||
import {SnippetsRepositoryInMemory} from '../../core/snippets/snippets.repository.inmemory';
|
||||
import assert from 'assert/strict';
|
||||
import sinon from 'sinon';
|
||||
|
||||
|
@ -11,7 +12,13 @@ describe('SnippetsController', () => {
|
|||
beforeEach(async () => {
|
||||
const moduleRef = await Test.createTestingModule({
|
||||
controllers: [SnippetsController],
|
||||
providers: [SnippetsService]
|
||||
providers: [
|
||||
{
|
||||
provide: 'SnippetsRepository',
|
||||
useClass: SnippetsRepositoryInMemory
|
||||
},
|
||||
SnippetsService
|
||||
]
|
||||
}).compile();
|
||||
|
||||
snippetsService = moduleRef.get<SnippetsService>(SnippetsService);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {Controller, Get} from '@nestjs/common';
|
||||
import {SnippetsService} from './snippets.service';
|
||||
import {SnippetsService} from '../../core/snippets/snippets.service';
|
||||
|
||||
@Controller('snippets')
|
||||
export class SnippetsController {
|
||||
|
|
|
@ -1,12 +0,0 @@
|
|||
import {Injectable} from '@nestjs/common';
|
||||
import {Snippet} from '../../core/snippets/Snippet';
|
||||
|
||||
@Injectable()
|
||||
export class SnippetsService {
|
||||
// @NOTE: inject repository here using models
|
||||
// constructor(@Inject('models') private readonly models) {}
|
||||
|
||||
browse(): Snippet[] {
|
||||
return [];
|
||||
}
|
||||
}
|
|
@ -1,9 +1,17 @@
|
|||
import {SnippetsController} from '../http/controllers/snippets.controller';
|
||||
import {SnippetRepositoryBookshelf} from '../core/snippets/snippets.repository.bookshelf';
|
||||
import {SnippetsService} from '../core/snippets/snippets.service';
|
||||
|
||||
class AppModuleClass {}
|
||||
|
||||
export const AppModule = {
|
||||
module: AppModuleClass,
|
||||
controllers: [SnippetsController],
|
||||
providers: []
|
||||
providers: [
|
||||
{
|
||||
provide: 'SnippetsRepository',
|
||||
useClass: SnippetRepositoryBookshelf
|
||||
},
|
||||
SnippetsService
|
||||
]
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue