feat: use PO for repo layer
All checks were successful
Build and test / Build (push) Successful in 2m26s

This commit is contained in:
Muyao CHEN 2024-10-16 09:59:03 +02:00
parent 0e05924585
commit 29633e0e95
4 changed files with 26 additions and 13 deletions

View File

@ -50,8 +50,8 @@ func NewUserRepository(db *sql.DB) repo.UserRepository {
func (ur *userRepository) Create(
ctx context.Context,
transaction interface{},
u *model.User,
) (*model.User, error) {
u *model.UserPO,
) (*model.UserPO, error) {
timeoutCtx, cancel := context.WithTimeout(ctx, insertTimeout)
defer cancel()
@ -76,7 +76,7 @@ func (ur *userRepository) Create(
return nil, err
}
return &model.User{
return &model.UserPO{
ID: int(userDB.ID),
Email: userDB.Email,
FirstName: userDB.FirstName,
@ -88,7 +88,7 @@ func (ur *userRepository) Create(
}
// GetByEmail if not found, return nil for user but not error.
func (ur *userRepository) GetByEmail(ctx context.Context, email string) (*model.User, error) {
func (ur *userRepository) GetByEmail(ctx context.Context, email string) (*model.UserPO, error) {
queries := sqlc.New(ur.db)
userDB, err := queries.GetUserByEmail(ctx, email)
if errors.Is(err, pgx.ErrNoRows) {
@ -98,7 +98,7 @@ func (ur *userRepository) GetByEmail(ctx context.Context, email string) (*model.
return nil, err
}
return &model.User{
return &model.UserPO{
ID: int(userDB.ID),
Email: userDB.Email,
FirstName: userDB.FirstName,

View File

@ -29,6 +29,6 @@ import (
)
type UserRepository interface {
Create(ctx context.Context, transaction interface{}, u *model.User) (*model.User, error)
GetByEmail(ctx context.Context, email string) (*model.User, error)
Create(ctx context.Context, transaction interface{}, u *model.UserPO) (*model.UserPO, error)
GetByEmail(ctx context.Context, email string) (*model.UserPO, error)
}

View File

@ -37,8 +37,8 @@ type TestUserRepository struct{}
func (tur *TestUserRepository) Create(
ctx context.Context,
transaction interface{},
u *model.User,
) (*model.User, error) {
u *model.UserPO,
) (*model.UserPO, error) {
user := *u
user.ID = 123
@ -50,11 +50,11 @@ func (tur *TestUserRepository) Create(
return &user, nil
}
func (ur *TestUserRepository) GetByEmail(ctx context.Context, email string) (*model.User, error) {
func (ur *TestUserRepository) GetByEmail(ctx context.Context, email string) (*model.UserPO, error) {
hashedPwd, _ := bcrypt.GenerateFromPassword([]byte("strongHashed"), 12)
switch email {
case "a@b.c":
return &model.User{
return &model.UserPO{
ID: 123,
Email: "a@b.c",
Password: string(hashedPwd),

View File

@ -82,7 +82,7 @@ func (uuc *userUsecase) Create(ctx context.Context, u *model.UserCreateDTO) (*mo
data, err := uuc.dbRepo.Transaction(
ctx,
func(txCtx context.Context, tx interface{}) (interface{}, error) {
created, err := uuc.userRepo.Create(txCtx, tx, &model.User{
created, err := uuc.userRepo.Create(txCtx, tx, &model.UserPO{
Email: u.Email,
Password: u.Password,
FirstName: u.FirstName,
@ -113,7 +113,20 @@ func (uuc *userUsecase) Create(ctx context.Context, u *model.UserCreateDTO) (*mo
return nil, err
}
user := data.(*model.User)
userPO := data.(*model.UserPO)
user := &model.User{
ID: userPO.ID,
Email: userPO.Email,
Password: userPO.Password,
FirstName: userPO.FirstName,
LastName: userPO.LastName,
EventIDs: []int{}, // Unfilled
CreatedAt: userPO.CreatedAt,
UpdatedAt: userPO.UpdatedAt,
}
return user, nil
}