From 88ae5fa1880ebbe7e3a0e9fbf284381248ff90cb Mon Sep 17 00:00:00 2001 From: Shuhrat Tultaganov Date: Sun, 15 Jan 2023 22:03:16 +0300 Subject: [PATCH] Add creat user in database --- internal/bd/userrepo.go | 4 ++-- internal/restserver/userHendle.go | 23 +++++++++++++++------- migrates/users/20230106180150_users.up.sql | 14 ++++--------- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/internal/bd/userrepo.go b/internal/bd/userrepo.go index 0083aa8..3e3fd6b 100644 --- a/internal/bd/userrepo.go +++ b/internal/bd/userrepo.go @@ -13,8 +13,8 @@ type Userrepo struct { func (u *Userrepo) Create(user *model.User) (*model.User, error) { err := u.db.db.QueryRow(context.Background(), - "INSERT INTO users (id, login, email, password, avatar_url, token, permissionLVL) VALUES($1, $2, $3, $4, $5, $6, $7)"). - Scan(user.Id, user.Login, user.Email, user.Password, user.Avatar_Url, user.Token, user.PermisionLVL) + "INSERT INTO users (id, login, email, password, avatar_url, token, permisionlvl) VALUES($1, $2, $3, $4, $5, $6, $7) RETURNING id, permisionlvl", user.Id, user.Login, user.Email, user.Password, user.Avatar_Url, user.Token, user.PermisionLVL). + Scan(&user.Id, &user.PermisionLVL) if err != nil { return nil, err } diff --git a/internal/restserver/userHendle.go b/internal/restserver/userHendle.go index b07fe15..45a16b1 100644 --- a/internal/restserver/userHendle.go +++ b/internal/restserver/userHendle.go @@ -1,8 +1,9 @@ package restserver import ( - "fmt" "net/http" + + "git.ukamnya.ru/stulyaganov/RestApi/internal/bd/model" ) func (r *RestServer) configureRouterUser() { @@ -11,17 +12,25 @@ func (r *RestServer) configureRouterUser() { func (r *RestServer) HandleFuncRegUser() http.HandlerFunc { return func(w http.ResponseWriter, res *http.Request) { + user := model.User{} err := res.ParseForm() if err != nil { r.logger.Error(err) } + user.Id = 1 + user.Login = res.Form.Get("user") + user.Password = res.Form.Get("password") + user.Email = res.Form.Get("email") + user.PermisionLVL = 1 + user.Token = "dasf2!23sfafaQWerq" - user := res.Form.Get("user") - password := res.Form.Get("password") - mail := res.Form.Get("email") - borndata := res.Form.Get("born") - - fmt.Println(user, password, mail, borndata) + RetUser, err := r.db.User().Create(&user) + if err != nil { + r.logger.Errorln(err) + } + if RetUser != nil { + r.logger.Infoln("Create new user with id = ", RetUser.Id, " and with permissionLvl = ", RetUser.Id) + } } } diff --git a/migrates/users/20230106180150_users.up.sql b/migrates/users/20230106180150_users.up.sql index ecc4fd0..7d2c9b1 100644 --- a/migrates/users/20230106180150_users.up.sql +++ b/migrates/users/20230106180150_users.up.sql @@ -2,15 +2,9 @@ CREATE TABLE users ( id INTEGER NOT NULL, login VARCHAR(25) not NULL, email VARCHAR(255) NOT NULL, - password VARCHAR() NOT NULL, - avatar_url VARCHAR(), - token VARCHAR() NOT NULL, - permisionLVL INTEGER NOT NULL - - - + password VARCHAR(2500) NOT NULL, + avatar_url VARCHAR(2500), + token VARCHAR(2500) NOT NULL, + permisionLVL INTEGER NOT NULL, PRIMARY KEY(id) - PRIMARY KEY(email) - PRIMARY KEY(login) - PRIMARY KEY(token) ); \ No newline at end of file