Files
glossary_api/src/entries/entries.controller.ts

61 lines
1.5 KiB
TypeScript

import {
Body,
Controller,
Delete,
Get,
Inject,
Param,
Post,
Put,
} from '@nestjs/common';
import { EntryService } from './entries.service';
import { EntryDTO } from './entries.dto';
import { Entry } from './entries.entity';
@Controller('entries')
export class EntriesController {
constructor(
@Inject(EntryService)
private readonly entryService: EntryService,
) {}
@Post()
async saveEntry(@Body() entry: EntryDTO): Promise<string | undefined> {
return (await this.entryService.save(entry)).uuid;
}
@Put(':uuid')
async updateEntry(
@Param('uuid') uuid: string,
@Body() entry: EntryDTO,
): Promise<string | undefined> {
return (await this.entryService.updateByUuid(uuid, entry)).uuid;
}
@Get()
async findAll(): Promise<Entry[]> {
const entries = await this.entryService.findAll();
return entries.sort((a, b) => {
return (
new Date(b.created_at as Date).getTime() -
new Date(a.created_at as Date).getTime()
);
});
}
@Get(':uuid')
async findOneByUuid(@Param('uuid') uuid: string): Promise<Entry | null> {
return await this.entryService.findOneByUuid(uuid);
}
@Delete(':uuid')
async softDelete(@Param('uuid') uuid: string): Promise<void> {
await this.entryService.softDeleteByUuid(uuid);
}
@Put('/restore/:uuid')
async restoreSoftDeleted(@Param('uuid') uuid: string): Promise<Entry | null> {
return await this.entryService.restoreDeletedByUuid(uuid);
}
}