---
# System prepended metadata

title: 飲食紀錄管理

---

# 飲食紀錄管理

# update notes


| 日期 | 說明 |
| -------- | -------- |
| 2020/12/11 | review Meeting 在App"健康總覽"頁面加入"脂肪" |

### 飲食紀錄目標

[ 飲食目標（DietGoals）](Untitled%2010/DietGoals.csv)

# Schema

example:

```json=
    {
        "height":  170,
        "weight": 65,
        "calorie": 1883,
        "protein": 78,
        "dietaryFiber": 117,
        "carbohydrate": 56
    }
```

### 飲食紀錄 (DietDiary)

| Name | Tags | 建立時傳入 |說明 |
| -------- | -------- | -------- | -------- |
| id     | number    | N | 飲食紀錄編號    |
| image   | string    | Y |  新增時，APP 會傳入圖片 base64 字串，後端會儲存成影像檔後，在 image 屬性裡存入飲食紀錄圖片 url  |
| imageType | string | Y | enum: ['jpg', 'png']  |
| patientId   | number    |  Y | 個案 ID (若不傳入 patientId，則會由 token 取得個案資料) |
| time   | timestamp   | Y | 飲食紀錄時間  |
| hhmmOfTime | number  | N | time 的 hhmm 格式，比如早上 10:30, 這個值就是 1030，作為搜尋的條件之用 |
| timePeriod   | number   |Y | 飲食紀錄時段 enum: [1: "早上",  2: "中午", 3: "晚上", 4: "睡前"] |
| timePeriodType   | number   | Y |飲食紀錄餐別 enum: [10: "早餐", 11: "早午餐",  20: "午餐", 21: "下午茶",  30: "晚餐",  31: "點心", 40: "宵夜"] |
| foodId  | number   | Y |飲食紀錄的的食品編號 |
| timeAndTimePeriodType | timestamp | N | 作為排序之用，先依 time 排出每日的飲食紀錄，再依 timePeriodType 來排序 |
| quantity  | number   | Y |飲食紀錄的的食品數量|
| calorie  | number   |  N |建議熱量 (需乘上 quantity，四捨五入到小數點第一位) |
| protein  | number   |  N |建議蛋白質 (需乘上 quantity，四捨五入到小數點第一位) |
| dietaryFiber  | number   |  N |建議膳食纖維 (需乘上 quantity，四捨五入到小數點第一位) |
| carbohydrate  | number   |  N |建議碳水化合|物 (需乘上 quantity，四捨五入到小數點第一位) |
| fat | number | N | 脂肪 |

example:

```json=
    {
        "id": 1,
        "image": "http://example.com/image.jpg",
        "time": 1585546156101,
    	"patientId": 6,
        "timePeriod": 2,
    	"timePeriodType": 21,
        "foodId": 12,
        "quantity": 2,
        "calorie": 500,
        "protein": 12.9,
        "dietaryFiber": 8.09,
        "carbohydrate": 73.0587,
        "fat": 30
    }
```

### 食品

[食品(Food)](Untitled%2010/Food.csv)

example:

```json=
    {
        "id": 1,
        "name": "美而美 紅茶",
        "foodType": "飲料類",
        "unit":　"ml",
        "calorie": 350,
    	"quantity": 350,
        "fat": 30,
    	"size": "杯"
    }
```

# API

## 飲食目標

### 新增/編輯飲食目標

Request:

    POST /diet/goal

Header:

```json=
    {
       "Authorization": "Bearer Owicare::<token>"
    }
```

Payload:

```json=
    {
        "height": 170,
        "weight": 65
    }
```

Response (成功):

```json=
    {
    	"success": true,
    	"data": {
    		"height":  170,
    		"weight": 65,
    		"calorie": 1883,
    		"protein": 78,
    		"dietaryFiber": 117,
    		"carbohydrate": 56
    	}
    }
```

### 取得飲食目標

Request:

    GET /user/$uid/diet/goal

Header:

```json=
    {
       "Authorization": "Bearer Owicare::<token>"
    }
```

Response (成功):

```json=
    {
    	"success": true,
    	"data": {
    	    "height":  170,
    	    "weight": 65,
    	    "calorie": 1883,
    	    "protein": 78,
    	    "dietaryFiber": 117,
    	    "carbohydrate": 56,
            "fat": 30
    	}
    }
```

## 飲食紀錄

### 新增飲食紀錄

Request:

    POST /diet/diary/bulkAdd

Header:

```json=
    {
       "Authorization": "Bearer Owicare::<token>"
    }
```

Payload:

```json= 
    {
        "patientId": 23, // 若不傳入 patientId，則會由 token 取得個案資料
        "image": 'XXXXXXXXXXXX', // base64 編碼
        "imageType": 'jpg',
        "dietDiaryList": [ 
            {
                "time": 1585546156101,
                "timePeriod": 1,
                "timePeriodType": 11,
                "foodId": 232,            
                "quantity": 2
            }, 
            {    
                "time": 1585546165445,
                "timePeriod": 2,
                "timePeriodType": 20,
                "foodId": 232,            
                "quantity": 1
            }
        ]
           
    }
```

