檢查用戶名和郵箱是否被佔用
This commit is contained in:
		
							
								
								
									
										12
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								README.md
									
									
									
									
									
								
							@@ -14,7 +14,7 @@ type User struct {
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
- [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}) 刪除標籤
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user