initial commit
This commit is contained in:
88
internal/repository/postgres/auth/account.go
Normal file
88
internal/repository/postgres/auth/account.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.uber.org/fx"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
|
||||
domainAuth "base/internal/domain/auth"
|
||||
)
|
||||
|
||||
type accountRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewAccountRepository(lc fx.Lifecycle, db *gorm.DB) domainAuth.AccountRepository {
|
||||
lc.Append(
|
||||
fx.Hook{
|
||||
OnStart: func(ctx context.Context) error {
|
||||
return nil
|
||||
},
|
||||
OnStop: func(ctx context.Context) error {
|
||||
return nil
|
||||
},
|
||||
})
|
||||
return &accountRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *accountRepository) Create(ctx context.Context, account *domainAuth.Account) error {
|
||||
model := toAccountModel(account)
|
||||
if err := r.db.WithContext(ctx).Create(model).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
copyAccountFromModel(account, model)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *accountRepository) FindByID(ctx context.Context, id uuid.UUID) (*domainAuth.Account, error) {
|
||||
var model AccountModel
|
||||
if err := r.db.WithContext(ctx).Where("id = ?", id).First(&model).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toAccountDomain(&model), nil
|
||||
}
|
||||
|
||||
func (r *accountRepository) FindByUserID(ctx context.Context, userID uuid.UUID) ([]*domainAuth.Account, error) {
|
||||
var models []AccountModel
|
||||
if err := r.db.WithContext(ctx).Where("user_id = ?", userID).Find(&models).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
accounts := make([]*domainAuth.Account, len(models))
|
||||
for i, model := range models {
|
||||
accounts[i] = toAccountDomain(&model)
|
||||
}
|
||||
return accounts, nil
|
||||
}
|
||||
|
||||
func (r *accountRepository) Update(ctx context.Context, account *domainAuth.Account) error {
|
||||
model := toAccountModel(account)
|
||||
return r.db.WithContext(ctx).Model(&AccountModel{}).Where("id = ?", account.ID).Updates(model).Error
|
||||
}
|
||||
|
||||
func (r *accountRepository) Delete(ctx context.Context, id uuid.UUID) error {
|
||||
return r.db.WithContext(ctx).Delete(&AccountModel{}, "id = ?", id).Error
|
||||
}
|
||||
|
||||
func (r *accountRepository) List(ctx context.Context, limit, offset int) ([]*domainAuth.Account, error) {
|
||||
var models []AccountModel
|
||||
if err := r.db.WithContext(ctx).Limit(limit).Offset(offset).Find(&models).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
accounts := make([]*domainAuth.Account, len(models))
|
||||
for i, model := range models {
|
||||
accounts[i] = toAccountDomain(&model)
|
||||
}
|
||||
return accounts, nil
|
||||
}
|
||||
|
||||
func (r *accountRepository) Count(ctx context.Context) (int64, error) {
|
||||
var count int64
|
||||
if err := r.db.WithContext(ctx).Model(&AccountModel{}).Count(&count).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
Reference in New Issue
Block a user