add filtering and base repository
This commit is contained in:
@@ -2,35 +2,16 @@ package repository
|
||||
|
||||
import (
|
||||
"acc-server-manager/local/model"
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ApiRepository struct {
|
||||
db *gorm.DB
|
||||
*BaseRepository[model.ApiModel, model.ApiFilter]
|
||||
}
|
||||
|
||||
func NewApiRepository(db *gorm.DB) *ApiRepository {
|
||||
return &ApiRepository{
|
||||
db: db,
|
||||
BaseRepository: NewBaseRepository[model.ApiModel, model.ApiFilter](db, model.ApiModel{}),
|
||||
}
|
||||
}
|
||||
|
||||
// GetFirst
|
||||
// Gets first row from API table.
|
||||
//
|
||||
// Args:
|
||||
// context.Context: Application context
|
||||
// Returns:
|
||||
// model.ApiModel: Api object from database.
|
||||
func (as ApiRepository) GetFirst(ctx context.Context) *model.ApiModel {
|
||||
db := as.db.WithContext(ctx)
|
||||
apiModel := new(model.ApiModel)
|
||||
result := db.First(&apiModel)
|
||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
return nil
|
||||
}
|
||||
return apiModel
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user