2025-07-31 Line 訂單系統-「第一塊積木」 進入 A 階段真正實作

進入 A 階段真正實作

📗 A 階段 — 系統最小可運作核心(MVP)真正實作

我會用你「單人、慢慢堆積木」的方式,從最小、最簡單、最乾淨、最不會出錯」的 第一塊積木 開始:


📗 A 階段 — 實作開始

以下內容是「真的可以跑起來的第一階段」,並且會一步步堆這個後端系統。


✅ A-1:建立後端專案(NestJS)

你現在的第一個實作目標只有兩個:

  1. 跑得起來的 NestJS 專案
  2. 最基本的 Products 模組(CRUD)

這些會是整個系統的基底。


▶️ A-1-1 建立 Nest 專案

1
2
npm i -g @nestjs/cli
nest new line-order-backend

選:npm

然後進入專案:

1
2
cd line-order-backend
npm run start:dev

看到:

1
[Nest] ... Listening on port 3000

表示成功。


▶️ A-1-2 建立 Products Module(第一塊積木)

我們建立以下檔案:

1
2
3
4
5
6
7
8
9
src/
└── products/
├── products.module.ts
├── products.service.ts
├── products.controller.ts
├── dto/
│ └── create-product.dto.ts
└── entities/
└── product.entity.ts

我直接產生「完整版、可執行」的程式碼給你。


📦 Products Module 完整程式碼(A 階段正式開始)

1️⃣ product.entity.ts

1
2
3
4
5
export class Product {
id: number;
name: string;
price: number;
}

2️⃣ create-product.dto.ts

1
2
3
4
5
6
7
8
9
import { IsString, IsNumber } from 'class-validator';

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

@IsNumber()
price: number;
}

3️⃣ products.service.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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[] = [];
private idCounter = 1;

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

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

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

4️⃣ products.controller.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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);
}
}

5️⃣ products.module.ts

1
2
3
4
5
6
7
8
9
import { Module } from '@nestjs/common';
import { ProductsService } from './products.service';
import { ProductsController } from './products.controller';

@Module({
controllers: [ProductsController],
providers: [ProductsService],
})
export class ProductsModule {}

6️⃣ 把 ProductsModule 加入 AppModule

app.module.ts 加一行:

1
2
3
4
5
6
import { ProductsModule } from './products/products.module';

@Module({
imports: [ProductsModule],
})
export class AppModule {}

▶️ A-1 完成後你可以測試

GET http://localhost:3000/products

回應:

1
[]

POST http://localhost:3000/products

Body:

1
2
3
4
{
"name": "Latte",
"price": 120
}

回應:

1
2
3
4
5
{
"id": 1,
"name": "Latte",
"price": 120
}

🎉 恭喜:A 階段「第一塊積木」正式完成!

到這裡為止,你已經成功建立:

  • ✔ NestJS 專案
  • ✔ 第一個 Products 模組(CRUD)
  • ✔ DTO、Validation、Service、Controller、Module 的基本架構
  • ✔ 可運作 API

這是所有後續 LINE webhook、訂單管理、Redis queue、前端 Vue3、整個 A~G 階段的基礎。


📘 接下來該做什麼?

下一個步驟(A 階段的第二塊積木)有兩個選擇:

A-2(後端路線)

➜ 建立 Orders Module

  • create-order
  • find-all-orders
  • 內部假資料(不用 DB)

A-3(資料庫路線)

➜ 加入 MySQL + TypeORM(或 Prisma)

  • 建 product table
  • 建 order table
  • 把 A 階段產品 + 訂單 CRUD 跑到真資料庫

👉 你要先走 A-2(繼續後端),還是 A-3(加入資料庫)?