mobility-accounts/grpcapi/grpcapi.go

168 lines
5.6 KiB
Go
Executable File

package grpcapi
import (
context "context"
"fmt"
"log"
"net"
"git.coopgo.io/coopgo-platform/mobility-accounts/handlers"
"github.com/spf13/viper"
"google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
"google.golang.org/grpc/reflection"
status "google.golang.org/grpc/status"
)
type MobilityAccountsServerImpl struct {
handler handlers.MobilityAccountsHandler
}
func NewMobilityAccountsServer(h handlers.MobilityAccountsHandler) *MobilityAccountsServerImpl {
return &MobilityAccountsServerImpl{
handler: h,
}
}
func (s MobilityAccountsServerImpl) Login(ctx context.Context, req *LoginRequest) (*LoginResponse, error) {
account, err := s.handler.Login(req.Username, req.Password, req.Namespace)
if err != nil {
return nil, status.Errorf(codes.NotFound, "could not log in : %v", err)
}
response, err := AccountFromStorageType(account)
if err != nil {
fmt.Println(err)
return nil, status.Errorf(codes.Internal, "issue while retrieving account : %v", err)
}
return &LoginResponse{Account: response}, nil
}
func (s MobilityAccountsServerImpl) Register(ctx context.Context, req *RegisterRequest) (*RegisterResponse, error) {
a := req.Account.ToStorageType()
account, err := s.handler.Register(a)
if err != nil {
fmt.Println(err)
return nil, status.Errorf(codes.AlreadyExists, "account creation failed : %v", err)
}
response, err := AccountFromStorageType(account)
if err != nil {
fmt.Println(err)
return nil, status.Errorf(codes.Internal, "issue while retrieving account : %v", err)
}
return &RegisterResponse{Account: response}, nil
}
func (s MobilityAccountsServerImpl) UpdateData(ctx context.Context, req *UpdateDataRequest) (*UpdateDataResponse, error) {
a := req.Account.ToStorageType()
account, err := s.handler.UpdateData(a.ID, a.Data)
if err != nil {
return nil, status.Errorf(codes.Internal, "issue while apdating account : %v", err)
}
response, err := AccountFromStorageType(account)
if err != nil {
fmt.Println(err)
return nil, status.Errorf(codes.Internal, "issue while retrieving account : %v", err)
}
return &UpdateDataResponse{Account: response}, nil
}
func (s MobilityAccountsServerImpl) UpdatePhoneNumber(ctx context.Context, req *UpdatePhoneNumberRequest) (*UpdatePhoneNumberResponse, error) {
err := s.handler.UpdatePhoneNumber(
req.Id,
req.PhoneNumber,
req.Verified,
req.VerificationCode,
)
if err != nil {
return nil, status.Errorf(codes.Internal, "issue while updating phone number : %v", err)
}
return &UpdatePhoneNumberResponse{Ok: true}, nil
}
func (s MobilityAccountsServerImpl) GetAccount(ctx context.Context, req *GetAccountRequest) (*GetAccountResponse, error) {
account, err := s.handler.GetAccount(req.Id)
if err != nil {
fmt.Println(err)
return nil, status.Errorf(codes.AlreadyExists, "issue while retrieving account : %v", err)
}
response, err := AccountFromStorageType(account)
if err != nil {
fmt.Println(err)
return nil, status.Errorf(codes.Internal, "issue while retrieving account : %v", err)
}
return &GetAccountResponse{Account: response}, nil
}
func (s MobilityAccountsServerImpl) GetAccountUsername(ctx context.Context, req *GetAccountUsernameRequest) (*GetAccountUsernameResponse, error) {
account, err := s.handler.GetAccountUsername(req.Username, req.Namespace)
if err != nil {
fmt.Println(err)
return nil, status.Errorf(codes.AlreadyExists, "issue while retrieving account : %v", err)
}
response, err := AccountFromStorageType(account)
if err != nil {
fmt.Println(err)
return nil, status.Errorf(codes.Internal, "issue while retrieving account : %v", err)
}
return &GetAccountUsernameResponse{Account: response}, nil
}
func (s MobilityAccountsServerImpl) GetAccounts(ctx context.Context, req *GetAccountsRequest) (*GetAccountsResponse, error) {
responses, err := s.handler.GetAccounts(req.Namespaces)
if err != nil {
return nil, status.Errorf(codes.NotFound, "could not get accounts : %v", err)
}
var accounts []*Account
for _, a := range responses {
account, err := AccountFromStorageType(&a)
if err != nil {
return nil, status.Errorf(codes.Internal, "could not get accounts : %v", err)
}
accounts = append(accounts, account)
}
return &GetAccountsResponse{Accounts: accounts}, nil
}
func (s MobilityAccountsServerImpl) GetAccountsBatch(ctx context.Context, req *GetAccountsBatchRequest) (*GetAccountsBatchResponse, error) {
responses, err := s.handler.GetAccountsBatch(req.Accountids)
if err != nil {
return nil, status.Errorf(codes.NotFound, "could not get accounts : %v", err)
}
var accounts []*Account
for _, a := range responses {
account, err := AccountFromStorageType(&a)
if err != nil {
return nil, status.Errorf(codes.Internal, "could not get accounts : %v", err)
}
accounts = append(accounts, account)
}
return &GetAccountsBatchResponse{Accounts: accounts}, nil
}
func (s MobilityAccountsServerImpl) ChangePassword(ctx context.Context, req *ChangePasswordRequest) (*ChangePasswordResponse, error) {
err := s.handler.ChangePassword(req.Id, req.Password)
if err != nil {
return nil, err
}
return &ChangePasswordResponse{}, nil
}
func (MobilityAccountsServerImpl) mustEmbedUnimplementedMobilityAccountsServer() {}
func Run(done chan error, cfg *viper.Viper, handler handlers.MobilityAccountsHandler) {
var (
dev_env = cfg.GetBool("dev_env")
address = ":" + cfg.GetString("services.grpc.port")
)
fmt.Println("-> GRPC server on", address)
server := grpc.NewServer()
RegisterMobilityAccountsServer(server, NewMobilityAccountsServer(handler))
l, err := net.Listen("tcp", address)
if err != nil {
log.Fatal(err)
}
if dev_env {
reflection.Register(server)
}
if err := server.Serve(l); err != nil {
fmt.Println("gRPC service ended")
done <- err
}
}