forked from mtusi2026/mtusi2026lab5
45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/domain"
|
|
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/password"
|
|
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/store"
|
|
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/validate"
|
|
)
|
|
|
|
var (
|
|
_ UsersService = (*Users)(nil)
|
|
)
|
|
|
|
type Users struct {
|
|
store store.UserStore
|
|
hasher *password.Hasher
|
|
}
|
|
|
|
func NewUsers(s store.UserStore, h *password.Hasher) *Users {
|
|
return &Users{store: s, hasher: h}
|
|
}
|
|
|
|
func (u *Users) Register(ctx context.Context, username, displayName, plainPassword string) (domain.User, error) {
|
|
if err := validate.Username(username); err != nil {
|
|
return domain.User{}, err
|
|
}
|
|
if err := validate.DisplayName(displayName); err != nil {
|
|
return domain.User{}, err
|
|
}
|
|
if err := validate.Password(plainPassword); err != nil {
|
|
return domain.User{}, err
|
|
}
|
|
hash, err := u.hasher.Hash(ctx, plainPassword)
|
|
if err != nil {
|
|
return domain.User{}, err
|
|
}
|
|
return u.store.CreateUser(ctx, username, displayName, hash)
|
|
}
|
|
|
|
func (u *Users) Me(ctx context.Context, callerID string) (domain.User, error) {
|
|
return u.store.UserByID(ctx, callerID)
|
|
}
|