2025-07-25 Line 訂單系統- NestJS 第1 ~7步驟

2025-07-25 Line 訂單系統- NestJS 第1 ~7步驟

具備:
✔ 模組化概念
✔ Controller / Service 分層的概念
✔ DTO
✔ DI 注入
✔ Entity / Interface
✔ 響應流程從 Service → Controller 的規劃

你已具備理解 NestJS 的最核心抽象能力。
現在只需要 把抽象變成具體,你就能真正用 NestJS 堆出你的藍圖。


🎉 那我們立即開始「第一塊積木」吧!

我會帶你一步一步建:

  1. Module
  2. Controller
  3. Service
  4. DTO(含 Validation)
  5. Entity
  6. DI 注入
  7. 第一個 REST API:GET /products

做完後,你會有一個 完整能用的 Products 資料夾架構,後續所有功能(Orders、LINE Webhook、AI客服、Dashboard…)都可以複製這個模式。


🧱【第一步】建立 Products Module(積木 01)

請在 NestJS 專案中執行:

1
nest g module products

這會建立:

1
src/products/products.module.ts

🧱【第二步】建立 Controller(積木 02)

1
nest g controller products

建立:

1
src/products/products.controller.ts

🧱【第三步】建立 Service(積木 03)

1
nest g service products

建立:

1
src/products/products.service.ts

此時你的結構應該是:

1
2
3
4
src/products/
├── products.module.ts
├── products.controller.ts
└── products.service.ts

⚡ 已經是企業級架構的苗芽!


🧱【第四步】建立 Entity(積木 04)

建立一個 product.entity.ts

1
2
3
4
5
6
7
// src/products/entities/product.entity.ts
export class Product {
id: number;
name: string;
price: number;
stock: number;
}

🧱【第五步】建立 DTO(積木 05)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// src/products/dto/create-product.dto.ts
import { IsString, IsNumber, IsNotEmpty } from 'class-validator';

export class CreateProductDto {
@IsString()
@IsNotEmpty()
name: string;

@IsNumber()
price: number;

@IsNumber()
stock: number;
}

並記得在 main.ts 加 validation pipeline:

1
app.useGlobalPipes(new ValidationPipe());

🧱【第六步】Service 商業邏輯(積木 06)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// src/products/products.service.ts
import { Injectable } from '@nestjs/common';
import { Product } from './entities/product.entity';
import { CreateProductDto } from './dto/create-product.dto';

@Injectable()
export class ProductsService {
private products: Product[] = [];

findAll(): Product[] {
return this.products;
}

create(dto: CreateProductDto): Product {
const newProduct: Product = {
id: this.products.length + 1,
...dto,
};

this.products.push(newProduct);
return newProduct;
}
}

🧱【第七步】Controller 定義 API(積木 07)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// src/products/products.controller.ts

import { Controller, Get, Post, Body } from '@nestjs/common';
import { ProductsService } from './products.service';
import { CreateProductDto } from './dto/create-product.dto';

@Controller('products')
export class ProductsController {
constructor(private readonly productsService: ProductsService) {}

@Get()
findAll() {
return this.productsService.findAll();
}

@Post()
create(@Body() dto: CreateProductDto) {
return this.productsService.create(dto);
}
}

🎉 恭喜!

你已完成第一塊完整的 NestJS 積木:

1
2
3
4
5
6
7
ProductsModule
ProductsController
ProductsService
DTO + Validation
Entity
DI 完成
API 完成

而且架構已經完全符合:

✔ DI
✔ 分層
✔ 乾淨架構
✔ RESTful API
✔ 可擴展性


接下來你可以選擇:

A. 我繼續帶你做「Orders module」

(你會學到 service 互相注入、order → product 之間邏輯)

B. 我帶你做「LINE Webhook module」

(開始整合 LINE bot)

C. 我帶你做「資料庫 MySQL 版本(TypeORM/Prisma)」

(把 in-memory 改成真實 DB)

D. 帶你做「後台管理 UI + API」

你想先堆哪一塊?