Response (成功):

* deprecated
```json=
    {
    	"success": true,
    	"data": [
               DietDiary,
               DietDiary,
               ...
        ]        
    }
```

* Updated for now as below
```json=
{
    "httpCode": 200,
    "result": {
        "data": [
            {
                "image": "diet/diary/90862aa4-d4e1-412b-aa67-3d5cb2a4e1c0.png",
                "imageType": "png",
                "householdMemberId": "123",
                "time": 1585546156101,
                "hhmmOfTime": 1126,
                "timePeriod": 2,
                "timePeriodType": 21,
                "foodId": 0,
                "foodName": "test food",
                "timeAndTimePeriodType": 1607961600021,
                "quantity": 2,
                "calorie": 0,
                "protein": 0,
                "dietaryFiber": 0,
                "carbohydrate": 0,
                "fat": 0,
                "createdAt": 1608002816917
            }
        ]
    }
}
```

### 依飲食紀錄餐別整批更新
依照PayLoad陣列裡面的timePeriod來批次刪除並批次新增
Request:

    POST /diet/diary/bulkUpdate
    
Header:

```json=
    {
       "Authorization": "Bearer Owicare::<token>"
    }
```

Payload:

```json=
    {
        "patientId": 6,  // 若不傳入 patientId，則會由 token 取得個案資料
        "date": "2020/02/03",
        "timePeriod": 1,
        "timePeriodType": 21,
        "image": 'XXXX', //base64 
        "imageType": 'jpg'
        "dietDiaryList": [ 
            {
                "time": 1585546156101,
                "foodId": 232,            
                "quantity": 2
            }, 
            {    
                "time": 1585546156101,
                "foodId": 112,            
                "quantity": 1
            }
        ]
    }
```

Response (成功):

```json=
    {
    	"success": true,
    	"data": [
            {            
    	        "id": 1,
                "image": "http://example.com/foodimage1.jpg",
                "time": 1585546156101,
    	        "patientId": 6,
                "timePeriod": 2,
    	        "timePeriodType": 21,
                "foodId": 232,
                "quantity": 2  
            },
            {            
    	        "id": 2,
                "image": "http://example.com/foodimage2.jpg",
                "time": 1585546156101,
    	        "patientId": 6,
                "timePeriod": 2,
    	        "timePeriodType": 21,
                "foodId": 112,
                "quantity": 1 
            }
    	]
    }
```

### 編輯飲食紀錄

Request:

    PUT /diet/diary/:id

Header:

```json=
    {
       "Authorization": "Bearer Owicare::<token>"
    }
```

Payload:

```json=
    {       
        "image": "http://example.com/image.jpg",
        "time": "1585546156101",
    	"patientId": 6,
        "timePeriod": "2",
    	"timePeriodType": "21 ",
        "foodId": 12,
        "quantity": 2        
    }
```

Response (成功):

```json=
    {
    	"success": true,
    	"data": {
    	    "id": 1,
            "image": "http://example.com/image.jpg",
            "time": "1585546156101",
    	    "patientId": 6,
            "timePeriod": "2",
    	    "timePeriodType": "21 ",
            "foodId": 12,
            "quantity": 2  
    	}
    }
```

### 查詢飲食紀錄

Request:

    GET /diet/diary/$id

Header:

```json=
    {
       "Authorization": "Bearer Owicare::<token>"
    }
```

Response (成功):

```json=
    {
    		"success": true,
    		"data": {
                DietDiary
            }
    }
```

### 查詢飲食紀錄列表

Request:

    GET /diet/diary?$queryParams  (WEB/APP 共用)

[Query Params:]

| 欄位 | 型態 | 預設 | 說明 |
| -------- | -------- | -------- |-------- |
| page     | number     | 1     | 當前頁數 |
| pageSize | number     | -1    | 每頁結果數量，-1 為顯示所有 |
| sort    | string     |  無    | 排序欄位，"timeAndTimePeriodType"：這個可以先依 time 排出每日的飲食紀錄，再依 timePeriodType 來排序 |
| desc    | boolean    | false |  是否倒序排列 ? |
| startedAt | timestamp | 無     | 要搜尋的日期區間 |
| endAt    | timestamp | 無    | 要搜尋的日期區間|
| patientId    | number |  無    | 要搜尋的 patient id，若沒有傳入 patient id，就以改由 token 取得個案資料 |


Header:

```json=
    {
       "Authorization": "Bearer Owicare::<token>"
    }
```

Response (成功):

