# Assignment 1 Model Answer **Repository Path**: DQH14/assignment-1-model-answer ## Basic Information - **Project Name**: Assignment 1 Model Answer - **Description**: No description available - **Primary Language**: JavaScript - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 5 - **Created**: 2022-05-04 - **Last Updated**: 2022-05-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Web应用开发与实践 Assignment 1 Model Answer ## 初始化 项目运行前,请确保所有依赖已经安装至本地,在终端或者CMD中执行: ```bash npm i ``` ## API说明 ### 字符编码 HTTP通讯及报文BASE64编码均采用UTF-8字符集编码格式。 ### 创建新用户 #### Reuqest - Method: **POST** - URL: ```/api/users``` - Headers: Content-Type:application/json - Body: ```json { "phone" : "13511112222", "nickName" : "Peking Duck Wong", "password" : "Wangkaoya1?" } ``` #### Response ##### 正确返回 - HTTP status code: 201 - Body ```json { "user": { "id": 3, "nickname": "Peking Duck Wong", "phoneNumber": "13511112222", "isAdmin": 0, "createDateTime": "2022-04-22 12:32:59" }, "message": "new user has created" } ``` ##### 错误返回 - HTTP status code: 400 - Body ```json { "message": "Phone Number is illegal" } ``` - HTTP status code: 400 - Body ```json { "message": "Phone Number already been used." } ``` - HTTP status code: 400 - Body ```json { "message": "Password must at least 8 characters, at least 1 special character,1 number and 1 upper case character" } ``` - HTTP status code: 400 - Body ```json { "message": ""nickname" is required" } ``` etc. ### 用户登录 #### Reuqest - Method: **POST** - URL: ```/api/auth``` - Headers: Content-Type:application/json - Body: ```json { "phone" : "13511112222", "password" : "Wangkaoya1?" } ``` #### Response ##### 正确返回 - HTTP status code: 201 - Body ```json { "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaXNBZG1pbiI6MSwiaWF0IjoxNjUwNjAyMzg1fQ.882K_7SeRoUB26j_WErXtnYtFd8DR7le9qxnlRVR3g0", "message": "login seccessful" } ``` ##### 错误返回 - HTTP status code: 400 - Body ```json { "message": "Phone Number is illegal" } ``` - HTTP status code: 400 - Body ```json { "message": "Invaild Password" } ``` etc. ### 删除用户 #### Reuqest - Method: **DELECT** - URL: ```/api/users/[user id]``` - Headers: ``` x-auth-token:[jwt token here WITH ADMIN ROLE] ``` #### Response ##### 正确返回 - HTTP status code: 200 - Body ```json { "user": { "id": 2, "nickname": "123", "phoneNumber": "13450000012", "isDisabled": 1, "isAdmin": 1, "createDateTime": "2022/4/18 11:09:33" }, "message": "user deleted" } ``` ##### 错误返回 - HTTP status code: 404 - Body ```json { "message": "cannot find the comment with the id" } ``` - HTTP status code: 400 - Body ```json { "message": "cannot delete yourself" } ``` etc. ### 车辆列表 #### Reuqest - Method: **GET** - URL: ```/api/cars``` - Queries: ``` kw: 搜索关键字 page: 页数 (1、2、3、4...) orderby: 排列 (cmt:评价时间、carcdt:车辆创建时间、rate:评价) order: 顺序(asc:正序、desc:反序) ``` #### Response ##### 正确返回 - HTTP status code: 200 - Body ```json { "cars": [ { "rego": "晋A12N75", "color": "red", "model": "sedan", "carCDT": "2022-04-19 12:13:11", "userId": 1, "userName": "123", "maxCmtCDT": "2022-04-19 19:50:48", "minCmtCDT": "2022-04-19 00:40:45", "avgRate": 0.2857142857142857, "commentCount": 7 }, { "rego": "晋A88888", "color": "red", "model": "sedan", "carCDT": "2022-04-19 12:13:25", "userId": null, "userName": null, "maxCmtCDT": null, "minCmtCDT": null, "avgRate": null, "commentCount": 0 } ], } ``` ### 增加车辆 #### Reuqest - Method: **POST** - URL: ```/api/cars``` - Headers: ``` Content-Type:application/json x-auth-token:[jwt token here] ``` - Body: ```json { "rego": "晋A18S30", "model":"SUV", "color":"red" } ``` #### Response ##### 正确返回 - HTTP status code: 201 - Body ```json { "car": { "rego": "晋A18S30", "color": "red", "model": "SUV", "isDisabled": 0, "createDateTime": "2022-04-22 22:11:09" }, "message": "new car has created" } ``` ##### 错误返回 - HTTP status code: 400 - Body ```json { "message": "already has this rego" } ``` etc. ### 查看车辆 #### Reuqest - Method: **GET** - URL: ```/api/cars/[car's number]``` #### Response ##### 正确返回 - HTTP status code: 200 - Body ```json { "car": { "rego": "晋A18S30", "color": "red", "model": "SUV", "createDateTime": "2022-04-22 22:11:09", "avgRate": null, "commentCount": 0 }, } ``` ##### 错误返回 - HTTP status code: 404 - Body ```json { "message": "cannot find the car with the rego" } ``` etc. ### 修改车辆信息 #### Reuqest - Method: **PUT** - URL: ```/api/cars/[car's number]``` - Headers: ``` Content-Type:application/json x-auth-token:[jwt token here WITH ADMIN ROLE] ``` - Body: ```json { "rego": "晋A18S30", "model":"SUV", "color":"red" } ``` #### Response ##### 正确返回 - HTTP status code: 200 - Body ```json { "car": { "rego": "晋A18S30", "color": "sedan", "model": "black", "isDisabled": 0, "createDateTime": "2022-04-22 22:22:18" }, "message": "update selected car successful" } ``` ##### 错误返回 - HTTP status code: 404 - Body ```json { "message": "cannot find the car with the rego" } ``` etc. ### 删除车辆 #### Reuqest - Method: **DELECT** - URL: ```/api/cars/[car's number]``` - Headers: ``` x-auth-token:[jwt token here WITH ADMIN ROLE] ``` #### Response ##### 正确返回 - HTTP status code: 200 - Body ```json { "car": { "rego": "晋A18S30", "color": "sedan", "model": "black", "isDisabled": 1, "createDateTime": "2022-04-22 22:22:18" }, "message": "sccessfully deteled the car" } ``` ##### 错误返回 - HTTP status code: 404 - Body ```json { "message": "cannot find the car with the rego" } ``` etc. ### 评价列表 #### Reuqest - Method: **GET** - URL: ```/api/cars/[car's number]/comments``` - Queries: ``` page: 页数 (1、2、3、4...) rate: 排列 (0:负面评价、1:正面评价) ``` #### Response ##### 正确返回 - HTTP status code: 200 - Body ```json { "comments": [ { "id": 9, "rate": 0, "content": "very bad behavour", "location": "Taiyuan", "cmtCDT": "2022-04-19 19:47:27", "username": "123" }, { "id": 6, "rate": 1, "content": "good", "location": "Taiyuan", "cmtCDT": "2022-04-19 12:34:55", "username": "123" }, { "id": 8, "rate": 0, "content": "bad behavour", "location": "Taiyuan", "cmtCDT": "2022-04-19 00:41:33", "username": "123" }, { "id": 7, "rate": 1, "content": "very good", "location": "Taiyuan", "cmtCDT": "2022-04-19 00:40:45", "username": "123" } ], } ``` ##### 错误返回 - HTTP status code: 404 - Body ```json { "message": "cannot find the car with the rego" } ``` etc. ### 增加评论 #### Reuqest - Method: **POST** - URL: ```/api/cars/[car's number]/comments``` - Headers: ``` Content-Type:application/json x-auth-token:[jwt token here] ``` - Body: ```json { "content":"very good", "rate":1, "location":"Taiyuan" } ``` #### Response ##### 正确返回 - HTTP status code: 201 - Body ```json { "comment": { "id": 13, "userId": 1, "rego": "晋A12N75", "content": "very good", "rate": 1, "location": "Taiyuan", "isDisabled": 0, "createDateTime": "2022-04-22 22:40:10" }, "message": "new comment has posted" } ``` ##### 错误返回 - HTTP status code: 404 - Body ```json { "message": "cannot find the car with the rego" } ``` etc. ### 获取评论列表 #### Reuqest - Method: **GET** - URL: ```/api/comments``` - Queries: ``` page: 页数 (1、2、3、4...) rate: 排列 (0:负面评价、1:正面评价) ``` #### Response ##### 正确返回 - HTTP status code: 201 - Body ```json { "comments": [ { "id": 6, "rate": 1, "content": "good", "location": "Taiyuan", "cmtCDT": "2022-04-19 12:34:55", "username": "123" }, { "id": 8, "rate": 0, "content": "bad behavour", "location": "Taiyuan", "cmtCDT": "2022-04-19 00:41:33", "username": "123" }, { "id": 7, "rate": 1, "content": "very good", "location": "Taiyuan", "cmtCDT": "2022-04-19 00:40:45", "username": "123" } ], } ``` ### 删除评价 #### Reuqest - Method: **DELECT** - URL: ```/api/comments/[comment id]``` - Headers: ``` x-auth-token:[jwt token here WITH ADMIN ROLE] ``` #### Response ##### 正确返回 - HTTP status code: 200 - Body ```json { "comment": { "id": 6, "userId": 1, "rego": "晋A12N75", "content": "good", "rate": 1, "location": "Taiyuan", "isDisabled": 1, "createDateTime": "2022-04-19 12:34:55" }, "message": "comment deleted" } ``` ##### 错误返回 - HTTP status code: 404 - Body ```json { "message": "cannot find the comment with the id" } ``` etc.