You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
1.3 KiB
80 lines
1.3 KiB
package db
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
|
|
"git.applicatura.com/test3k/auth/internal/config"
|
|
|
|
api "git.applicatura.com/test3k/umate/pkg/api"
|
|
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
type AuthDBClient struct {
|
|
config *config.Config
|
|
client *grpc.ClientConn
|
|
db api.AuthDBClient
|
|
ctx context.Context
|
|
}
|
|
|
|
func NewDBClient(ctx context.Context, config *config.Config) *AuthDBClient {
|
|
connStr := fmt.Sprintf("%s:%d", config.Db.Host, config.Db.Port)
|
|
client, err := grpc.Dial(connStr, grpc.WithInsecure())
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
return &AuthDBClient{
|
|
config: config,
|
|
client: client,
|
|
db: api.NewAuthDBClient(client),
|
|
ctx: ctx,
|
|
}
|
|
}
|
|
|
|
func (s *AuthDBClient) Close() error {
|
|
return s.client.Close()
|
|
}
|
|
|
|
func (s *AuthDBClient) Login(uid string, password string) error {
|
|
_, err := s.db.Login(s.ctx, &api.LoginRequest{
|
|
Login: uid,
|
|
Password: password,
|
|
})
|
|
|
|
//
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
func (s *AuthDBClient) Registration(uid string, email string) (string, error) {
|
|
res, err := s.db.Registration(s.ctx, &api.RegistrationRequest{
|
|
Login: uid,
|
|
Email: email,
|
|
})
|
|
|
|
//
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return res.GetCode(), err
|
|
}
|
|
|
|
func (s *AuthDBClient) Confirmation(code string) error {
|
|
_, err := s.db.Confirmation(s.ctx, &api.ConfirmationRequest{
|
|
Code: code,
|
|
})
|
|
|
|
//
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return err
|
|
}
|
|
|