Skip to content

Commit

Permalink
Add user unique check
Browse files Browse the repository at this point in the history
  • Loading branch information
NHAS committed Apr 27, 2024
1 parent 9613410 commit 071925b
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion internal/data/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/client/v3/clientv3util"
)

type UserModel struct {
Expand Down Expand Up @@ -325,7 +326,18 @@ func CreateUserDataAccount(username string) (UserModel, error) {
}
b, _ := json.Marshal(&newUser)

_, err := etcd.Put(context.Background(), "users-"+username+"-", string(b))
txn := etcd.Txn(context.Background())
txn.If(clientv3util.KeyMissing("users-" + username + "-"))
txn.Then(clientv3.OpPut("users-"+username+"-", string(b)))

res, err := txn.Commit()
if err != nil {
return UserModel{}, err
}

if !res.Succeeded {
return UserModel{}, errors.New("user exists")
}

return newUser, err
}
Expand Down

0 comments on commit 071925b

Please sign in to comment.