You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.9 KiB
54 lines
1.9 KiB
import { {{ properCase name }} } from './{{ kebabCase name }}.entity';
|
|
import { Inject, Injectable, NotFoundException } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { {{ properCase name }}Repository } from './{{ kebabCase name }}.repository';
|
|
import { } from './{{ kebabCase name }}.constants';
|
|
import { {{ properCase name }}Dto } from './{{ kebabCase name }}.dto';
|
|
import Optional from 'typescript-optional';
|
|
|
|
|
|
@Injectable()
|
|
export class {{ properCase name }}Service {
|
|
|
|
constructor(
|
|
@InjectRepository({{ properCase name }}Repository)
|
|
private readonly {{ camelCase name }}Repository: {{ properCase name }}Repository,
|
|
) { }
|
|
|
|
async getAll(): Promise<{{ properCase name }}[]> {
|
|
return this.{{ camelCase name }}Repository.find({});
|
|
}
|
|
|
|
async getOneById(id: number): Promise<Optional<{{ properCase name }}>> {
|
|
return this.{{ camelCase name }}Repository.findOneById(id);
|
|
}
|
|
|
|
async saveNew(body: {{ properCase name }}Dto): Promise<{{ properCase name }}> {
|
|
let {{ camelCase name }}New = new {{ properCase name }}();
|
|
|
|
// Complete with the mappings
|
|
|
|
{{ camelCase name }}New = await this.{{ camelCase name }}Repository.save({{ camelCase name }}New);
|
|
|
|
return {{ camelCase name }}New;
|
|
}
|
|
|
|
async update(id: number, body: {{ properCase name }}Dto): Promise<{{ properCase name }}> {
|
|
let {{ camelCase name }}Found = (await this.{{ camelCase name }}Repository.findOneById(id)).orElseThrow(
|
|
() => new NotFoundException(),
|
|
);
|
|
|
|
// Complete with the mappings
|
|
|
|
{{ camelCase name }}Found = await this.{{ camelCase name }}Repository.save({{ camelCase name }}Found);
|
|
|
|
return {{ camelCase name }}Found;
|
|
}
|
|
|
|
async deleteById(id: number): Promise<void> {
|
|
const {{ camelCase name }}Found = (await this.{{ camelCase name }}Repository.findOneById(
|
|
id,
|
|
)).orElseThrow(() => new NotFoundException());
|
|
await this.{{ camelCase name }}Repository.remove({{ camelCase name }}Found);
|
|
}
|
|
}
|