From afec36dffced4f3f897182b0d660f27f709a79b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A1=9C=E8=8F=AF?= Date: Sat, 27 May 2023 09:04:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=AA=A2=E6=9F=A5=E7=94=A8=E6=88=B6=E5=90=8D?= =?UTF-8?q?=E5=92=8C=E9=83=B5=E7=AE=B1=E6=98=AF=E5=90=A6=E8=A2=AB=E4=BD=94?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 14 +++++++------- routers/users.go | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 222bb8e..25495e5 100644 --- a/README.md +++ b/README.md @@ -7,14 +7,14 @@ type User struct { ID int `json:"id"` Name string `json:"name"` Email string `json:"email"` - Password string `json:"password"` + Password string `json:"password"` CreatedAt string `json:"created_at"` UpdatedAt string `json:"updated_at"` } ``` - [x] GET [/api/users](/api/users) 用戶列表 -- [x] POST [/api/users](/api/users) 創建用戶 +- [x] POST [/api/users](/api/users) 創建用戶 `{email:'', password:'', name:''}` - [x] GET [/api/users](/api/users) 查詢指定用戶 - [x] PATCH [/api/users/{id}](/api/users/{id}) 修改指定用戶 - [x] DELETE [/api/users/{id}](/api/users/{id}) 刪除指定用戶 @@ -24,13 +24,13 @@ type User struct { ```go type Session struct { - Email string `json:"email"` - Password string `json:"password"` + ID string `json:"ID"` + UserID int `json:"user_id"` } ``` - [x] GET [/api/sessions](/api/sessions) 會話列表 -- [x] POST [/api/sessions](/api/sessions) 登錄賬戶(創建會話) +- [x] POST [/api/sessions](/api/sessions) 登錄賬戶(創建會話) `{email:'', password:''}` - [x] GET [/api/sessions/{id}](/api/sessions/{id}) 查詢會話詳情 - [x] PATCH [/api/sessions/{id}](/api/sessions/{id}) 修改會話狀態 - [x] DELETE [/api/sessions/{id}](/api/sessions/{id}) 註銷登錄(刪除指定會話) @@ -45,7 +45,7 @@ type Dataset struct { ``` - [x] GET [/api/datasets](/api/datasets) 數據集列表 -- [x] POST [/api/datasets](/api/datasets) 創建數據集 +- [x] POST [/api/datasets](/api/datasets) 創建數據集 `{images:['https://oss..']}` - [x] GET [/api/datasets/{id}](/api/datasets/{id}) 查詢指定數據集 - [x] PATCH [/api/datasets/{id}](/api/datasets/{id}) 修改指定數據集 - [x] DELETE [/api/datasets/{id}](/api/datasets/{id}) 刪除指定數據集 @@ -63,7 +63,7 @@ type Tag struct { ``` - [x] GET [/api/tags](/api/tags) 標籤列表(全部) -- [x] POST [/api/tags](/api/tags) 創建標籤 +- [x] POST [/api/tags](/api/tags) 創建標籤 `{name:''}` - [x] GET [/api/tags/{id}](/api/tags/{id}) 標籤詳情 - [x] PATCH [/api/tags/{id}](/api/tags/{id}) 修改標籤 - [x] DELETE [/api/tags/{id}](/api/tags/{id}) 刪除標籤 diff --git a/routers/users.go b/routers/users.go index 6af5183..e10025f 100644 --- a/routers/users.go +++ b/routers/users.go @@ -44,6 +44,21 @@ func UsersPost(w http.ResponseWriter, r *http.Request) { Password: fmt.Sprintf("%x", md5.Sum([]byte(form["password"].(string)+slat))), Slat: slat, } + // 檢查郵箱是否已經存在, 郵箱不能重複 + var count int64 + configs.ORMDB().Model(&models.User{}).Where("email = ?", user.Email).Count(&count) + if count > 0 { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("400 - email already exists")) + return + } + // 檢查用戶名是否已經存在, 用戶名不能重複 + configs.ORMDB().Model(&models.User{}).Where("name = ?", user.Name).Count(&count) + if count > 0 { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("400 - name already exists")) + return + } // 寫入數據庫 if err := configs.ORMDB().Create(&user).Error; err != nil { w.WriteHeader(http.StatusBadRequest)