mirror of
https://github.com/get-drexa/drive.git
synced 2025-11-30 21:41:39 +00:00
33 lines
524 B
Go
33 lines
524 B
Go
|
|
package user
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"database/sql"
|
||
|
|
"errors"
|
||
|
|
|
||
|
|
"github.com/google/uuid"
|
||
|
|
"github.com/uptrace/bun"
|
||
|
|
)
|
||
|
|
|
||
|
|
type Service struct {
|
||
|
|
db *bun.DB
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewService(db *bun.DB) *Service {
|
||
|
|
return &Service{
|
||
|
|
db: db,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *Service) UserByID(ctx context.Context, id uuid.UUID) (*User, error) {
|
||
|
|
var user User
|
||
|
|
err := s.db.NewSelect().Model(&user).Where("id = ?", id).Scan(ctx)
|
||
|
|
if err != nil {
|
||
|
|
if errors.Is(err, sql.ErrNoRows) {
|
||
|
|
return nil, newNotFoundError(id)
|
||
|
|
}
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return &user, nil
|
||
|
|
}
|