57 lines
1 KiB
Go
57 lines
1 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
|
|
"go-backend/internal/database/sqlc"
|
|
)
|
|
|
|
type UserService struct {
|
|
db *sql.DB
|
|
queries *sqlc.Queries
|
|
}
|
|
|
|
func NewUserService(db *sql.DB, queries *sqlc.Queries) *UserService {
|
|
return &UserService{
|
|
db: db,
|
|
queries: queries,
|
|
}
|
|
}
|
|
|
|
func (s *UserService) FindByID(ctx context.Context, id string) (*sqlc.UserAuth, error) {
|
|
user, err := s.queries.SelectUserById(ctx, id)
|
|
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
return &user, nil
|
|
}
|
|
|
|
func (s *UserService) FindByUsername(ctx context.Context, username string) (*sqlc.UserAuth, error) {
|
|
user, err := s.queries.SelectUserByUsername(ctx, username)
|
|
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
return &user, nil
|
|
}
|
|
|
|
func (s *UserService) CreateUser(ctx context.Context, userInput sqlc.InsertUserParams) (*sqlc.UserAuth, error) {
|
|
user, err := s.queries.InsertUser(ctx, userInput)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &user, nil
|
|
}
|