RestApiv2/internal/bd/userrepo.go

66 lines
1.5 KiB
Go
Raw Normal View History

package bd
import (
"context"
"fmt"
2023-03-31 20:08:38 +03:00
"git.ukamnya.ru/stulyaganov/RestApiv2/internal/bd/model"
)
type Userrepo struct {
db Bd
}
2023-04-14 17:35:25 +03:00
func (u *Userrepo) Create(user *model.User) error {
_, err := u.db.db.Exec(context.Background(),
"INSERT INTO users (login, email, password, permisionlvl) VALUES($1, $2, $3, $4)", user.Login, user.Email, user.Password, user.PermisionLVL)
if err != nil {
2023-04-14 17:35:25 +03:00
return err
}
2023-04-14 17:35:25 +03:00
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).
2023-04-14 17:35:25 +03:00
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
}
2023-04-14 17:35:25 +03:00
defer rows.Close()
for rows.Next() {
err = rows.Scan(&user.Email)
}
return &user, nil
2023-04-14 17:35:25 +03:00
}
func (u *Userrepo) FindByAll() (*model.User, error) {
return nil, nil
}