softplayer-backend/api/v1/accounts.go

82 lines
2.0 KiB
Go

package v1
import (
"context"
"git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers"
"git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/accounts"
ctrl "sigs.k8s.io/controller-runtime"
)
func NewAccountRPCImpl(contoller ctrl.Manager, hashCost int16) *AccountsServer {
return &AccountsServer{
Controller: contoller,
Params: &controllers.AccountParams{
HashCost: hashCost,
},
}
}
type AccountsServer struct {
accounts.UnimplementedAccountsServer
Controller ctrl.Manager
Params *controllers.AccountParams
}
func (a *AccountsServer) SignUp(ctx context.Context, in *accounts.AccountWithPassword) (*accounts.AccountFullWithToken, error) {
data := populateData(in.Data.GetName(), in.Password.GetPassword(), in.Data.GetEmail())
acc := populateAccount(data, a.Controller)
if err := acc.Create(ctx); err != nil {
return nil, err
}
return &accounts.AccountFullWithToken{
Id: &accounts.AccountId{
Id: acc.Data.UUID,
},
Data: &accounts.AccountData{
Name: acc.Data.Username,
Email: acc.Data.Email,
},
Token: acc.Token,
}, nil
}
func (a *AccountsServer) SignIn(ctx context.Context, in *accounts.AccountWithPassword) (*accounts.AccountFullWithToken, error) {
data := populateData(in.Data.GetName(), in.Password.GetPassword(), in.Data.GetEmail())
acc := populateAccount(data, a.Controller)
if err := acc.Login(ctx); err != nil {
return nil, err
}
return &accounts.AccountFullWithToken{
Id: &accounts.AccountId{
Id: acc.Data.UUID,
},
Data: &accounts.AccountData{
Name: acc.Data.Username,
Email: acc.Data.Email,
},
Token: acc.Token,
}, nil
}
func populateData(username, password, email string) *controllers.AccountData {
return &controllers.AccountData{
Username: username,
Password: password,
Email: email,
}
}
func populateAccount(data *controllers.AccountData, controller ctrl.Manager) *controllers.Account {
return &controllers.Account{
Controller: controller,
Data: data,
}
}