tspace-spear
Version:
tspace-spear is a lightweight, high-performance API framework for Node.js that leverages the native HTTP server and supports uWebSockets.js (C++) for maximum speed and efficiency.
2 lines (1 loc) • 1.5 kB
TypeScript
export declare const ServiceTemplate = "\nimport { \n CreateCatDto, \n UpdateCatDto \n} from \"./cat.dto\";\n\ntype Cat = {\n id: number;\n name: string;\n age: number;\n}\n\nexport class CatService {\n private cats: Cat[] = [\n { id: 1, name: 'cat1', age: 1.6 },\n { id: 2, name: 'cat2', age: 1.8 },\n ];\n public async index() {\n return this.cats;\n };\n\n public async show(id: number) {\n const cat = this.cats.find(cat => cat.id === id);\n if(cat == null) return null;\n return cat;\n };\n\n public async create({ name, age }: CreateCatDto) {\n\n const cat = {\n id: this.cats.length + 1,\n name: name,\n age: age\n };\n\n this.cats.push(cat);\n\n return cat;\n }\n \n public async update(id: number, { name, age }: UpdateCatDto) {\n const index = this.cats.findIndex(d => d.id === id);\n\n if (index === -1) {\n throw new Error(\"Cat not found\");\n }\n\n this.cats[index] = {\n ...this.cats[index],\n ...{ name, age } \n };\n\n const cat = this.cats[index];\n\n return cat;\n }\n\n public async remove(id: number) {\n const index = this.cats.findIndex(d => d.id === id);\n\n if (index === -1) {\n throw new Error(\"Cat not found\");\n }\n\n this.cats.splice(index, 1);\n\n return true;\n }\n}\n";