mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-11 19:04:35 +00:00
User service: move to db (#146)
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/micro/micro/v3/service/errors"
|
||||
db "github.com/micro/services/db/proto"
|
||||
"github.com/micro/services/user/domain"
|
||||
pb "github.com/micro/services/user/proto"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
@@ -37,9 +38,9 @@ type User struct {
|
||||
domain *domain.Domain
|
||||
}
|
||||
|
||||
func NewUser() *User {
|
||||
func NewUser(db db.DbService) *User {
|
||||
return &User{
|
||||
domain: domain.New(),
|
||||
domain: domain.New(db),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +55,7 @@ func (s *User) Create(ctx context.Context, req *pb.CreateRequest, rsp *pb.Create
|
||||
}
|
||||
pp := base64.StdEncoding.EncodeToString(h)
|
||||
|
||||
return s.domain.Create(&pb.Account{
|
||||
return s.domain.Create(ctx, &pb.Account{
|
||||
Id: req.Id,
|
||||
Username: strings.ToLower(req.Username),
|
||||
Email: strings.ToLower(req.Email),
|
||||
@@ -62,16 +63,27 @@ func (s *User) Create(ctx context.Context, req *pb.CreateRequest, rsp *pb.Create
|
||||
}
|
||||
|
||||
func (s *User) Read(ctx context.Context, req *pb.ReadRequest, rsp *pb.ReadResponse) error {
|
||||
account, err := s.domain.Read(req.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
switch {
|
||||
case req.Id != "":
|
||||
account, err := s.domain.Read(ctx, req.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rsp.Account = account
|
||||
return nil
|
||||
case req.Username != "" || req.Email != "":
|
||||
accounts, err := s.domain.Search(ctx, req.Username, req.Email)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rsp.Account = accounts[0]
|
||||
return nil
|
||||
}
|
||||
rsp.Account = account
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *User) Update(ctx context.Context, req *pb.UpdateRequest, rsp *pb.UpdateResponse) error {
|
||||
return s.domain.Update(&pb.Account{
|
||||
return s.domain.Update(ctx, &pb.Account{
|
||||
Id: req.Id,
|
||||
Username: strings.ToLower(req.Username),
|
||||
Email: strings.ToLower(req.Email),
|
||||
@@ -79,20 +91,11 @@ func (s *User) Update(ctx context.Context, req *pb.UpdateRequest, rsp *pb.Update
|
||||
}
|
||||
|
||||
func (s *User) Delete(ctx context.Context, req *pb.DeleteRequest, rsp *pb.DeleteResponse) error {
|
||||
return s.domain.Delete(req.Id)
|
||||
}
|
||||
|
||||
func (s *User) Search(ctx context.Context, req *pb.SearchRequest, rsp *pb.SearchResponse) error {
|
||||
accounts, err := s.domain.Search(req.Username, req.Email, req.Limit, req.Offset)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rsp.Accounts = accounts
|
||||
return nil
|
||||
return s.domain.Delete(ctx, req.Id)
|
||||
}
|
||||
|
||||
func (s *User) UpdatePassword(ctx context.Context, req *pb.UpdatePasswordRequest, rsp *pb.UpdatePasswordResponse) error {
|
||||
usr, err := s.domain.Read(req.UserId)
|
||||
usr, err := s.domain.Read(ctx, req.UserId)
|
||||
if err != nil {
|
||||
return errors.InternalServerError("user.updatepassword", err.Error())
|
||||
}
|
||||
@@ -100,7 +103,7 @@ func (s *User) UpdatePassword(ctx context.Context, req *pb.UpdatePasswordRequest
|
||||
return errors.InternalServerError("user.updatepassword", "Passwords don't math")
|
||||
}
|
||||
|
||||
salt, hashed, err := s.domain.SaltAndPassword(usr.Username, usr.Email)
|
||||
salt, hashed, err := s.domain.SaltAndPassword(ctx, usr.Username, usr.Email)
|
||||
if err != nil {
|
||||
return errors.InternalServerError("user.updatepassword", err.Error())
|
||||
}
|
||||
@@ -121,7 +124,7 @@ func (s *User) UpdatePassword(ctx context.Context, req *pb.UpdatePasswordRequest
|
||||
}
|
||||
pp := base64.StdEncoding.EncodeToString(h)
|
||||
|
||||
if err := s.domain.UpdatePassword(req.UserId, salt, pp); err != nil {
|
||||
if err := s.domain.UpdatePassword(ctx, req.UserId, salt, pp); err != nil {
|
||||
return errors.InternalServerError("user.updatepassword", err.Error())
|
||||
}
|
||||
return nil
|
||||
@@ -131,7 +134,7 @@ func (s *User) Login(ctx context.Context, req *pb.LoginRequest, rsp *pb.LoginRes
|
||||
username := strings.ToLower(req.Username)
|
||||
email := strings.ToLower(req.Email)
|
||||
|
||||
salt, hashed, err := s.domain.SaltAndPassword(username, email)
|
||||
salt, hashed, err := s.domain.SaltAndPassword(ctx, username, email)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -153,7 +156,7 @@ func (s *User) Login(ctx context.Context, req *pb.LoginRequest, rsp *pb.LoginRes
|
||||
Expires: time.Now().Add(time.Hour * 24 * 7).Unix(),
|
||||
}
|
||||
|
||||
if err := s.domain.CreateSession(sess); err != nil {
|
||||
if err := s.domain.CreateSession(ctx, sess); err != nil {
|
||||
return errors.InternalServerError("user.Login", err.Error())
|
||||
}
|
||||
rsp.Session = sess
|
||||
@@ -161,11 +164,11 @@ func (s *User) Login(ctx context.Context, req *pb.LoginRequest, rsp *pb.LoginRes
|
||||
}
|
||||
|
||||
func (s *User) Logout(ctx context.Context, req *pb.LogoutRequest, rsp *pb.LogoutResponse) error {
|
||||
return s.domain.DeleteSession(req.SessionId)
|
||||
return s.domain.DeleteSession(ctx, req.SessionId)
|
||||
}
|
||||
|
||||
func (s *User) ReadSession(ctx context.Context, req *pb.ReadSessionRequest, rsp *pb.ReadSessionResponse) error {
|
||||
sess, err := s.domain.ReadSession(req.SessionId)
|
||||
sess, err := s.domain.ReadSession(ctx, req.SessionId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user