github.com/lovung/GoCleanArchitecture@v0.0.0-20210302152432-50d91fd29f9f/app/internal/usecase/interactor/user_usecase.go (about)

     1  package interactor
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/lovung/GoCleanArchitecture/app/internal/domain/entity"
     7  	"github.com/lovung/GoCleanArchitecture/app/internal/domain/repository"
     8  	"github.com/lovung/GoCleanArchitecture/app/internal/usecase/dto"
     9  	"github.com/lovung/GoCleanArchitecture/pkg/copier"
    10  )
    11  
    12  // UserUseCase implements the user use case interface
    13  type UserUseCase struct {
    14  	userRepo repository.UserRepository
    15  }
    16  
    17  // NewUserUseCase create the new use case
    18  func NewUserUseCase(
    19  	userRepo repository.UserRepository,
    20  ) *UserUseCase {
    21  	return &UserUseCase{
    22  		userRepo: userRepo,
    23  	}
    24  }
    25  
    26  // Create method to create a new user
    27  func (u *UserUseCase) Create(ctx context.Context, candidate dto.CreateUserRequest) (created dto.OneUserResponse, err error) {
    28  	var ent entity.User
    29  	copier.MustCopy(&ent, &candidate)
    30  
    31  	err = ent.HashPassword()
    32  	if err != nil {
    33  		return created, err
    34  	}
    35  
    36  	createdEnt, err := u.userRepo.Create(ctx, ent)
    37  	if err != nil {
    38  		return created, err
    39  	}
    40  
    41  	copier.MustCopy(&created, &createdEnt)
    42  	return created, nil
    43  }
    44  
    45  // GetByID to get the user by ID
    46  func (u *UserUseCase) GetByID(ctx context.Context, id interface{}) (exist dto.OneUserResponse, err error) {
    47  	existEnt, err := u.userRepo.GetByID(ctx, id)
    48  	if err != nil {
    49  		return dto.OneUserResponse{}, err
    50  	}
    51  
    52  	// Do some specific business logic code here
    53  	copier.MustCopy(&exist, &existEnt)
    54  	return exist, nil
    55  }