Spaces:
Runtime error
Runtime error
Delete repositories
Browse filesFiles changed (2)
- repositories/account_repository.goDeleted-87
- repositories/repository.goDeleted-117
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687package repositories
import (
"pweb-api.abdanhafidz.com/models"
)
func GetAccountbyEmail(email string) Repository[models.Account, models.Account] {
repo := Construct[models.Account, models.Account](
models.Account{Email: email},
)
repo.Transactions(
WhereGivenConstructor[models.Account, models.Account],
Find[models.Account, models.Account],
)
return *repo
}
func GetAllAccount() Repository[models.Account, []models.Account] {
repo := Construct[models.Account, []models.Account](
models.Account{},
)
repo.Transactions(
Find[models.Account, []models.Account],
)
return *repo
}
func GetAccountById(AccountId uint) Repository[models.Account, models.Account] {
repo := Construct[models.Account, models.Account](
models.Account{Id: AccountId},
)
repo.Transactions(
WhereGivenConstructor[models.Account, models.Account],
Find[models.Account, models.Account],
)
return *repo
}
func UpdateAccount(account models.Account) Repository[models.Account, models.Account] {
repo := Construct[models.Account, models.Account](
account,
)
repo.Transaction.Save(&repo.Constructor)
repo.Result = repo.Constructor
return *repo
}
func GetDetailAccountById(AccountId uint) Repository[models.AccountDetails, models.AccountDetails] {
repo := Construct[models.AccountDetails, models.AccountDetails](
models.AccountDetails{AccountId: AccountId},
)
// fmt.Println("Account ID:", repo.Constructor.AccountId)
repo.Transactions(
WhereGivenConstructor[models.AccountDetails, models.AccountDetails],
Find[models.AccountDetails, models.AccountDetails],
)
return *repo
}
func CreateAccount(account models.Account) Repository[models.Account, models.Account] {
repo := Construct[models.Account, models.Account](
account,
)
Create(repo)
return *repo
}
func CreateAccountDetails(accountDetails models.AccountDetails) Repository[models.AccountDetails, models.AccountDetails] {
repo := Construct[models.AccountDetails, models.AccountDetails](
accountDetails,
)
Create(repo)
return *repo
}
func UpdateAccountDetails(accountDetails models.AccountDetails) Repository[models.AccountDetails, models.AccountDetails] {
repo := Construct[models.AccountDetails, models.AccountDetails](
models.AccountDetails{AccountId: accountDetails.AccountId},
)
repo.Transaction.Where("account_id = ?", accountDetails.AccountId).First(&repo.Constructor)
accountDetails.ID = repo.Constructor.ID
// fmt.Println(repo.Constructor)
// fmt.Println(accountDetails)
repo.Transaction.Updates(accountDetails)
repo.Result = accountDetails
return *repo
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117package repositories
import (
"gorm.io/gorm"
"pweb-api.abdanhafidz.com/config"
)
type Repositories interface {
FindAllPaginate()
Where()
Find()
Create()
Update()
CustomQuery()
Delete()
}
type PaginationConstructor struct {
Limit int
Offset int
Filter string
}
type CustomQueryConstructor struct {
SQL string
Values interface{}
}
type Repository[TConstructor any, TResult any] struct {
Constructor TConstructor
Pagination PaginationConstructor
CustomQuery CustomQueryConstructor
Result TResult
Transaction *gorm.DB
RowsCount int
NoRecord bool
RowsError error
}
func Construct[TConstructor any, TResult any](constructor ...TConstructor) *Repository[TConstructor, TResult] {
if len(constructor) == 1 {
return &Repository[TConstructor, TResult]{
Constructor: constructor[0],
Transaction: config.DB,
}
}
return &Repository[TConstructor, TResult]{
Constructor: constructor[0],
Transaction: config.DB.Begin(),
}
}
func (repo *Repository[T1, T2]) Transactions(transactions ...func(*Repository[T1, T2]) *gorm.DB) {
for _, tx := range transactions {
repo.Transaction = tx(repo)
if repo.RowsError != nil {
return
}
}
}
func WhereGivenConstructor[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB {
tx := repo.Transaction.Where(&repo.Constructor)
repo.RowsCount = int(tx.RowsAffected)
repo.NoRecord = repo.RowsCount == 0
repo.RowsError = tx.Error
return tx
}
func Find[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB {
tx := repo.Transaction.Find(&repo.Result)
repo.RowsCount = int(tx.RowsAffected)
repo.NoRecord = repo.RowsCount == 0
repo.RowsError = tx.Error
return tx
}
func FinddAllPaginate[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB {
tx := repo.Transaction.Limit(repo.Pagination.Limit).Offset(repo.Pagination.Offset).Find(&repo.Result)
repo.RowsCount = int(tx.RowsAffected)
repo.NoRecord = repo.RowsCount == 0
repo.RowsError = tx.Error
return tx
}
func Create[T1 any](repo *Repository[T1, T1]) *gorm.DB {
tx := repo.Transaction.Create(&repo.Constructor)
repo.RowsCount = int(tx.RowsAffected)
repo.NoRecord = repo.RowsCount == 0
repo.RowsError = tx.Error
repo.Result = repo.Constructor
return tx
}
func Update[T1 any](repo *Repository[T1, T1]) *gorm.DB {
tx := repo.Transaction.Updates(&repo.Constructor)
repo.RowsCount = int(tx.RowsAffected)
repo.NoRecord = repo.RowsCount == 0
repo.RowsError = tx.Error
repo.Result = repo.Constructor
return tx
}
func Delete[T1 any](repo *Repository[T1, T1]) *gorm.DB {
tx := repo.Transaction.Delete(&repo.Constructor)
repo.RowsCount = int(tx.RowsAffected)
repo.NoRecord = repo.RowsCount == 0
repo.RowsError = tx.Error
return tx
}
func CustomQuery[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB {
tx := repo.Transaction.Raw(repo.CustomQuery.SQL, repo.CustomQuery.Values).Scan(&repo.Result)
repo.RowsCount = int(tx.RowsAffected)
repo.NoRecord = repo.RowsCount == 0
repo.RowsError = tx.Error
return tx
}repositories/account_repository.goDeleted-87
repositories/repository.goDeleted-117