forked from mtusi2026/mtusi2026lab6
ЛР 6 — БПИ2302 — Нурлыгаянова — на 3
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user