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.
84 lines
1.5 KiB
84 lines
1.5 KiB
package db
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net"
|
|
"strconv"
|
|
|
|
"git.slaventius.ru/test3k/auth/internal/config"
|
|
|
|
api "git.slaventius.ru/test3k/umate/pkg/api"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
)
|
|
|
|
type AuthDBClient struct {
|
|
config *config.Config
|
|
client *grpc.ClientConn
|
|
db api.AuthDBClient
|
|
ctx context.Context
|
|
}
|
|
|
|
func NewDBClient(ctx context.Context, config *config.Config) *AuthDBClient {
|
|
conStr := net.JoinHostPort(config.Db.Host, strconv.Itoa(config.Db.Port))
|
|
// client, err := grpc.Dial(conStr, grpc.WithInsecure())
|
|
client, err := grpc.Dial(conStr, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
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 nil
|
|
}
|
|
|
|
func (s *AuthDBClient) Registration(uid string, email string, password string) (string, error) {
|
|
res, err := s.db.Registration(s.ctx, &api.RegistrationRequest{
|
|
Login: uid,
|
|
Password: password,
|
|
Email: email,
|
|
})
|
|
|
|
//
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return res.GetCode(), nil
|
|
}
|
|
|
|
func (s *AuthDBClient) Confirmation(code string) error {
|
|
_, err := s.db.Confirmation(s.ctx, &api.ConfirmationRequest{
|
|
Code: code,
|
|
})
|
|
|
|
//
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|