parent
a21a2257db
commit
ded40a2514
@ -1,4 +1,4 @@
|
||||
{
|
||||
"singleQuote": true,
|
||||
"trailingComma": "all"
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,40 @@
|
||||
import {
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
Entity,
|
||||
ManyToOne,
|
||||
PrimaryGeneratedColumn,
|
||||
UpdateDateColumn,
|
||||
} from 'typeorm';
|
||||
import { User } from '../user/user.entity';
|
||||
|
||||
@Entity()
|
||||
export class Document {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column({ type: 'text', nullable: false })
|
||||
title: string;
|
||||
|
||||
@Column({ type: 'text', nullable: false })
|
||||
slug: string;
|
||||
|
||||
@Column({ type: 'text', nullable: false })
|
||||
body: string;
|
||||
|
||||
@CreateDateColumn({
|
||||
type: 'timestamp',
|
||||
default: () => 'CURRENT_TIMESTAMP(6)',
|
||||
})
|
||||
public created_at: Date;
|
||||
|
||||
@UpdateDateColumn({
|
||||
type: 'timestamp',
|
||||
default: () => 'CURRENT_TIMESTAMP(6)',
|
||||
onUpdate: 'CURRENT_TIMESTAMP(6)',
|
||||
})
|
||||
public updated_at: Date;
|
||||
|
||||
@ManyToOne(() => User)
|
||||
author: User;
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UtilityModule } from 'src/modules/utility/utility.module';
|
||||
import { DatabaseModule } from '../database/database.module';
|
||||
import { documentProviders } from './document.providers';
|
||||
import { DocumentService } from './document.service';
|
||||
|
||||
@Module({
|
||||
imports: [DatabaseModule, UtilityModule],
|
||||
providers: [...documentProviders, DocumentService],
|
||||
exports: [DocumentService],
|
||||
})
|
||||
export class DocumentModule {}
|
@ -0,0 +1,17 @@
|
||||
import { Provider } from '@nestjs/common';
|
||||
import { join } from 'path';
|
||||
import { Connection } from 'typeorm';
|
||||
|
||||
import { Document } from './document.entity';
|
||||
|
||||
export const documentProviders: Provider<any>[] = [
|
||||
{
|
||||
provide: 'DOCUMENT_CACHE',
|
||||
useValue: join(process.cwd(), '.cache'),
|
||||
},
|
||||
{
|
||||
provide: 'DOCUMENT_REPOSITORY',
|
||||
useFactory: (connection: Connection) => connection.getRepository(Document),
|
||||
inject: ['DATABASE_CONNECTION'],
|
||||
},
|
||||
];
|
@ -0,0 +1,62 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Document } from './document.entity';
|
||||
import { TokenService } from 'src/modules/utility/services/token.service';
|
||||
import { marked } from 'marked';
|
||||
|
||||
@Injectable()
|
||||
export class DocumentService {
|
||||
constructor(
|
||||
@Inject('DOCUMENT_CACHE')
|
||||
private documentStorage: string,
|
||||
@Inject('DOCUMENT_REPOSITORY')
|
||||
private documentRepository: Repository<Document>,
|
||||
private tokenService: TokenService,
|
||||
) {}
|
||||
|
||||
public slugify(text: string): string {
|
||||
return text
|
||||
.toString()
|
||||
.normalize('NFD') // split an accented letter in the base letter and the accent
|
||||
.replace(/[\u0300-\u036f]/g, '') // remove all previously split accents
|
||||
.toLowerCase()
|
||||
.trim()
|
||||
.replace(/\s+/g, '-')
|
||||
.replace(/[^\w\-]+/g, '')
|
||||
.replace(/\-\-+/g, '-');
|
||||
}
|
||||
|
||||
public async render(input: string): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
marked.parse(input, (err, result) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
resolve(result);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public async getDocumentBySlug(
|
||||
slug: string,
|
||||
): Promise<Document & { html: string }> {
|
||||
const doc = await this.documentRepository.findOne({ where: { slug } });
|
||||
const html = await this.render(doc.body);
|
||||
return {
|
||||
...doc,
|
||||
html,
|
||||
};
|
||||
}
|
||||
|
||||
public async getDocumentByID(
|
||||
id: number,
|
||||
): Promise<Document & { html: string }> {
|
||||
const doc = await this.documentRepository.findOne({ where: { id } });
|
||||
const html = await this.render(doc.body);
|
||||
return {
|
||||
...doc,
|
||||
html,
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in new issue