```json=
    {
    	"success": true,
    	"total": 2,
    	"data": [
    		    { 
                                id:2,
                                time: 1587447592675,                               
                                patientId: 6,
                                image: "/images/foodimage1.jpg",
                                timePeriod: 1,
                                timePeriodType: 20,
                                quantity: 2,
                                foodName: '土司',
                                calorie: 283,
                                protein: 9.5,
                                dietaryFiber: 3,
                                carbohydrate: 48.6,
                                fat: 30
                    }, {
    		                id:3,
                                time: 1587447592675,                           
                                patientId: 6,
                                image: "/images/foodimage2.jpg",
                                timePeriod: 1,
                                timePeriodType: 20,
                                quantity: 1,
                                foodName: '豆漿',
                                calorie: 283,
                                protein: 9.5,
                                dietaryFiber: 3,
                                carbohydrate: 48.6,
                                fat: 33
                    }
        ]
    }

```

### 查詢特定時段的飲食紀錄列表

Request:

    GET /diet/Diary/timeframe?$queryParams

[Query Params:]

| 欄位 | 型態 | 預設 | 說明 |
| -------- | -------- | -------- |-------- |
| page     | number     | 1     | 當前頁數 |
| pageSize | number     | -1    | 每頁結果數量，-1 為顯示所有 |
| sort    | string     |  無    | 排序欄位|
| desc    | boolean    | false |  是否倒序排列 ? |
| patientId    | number |  無    | 要搜尋的 patient id  |
| startDate    | string | 無    | 要搜尋的日期區間，範例："2020/02/19"|
| endDate    | string | 無    | 要搜尋的日期區間，範例："2020/02/19"|
| startTime    | string | 無    | 要搜尋的時間區間，範例："12:02"|
| endTime    | string | 無    | 要搜尋的時間區間，範例："12:02"|
| timePeriod   | number | -1  | 飲食紀錄時段 enum: [-1: 全部, 1: "早上",  2: "中午", 3: "晚上", 4: "睡前"] |
| timePeriodType  | number | -1   | 飲食紀錄餐別 enum: [-1: 全部, 10: "早餐", 11: "早午餐",  20: "午餐", 21: "下午茶",  30: "晚餐",  31: "點心", 40: "宵夜"] |


Header:

```json=
    {
       "Authorization": "Bearer Owicare::<token>"
    }
```

Response (成功):

```json=
    {
    	"success": true,
    	"total": 2,
    	"data": [
    		    { 
                                id:2,
                                time: 1587447592675,                                
                                patientId: 6,
                                image: "/images/foodimage1.jpg",
                                timePeriod: 1,
                                timePeriodType: 20,
                                quantity: 2,
                                foodName: '土司',
                                calorie: 283,
                                protein: 9.5,
                                dietaryFiber: 3,
                                carbohydrate: 48.6,
                                fat: 28
                    }, {
    		                id:3,
                                time: 1587447592675,                               
                                patientId: 6,
                                image: "/images/foodimage2.jpg",
                                timePeriod: 1,
                                timePeriodType: 20,
                                quantity: 1,
                                foodName: '豆漿',
                                calorie: 182,
                                protein: 19.6,
                                dietaryFiber: 6,
                                carbohydrate: 20.6,
                                fat: 21
                    }
        ]
    }

```


### 刪除飲食紀錄

Request:

    DELETE /diet/diary/$id

Header:

```json=
    {
       "Authorization": "Bearer Owicare::<token>"
    }
```

Response (成功):

```json=
    {
    		"success": true,
    		"data": {}
    }
```

## 食品

### 新增食品

Request:

    POST /diet/food

Header:

```json=
    {
       "Authorization": "Bearer Owicare::<token>"
    }
```

Payload:

```json=
    {
       Food
    }
```

Response (成功):

```json=
    {
    		"success": true,
    		"data": {
    						Food
    		}
    }
```

### 編輯食品

Request:

    PUT /diet/food/$id

Header:

```json=
    {
       "Authorization": "Bearer Owicare::<token>"
    }
```

Payload:

```json=
    {
       Food
    }
```

Response (成功):

```json=
    {
    		"success": true,
    		"data": {
    						Food
    		}
    }
```

### 查詢食品列表

Request:

    GET /diet/food

Header:

```json=
    {
       "Authorization": "Bearer Owicare::<token>"
    }
```

Response (成功):

```json=
    {
    		"success": true,
    	  "total": 5,
    		"data": [
    						Food,
    						Food,
             ...
    		]
    }
```

### 查詢食品

Request:

    GET /diet/food/$id

Header:

```json=
    {
       "Authorization": "Bearer Owicare::<token>"
    }
```

Response (成功):

```json=
    {
    		"success": true,
    		"data": {
            Food
        }
    }
```

### 刪除食品

Request:

    DELETE /diet/food/$id

Header:

```json=
    {
       "Authorization": "Bearer Owicare::<token>"
    }
```

Response (成功):

```json=
    {
    		"success": true,
    		"data": {}
    }
```