# `POST` /api/followships ## API 功能 建立追蹤關係紀錄。 * 只能追蹤自己以外的 user。 ## 邏輯分析(後端看的) :::spoiler 運作邏輯 1. 取得 followerId 及 followingId。 2. 檢查 followerId 及 followingId 不可相同。 3. 檢查是否已追蹤。 4. 新建追蹤關係。 ::: :::spoiler [測試檔(pass)](https://github.com/ritachien/twitter-api-2022/blob/main/test/requests/followship.spec.js#L37-L55) ::: :::spoiler 人工測試(6/6) - [x] 前端提供資料欄位缺失則回傳錯誤訊息。 - [x] 如果 account/name 和其他 user 相同則回傳錯誤訊息。 - [x] 註冊完成回傳 user 資料。 - [x] 回傳資料不可以包含 user 密碼。 - [x] name 超過 50 字回傳錯誤訊息。 ::: ## 前端傳入資料 ### parameters N/A ### req.body ```json { "name": "user1", "account": "user1", "email": "user1@example.com", "password": "12345678", "passwordCheck": "12345678" } ``` ## 後端回傳資料 ### 註冊成功 ```javascript // status code: 200 { "status": "success", "message": "Sign up success.", "data": { "user": { "id": 1, "name": "user1", "account": "user1", "role": "user" } } } ``` ### 傳入資料格式有誤 發生原因: 檢查後發現任一欄位為空或短少欄位。 ```json // status code: 400 { "status": "error", "message": "All fields are required." } ``` 發生原因: `name` 欄位超過 50 字元。 ```json // status code: 400 { "status": "error", "Field 'name' should be limited within 50 characters." } ``` ### 驗證失敗 發生原因: 使用一般使用者帳號登入或密碼錯誤。 ```javascript // status code: 401 { "status": "error", "message": "Account or password incorrect." } ``` ## 相關連結 * [回首頁](https://hackmd.io/@twitter-2022/index) * [API 總表](/Gl56cI2LQ5ObBpmQnbnphw) ###### tags: `API-doc`