Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | 6x 6x 6x 6x 6x 6x | import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CustomSnippet } from '../core-entities';
import {
CreateCustomSnippetDto,
UpdateCustomSnippetDto,
} from './dto/custom-snippet.dto';
@Injectable()
export class CustomSnippetsService {
constructor(
@InjectRepository(CustomSnippet)
private customSnippetsRepository: Repository<CustomSnippet>,
) {}
async create(
createCustomSnippetDto: CreateCustomSnippetDto,
): Promise<CustomSnippet> {
const snippet = this.customSnippetsRepository.create(
createCustomSnippetDto,
);
return this.customSnippetsRepository.save(snippet);
}
async findAll(): Promise<CustomSnippet[]> {
return this.customSnippetsRepository.find({ order: { prefix: 'ASC' } });
}
async findOne(id: string): Promise<CustomSnippet> {
const snippet = await this.customSnippetsRepository.findOneBy({ id });
Iif (!snippet) {
throw new NotFoundException(`CustomSnippet with ID "${id}" not found`);
}
return snippet;
}
async update(
id: string,
updateCustomSnippetDto: UpdateCustomSnippetDto,
): Promise<CustomSnippet> {
const snippet = await this.findOne(id);
this.customSnippetsRepository.merge(snippet, updateCustomSnippetDto);
return this.customSnippetsRepository.save(snippet);
}
async remove(id: string): Promise<void> {
const snippet = await this.findOne(id);
await this.customSnippetsRepository.remove(snippet);
}
async duplicate(id: string): Promise<CustomSnippet> {
const originalSnippet = await this.findOne(id);
let newPrefix = `${originalSnippet.prefix}_copy`;
let suffix = 1;
while (
await this.customSnippetsRepository.findOneBy({ prefix: newPrefix })
) {
suffix++;
newPrefix = `${originalSnippet.prefix}_copy_${suffix}`;
}
const newSnippet = this.customSnippetsRepository.create({
prefix: newPrefix,
description: originalSnippet.description,
body: originalSnippet.body,
scope: originalSnippet.scope,
});
return this.customSnippetsRepository.save(newSnippet);
}
}
|