78 lines
1.8 KiB
Go
78 lines
1.8 KiB
Go
package bd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"git.ukamnya.ru/stulyaganov/RestApiv2/internal/bd/model"
|
|
)
|
|
|
|
type Userrepo struct {
|
|
db Bd
|
|
}
|
|
|
|
func (u *Userrepo) Create(user *model.User) error {
|
|
sql := fmt.Sprint("INSERT INTO users (login, email, password, permisionlvl) VALUES($1, $2, $3, $4)")
|
|
_, err := u.db.db.Exec(context.Background(), sql, user.Login, user.Email, user.Password, user.PermisionLVL)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (u *Userrepo) FindById(id string) (*model.User, error) {
|
|
var user model.User
|
|
query := fmt.Sprintf("SELECT * FROM users WHERE Id = %s;", id)
|
|
err := u.db.db.QueryRow(context.Background(), query).
|
|
Scan(user.Id, user.Login, user.Email, user.Password, user.Avatar_Url, user.PermisionLVL)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (u *Userrepo) FindByLogin(login string) (*model.User, error) {
|
|
var user model.User
|
|
rows, err := u.db.db.Query(context.Background(), "SELECT login FROM users WHERE login = $1", login)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
err = rows.Scan(&user.Login)
|
|
}
|
|
return &user, nil
|
|
|
|
}
|
|
|
|
func (u *Userrepo) FindByEmail(email string) (*model.User, error) {
|
|
var user model.User
|
|
rows, err := u.db.db.Query(context.Background(), "SELECT email FROM users WHERE email = $1", email)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
err = rows.Scan(&user.Email)
|
|
}
|
|
return &user, nil
|
|
|
|
}
|
|
|
|
func (u *Userrepo) FindByLoginPas(login string) (*model.User, error) {
|
|
var user model.User
|
|
rows, err := u.db.db.Query(context.Background(), "SELECT login, password, permisionlvl, email FROM users WHERE login = $1", login)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for rows.Next() {
|
|
err := rows.Scan(&user.Login, &user.Password, &user.PermisionLVL, &user.Email)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return &user, nil
|
|
}
|