User list
This commit is contained in:
17
app/repository/dao.go
Normal file
17
app/repository/dao.go
Normal file
@ -0,0 +1,17 @@
|
||||
package repository
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type DAO struct {
|
||||
db *gorm.DB
|
||||
UsersRepository *UsersRepository
|
||||
}
|
||||
|
||||
var Dao DAO
|
||||
|
||||
func CreateDAO(db *gorm.DB) DAO {
|
||||
return DAO{
|
||||
db: db,
|
||||
UsersRepository: CreateUsersRepository(db),
|
||||
}
|
||||
}
|
||||
12
app/repository/pagination.go
Normal file
12
app/repository/pagination.go
Normal file
@ -0,0 +1,12 @@
|
||||
package repository
|
||||
|
||||
type Pagination struct {
|
||||
PageSize int
|
||||
Page int
|
||||
}
|
||||
|
||||
func (p *Pagination) New() *Pagination {
|
||||
p.PageSize = 50
|
||||
p.Page = 1
|
||||
return p
|
||||
}
|
||||
45
app/repository/users.go
Normal file
45
app/repository/users.go
Normal file
@ -0,0 +1,45 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"iris-test/app/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type UsersRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
type UserFilter struct {
|
||||
IsActive *bool
|
||||
}
|
||||
|
||||
func CreateUsersRepository(db *gorm.DB) *UsersRepository {
|
||||
return &UsersRepository{db}
|
||||
}
|
||||
|
||||
func applyFilter(db *gorm.DB, filter *UserFilter) *gorm.DB {
|
||||
query := db
|
||||
|
||||
// if filter.State != "" {
|
||||
// query = query.Where("state = ?", filter.State)
|
||||
// }
|
||||
// if filter.SendAt_lt != nil {
|
||||
// query = query.Where("send_at < ?", filter.SendAt_lt)
|
||||
// }
|
||||
// if filter.SendAt_notNull != nil {
|
||||
// query = query.Not(map[string]interface{}{"send_at": nil})
|
||||
// }
|
||||
|
||||
return query
|
||||
}
|
||||
|
||||
func (repository *UsersRepository) List(filter *UserFilter) *[]models.User {
|
||||
var users []models.User
|
||||
|
||||
query := repository.db.Model(&models.User{})
|
||||
query = applyFilter(query, filter)
|
||||
query.Find(&users)
|
||||
|
||||
return &users
|
||||
}
|
||||
Reference in New Issue
Block a user