Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create UserWithPassword DTO #80

Open
Feyzanrs opened this issue May 19, 2024 · 0 comments
Open

Create UserWithPassword DTO #80

Feyzanrs opened this issue May 19, 2024 · 0 comments
Assignees
Labels
Be enhancement New feature or request feature Proposal or implementation of new features

Comments

@Feyzanrs
Copy link
Member

Feyzanrs commented May 19, 2024

We need to create a new file, userwithpassword.go, in the pkg/presentation/dto directory. This file will contain the UserWithPassword struct which serves as a Data Transfer Object (DTO) combining user details with password information. This struct will facilitate the handling of user and password data together, particularly useful for processes that involve both user information and authentication details.

Path of the file to be created

pkg/presentation/dto/userwithpassword.go

Code Overview

package presentation

import (
	"fmt"

	pb "github.com/octoposprime/op-be-shared/pkg/proto/pb/user"
	tuuid "github.com/octoposprime/op-be-shared/tool/uuid"
	me "github.com/octoposprime/op-be-user/internal/domain/model/entity"
	mo "github.com/octoposprime/op-be-user/internal/domain/model/object"
	"google.golang.org/protobuf/types/known/timestamppb"
)

// UserWithPassword is a struct that represents the dto of a user with password values.
type UserWithPassword struct {
	proto *pb.UserWithPassword
}

// NewUserWithPassword creates a new *UserWithPassword.
func NewUserWithPassword(pb *pb.UserWithPassword) *UserWithPassword {
	return &UserWithPassword{
		proto: pb,
	}
}

// String returns a string representation of the UserWithPassword.
func (s *UserWithPassword) String() string {
	return fmt.Sprintf(
		"User: %v, "+
			"Password: %v",
		s.proto.User,
		"***",
	)
}

func NewUserWithPasswordFromEntity(entity me.User) *UserWithPassword {
	return &UserWithPassword{
		&pb.UserWithPassword{
			User: &pb.User{
				Id:         entity.Id.String(),
				Username:   entity.UserName,
				Email:      entity.Email,
				Role:       entity.Role,
				UserType:   pb.UserType(entity.UserType),
				UserStatus: pb.UserStatus(entity.UserStatus),
				FirstName:  entity.FirstName,
				LastName:   entity.LastName,
				CreatedAt:  timestamppb.New(entity.CreatedAt), // Not sure if this needs to be converted
				UpdatedAt:  timestamppb.New(entity.UpdatedAt), // Not sure if this needs to be converted
			},
		},
	}
}

// ToPb returns a protobuf representation of the UserWithPassword.
func (s *UserWithPassword) ToPb() *pb.UserWithPassword {
	return s.proto
}

// ToEntity returns a entity representation of the UserWithPassword.
func (s *UserWithPassword) ToEntity() (*me.User, *me.UserPassword) {
	return &me.User{
			Id: tuuid.FromString(s.proto.User.Id),
			User: mo.User{
				UserName:   s.proto.User.Username,
				Email:      s.proto.User.Email,
				Role:       s.proto.User.Role,
				UserType:   mo.UserType(s.proto.User.UserType),
				UserStatus: mo.UserStatus(s.proto.User.UserStatus),
				Tags:       s.proto.User.Tags,
				FirstName:  s.proto.User.FirstName,
				LastName:   s.proto.User.LastName,
			},
			CreatedAt: s.proto.User.CreatedAt.AsTime(), // Not sure if this needs to be converted
			UpdatedAt: s.proto.User.UpdatedAt.AsTime(), // Not sure if this needs to be converted
		}, &me.UserPassword{
			Id:     tuuid.FromString(s.proto.UserPassword.Id),
			UserId: tuuid.FromString(s.proto.UserPassword.UserId),
			UserPassword: mo.UserPassword{
				Password:       s.proto.UserPassword.Password,
				PasswordStatus: mo.PasswordStatus(s.proto.UserPassword.PasswordStatus),
			},
		}
}

type UserWithPasswords []*UserWithPassword

// NewUserWithPasswordsFromEntities creates a new []*UserWithPassword from entities.
func NewUserWithPasswordsFromEntities(entities []me.User) UserWithPasswords {
	userWithPasswords := make([]*UserWithPassword, len(entities))
	for i, entity := range entities {
		userWithPasswords[i] = NewUserWithPasswordFromEntity(entity)
	}
	return userWithPasswords
}

// NewEmptyUserWithPassword creates a new *UserWithPassword with empty values.
func NewEmptyUserWithPassword() *UserWithPassword {
	return &UserWithPassword{
		&pb.UserWithPassword{
			User: &pb.User{
				Id:         "",
				Username:   "",
				Email:      "",
				Role:       "",
				UserType:   pb.UserType_UserTypeNONE,
				UserStatus: pb.UserStatus_UserStatusNONE,
				Tags:       []string{},
				FirstName:  "",
				LastName:   "",
				CreatedAt:  timestamppb.Now(), // Not sure if this needs to be converted
				UpdatedAt:  timestamppb.Now(), // Not sure if this needs to be converted
			},
			UserPassword: &pb.UserPassword{
				Id:             "",
				UserId:         "",
				Password:       "",
				PasswordStatus: pb.PasswordStatus_PasswordStatusNONE,
			},
		},
	}
}

// ToPbs returns a protobuf representation of the UserWithPasswords
func (s UserWithPasswords) ToPbs() []*pb.UserWithPassword {
	userWithPasswords := make([]*pb.UserWithPassword, len(s))
	for i, userWithPassword := range s {
		userWithPasswords[i] = userWithPassword.proto
	}
	return userWithPasswords
}

The changes will be merged into the enhance/team/66/implement-password-validation branch first. Therefore, set the base branch of the pull request as enhance/team/66/implement-password-validation.

@Feyzanrs Feyzanrs added enhancement New feature or request feature Proposal or implementation of new features labels May 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Be enhancement New feature or request feature Proposal or implementation of new features
Projects
None yet
Development

No branches or pull requests

3 participants