github.com/lovung/GoCleanArchitecture@v0.0.0-20210302152432-50d91fd29f9f/app/internal/interface/persistence/rdbms/gormrepo/user_repository.go (about)

     1  package gormrepo
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/lovung/GoCleanArchitecture/app/internal/domain/entity"
     7  )
     8  
     9  // UserRepository repository struct implement the User Repository interface
    10  type UserRepository struct {
    11  	baseRepository
    12  }
    13  
    14  // NewUserRepository creates the new instance of User Repository
    15  func NewUserRepository() *UserRepository {
    16  	return &UserRepository{}
    17  }
    18  
    19  // Create method to call DB to create the new user
    20  func (r *UserRepository) Create(ctx context.Context, ent entity.User) (created entity.User, err error) {
    21  	err = ent.GenID()
    22  	if err != nil {
    23  		return
    24  	}
    25  	err = r.DB(ctx).Create(&ent).Error
    26  	if err != nil {
    27  		return
    28  	}
    29  	err = r.DB(ctx).Take(&created, "id = ?", ent.ID).Error
    30  	return created, err
    31  }
    32  
    33  // GetByID get the User by ID
    34  func (r *UserRepository) GetByID(ctx context.Context, id interface{}) (ent entity.User, err error) {
    35  	err = r.DB(ctx).Take(&ent, id).Error
    36  	return ent, err
    37  }