116 lines
3.0 KiB
Go
116 lines
3.0 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) AddSaveFilmsItem(userLogin string, films int) error {
|
|
sql := fmt.Sprint("UPDATE users SET SaveItemFilms = array_append(SaveItemFilms, $1) WHERE login = $2")
|
|
_, err := u.db.db.Exec(context.Background(), sql, films, userLogin)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (u *Userrepo) AddSaveSiriesItem(userLogin string, siries int) error {
|
|
sql := fmt.Sprint("UPDATE users SET SaveItemSiries = array_append(SaveItemSiries, $1) WHERE login = $2")
|
|
_, err := u.db.db.Exec(context.Background(), sql, siries, userLogin)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (u *Userrepo) GetSaveFilmsItem(userLogin string) ([]int, error) {
|
|
var films []int
|
|
sql := fmt.Sprint("SELECT SaveItemFilms FROM users WHERE login = $1")
|
|
err := u.db.db.QueryRow(context.Background(), sql, userLogin).Scan(&films)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return films, err
|
|
}
|
|
|
|
func (u *Userrepo) GetSaveSiriesItem(userLogin string) ([]int, error) {
|
|
var siries []int
|
|
sql := fmt.Sprint("SELECT SaveItemSiries FROM users WHERE login = $1")
|
|
err := u.db.db.QueryRow(context.Background(), sql, userLogin).Scan(&siries)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return siries, err
|
|
}
|
|
|
|
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
|
|
}
|