package liveclient import ( "context" biz "kra/internal/biz/liveclient" "kra/internal/service/dto" ) type Service struct{ uc *biz.Usecase } func NewService(uc *biz.Usecase) *Service { return &Service{uc: uc} } func (s *Service) Register(ctx context.Context, in *dto.LiveClientRegisterRequest, ip, agent string) (*dto.LiveClientAuthResponse, error) { result, err := s.uc.Register(ctx, &biz.RegisterInput{Username: in.Username, Nickname: in.Nickname, Password: in.Password, ConfirmPassword: in.ConfirmPassword}, ip, agent) if err != nil { return nil, err } return authResponse(result), nil } func (s *Service) Login(ctx context.Context, in *dto.LiveClientLoginRequest, ip, agent string) (*dto.LiveClientAuthResponse, error) { result, err := s.uc.Login(ctx, &biz.LoginInput{Username: in.Username, Password: in.Password, IP: ip, Agent: agent}) if err != nil { return nil, err } return authResponse(result), nil } func (s *Service) Me(ctx context.Context, userID uint) (*dto.LiveClientAccountResponse, error) { account, err := s.uc.Get(ctx, userID) if err != nil { return nil, err } return accountResponse(account), nil } func (s *Service) Balance(ctx context.Context, userID uint) (*dto.LiveClientBalanceResponse, error) { account, err := s.uc.Get(ctx, userID) if err != nil { return nil, err } return &dto.LiveClientBalanceResponse{DiamondBalance: account.DiamondBalance}, nil } func (s *Service) UpdateProfile(ctx context.Context, userID uint, in *dto.LiveClientProfileRequest) (*dto.LiveClientAccountResponse, error) { account, err := s.uc.UpdateProfile(ctx, &biz.ProfileInput{UserID: userID, Nickname: in.Nickname, Avatar: in.Avatar}) if err != nil { return nil, err } return accountResponse(account), nil } func (s *Service) ChangePassword(ctx context.Context, userID uint, in *dto.LiveClientPasswordRequest, ip, agent string) (*dto.LiveClientAuthResponse, error) { result, err := s.uc.ChangePassword(ctx, &biz.PasswordInput{UserID: userID, OldPassword: in.OldPassword, NewPassword: in.NewPassword, ConfirmPassword: in.ConfirmPassword, IP: ip, Agent: agent}) if err != nil { return nil, err } return authResponse(result), nil } func accountResponse(account *biz.Account) *dto.LiveClientAccountResponse { return &dto.LiveClientAccountResponse{UserID: account.UserID, Username: account.Username, Nickname: account.Nickname, Avatar: account.Avatar, Enabled: account.Enabled, DiamondBalance: account.DiamondBalance, CreatedAt: account.CreatedAt} } func authResponse(result *biz.AuthResult) *dto.LiveClientAuthResponse { return &dto.LiveClientAuthResponse{User: accountResponse(result.Account), Token: result.Auth.Token, ExpiresAt: result.Auth.ExpiresAt.UnixMilli()} }