119 lines
2.8 KiB
Go
119 lines
2.8 KiB
Go
package sqlite
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"sso/internal/domain/models"
|
|
"sso/internal/storage"
|
|
|
|
"github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
type Storage struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
func New(storagePath string) (*Storage, error) {
|
|
const op = "storage.sqlite.New"
|
|
|
|
db, err := sql.Open("sqlite3", storagePath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
|
|
return &Storage{db: db}, nil
|
|
}
|
|
|
|
func (s *Storage) SaveUser(ctx context.Context, email string, passHash []byte) (int64, error) {
|
|
const op = "storage.sqlite.SaveUser"
|
|
|
|
stmt, err := s.db.Prepare("insert into users(email, pass_hash) values (?, ?)")
|
|
if err != nil {
|
|
return 0, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
|
|
res, err := stmt.ExecContext(ctx, email, passHash)
|
|
if err != nil {
|
|
var sqliteErr sqlite3.Error
|
|
if errors.As(err, &sqliteErr) && sqliteErr.ExtendedCode == sqlite3.ErrConstraintUnique {
|
|
return 0, fmt.Errorf("%s: %w", op, storage.ErrUserExists)
|
|
}
|
|
}
|
|
|
|
id, err := res.LastInsertId()
|
|
if err != nil {
|
|
return 0, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
|
|
return id, nil
|
|
}
|
|
|
|
// User returns user by email.
|
|
func (s *Storage) User(ctx context.Context, email string) (models.User, error) {
|
|
const op = "storage.sqlite.User"
|
|
|
|
stmt, err := s.db.Prepare("select id, email, pass_hash from users where email = ?")
|
|
if err != nil {
|
|
return models.User{}, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
|
|
var user models.User
|
|
err = stmt.QueryRowContext(ctx, email).Scan(&user.ID, &user.Email, &user.PassHash)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return models.User{}, fmt.Errorf("%s: %w", op, storage.ErrUserNotFound)
|
|
}
|
|
|
|
return models.User{}, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
|
|
return user, nil
|
|
}
|
|
|
|
func (s *Storage) IsAdmin(ctx context.Context, userID int64) (bool, error) {
|
|
const op = "storage.sqlite.IsAdmin"
|
|
|
|
stmt, err := s.db.Prepare("select is_admin from users where id = ?")
|
|
if err != nil {
|
|
return false, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
|
|
var is_admin bool
|
|
err = stmt.QueryRowContext(ctx, userID).Scan(&is_admin)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return false, fmt.Errorf("%s: %w", op, storage.ErrAppNotFound)
|
|
}
|
|
|
|
return false, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
|
|
return is_admin, nil
|
|
}
|
|
|
|
// App returns app by id.
|
|
func (s *Storage) App(ctx context.Context, id int) (models.App, error) {
|
|
const op = "storage.sqlite.App"
|
|
|
|
stmt, err := s.db.Prepare("SELECT id, name, secret FROM apps WHERE id = ?")
|
|
if err != nil {
|
|
return models.App{}, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
|
|
row := stmt.QueryRowContext(ctx, id)
|
|
|
|
var app models.App
|
|
err = row.Scan(&app.ID, &app.Name, &app.Secret)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return models.App{}, fmt.Errorf("%s: %w", op, storage.ErrAppNotFound)
|
|
}
|
|
|
|
return models.App{}, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
|
|
return app, nil
|
|
}
|