97 lines
2.3 KiB
Go
97 lines
2.3 KiB
Go
package query
|
|
|
|
import (
|
|
"github.com/Caknoooo/go-pagination"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type M_Client struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
PIC string `json:"pic"`
|
|
Phone string `json:"phone"`
|
|
Email string `json:"email"`
|
|
Address string `json:"address"`
|
|
Logo string `json:"logo"`
|
|
}
|
|
|
|
type ClientFilter struct {
|
|
pagination.BaseFilter
|
|
Name string `form:"name"` // tambahkan ini
|
|
PIC string `form:"pic"` // tambahkan ini
|
|
Phone string `form:"phone"` // tambahkan ini
|
|
Email string `form:"email"` // tambahkan ini
|
|
Address string `form:"address"` // tambahkan ini
|
|
ClientID string `form:"client_id"` // tambahkan ini
|
|
Includes []string `form:"includes"` // tambahkan ini
|
|
}
|
|
|
|
func (f *ClientFilter) ApplyFilters(query *gorm.DB) *gorm.DB {
|
|
// Apply your filters here
|
|
if f.Name != "" {
|
|
query = query.Where("name ILIKE ?", "%"+f.Name+"%")
|
|
}
|
|
if f.ClientID != "" {
|
|
query = query.Where("client_id = ?", f.ClientID)
|
|
}
|
|
if f.PIC != "" {
|
|
query = query.Where("pic ILIKE ?", "%"+f.PIC+"%")
|
|
}
|
|
if f.Phone != "" {
|
|
query = query.Where("phone ILIKE ?", "%"+f.Phone+"%")
|
|
}
|
|
if f.Email != "" {
|
|
query = query.Where("email ILIKE ?", "%"+f.Email+"%")
|
|
}
|
|
if f.Address != "" {
|
|
query = query.Where("address ILIKE ?", "%"+f.Address+"%")
|
|
}
|
|
|
|
// Manual preload untuk roles dengan field terbatas
|
|
// for _, include := range f.Includes {
|
|
// if include == "Roles" {
|
|
// query = query.Preload("Roles", func(db *gorm.DB) *gorm.DB {
|
|
// return db.Select("id", "name") // Hanya ambil id dan name
|
|
// })
|
|
// }
|
|
// }
|
|
return query
|
|
}
|
|
|
|
func (f *ClientFilter) GetTableName() string {
|
|
return "m_clients"
|
|
}
|
|
|
|
func (f *ClientFilter) GetSearchFields() []string {
|
|
return []string{"name"}
|
|
}
|
|
|
|
func (f *ClientFilter) GetDefaultSort() string {
|
|
return "id asc"
|
|
}
|
|
|
|
func (f *ClientFilter) GetIncludes() []string {
|
|
return f.Includes
|
|
}
|
|
|
|
func (f *ClientFilter) GetPagination() pagination.PaginationRequest {
|
|
return f.Pagination
|
|
}
|
|
|
|
func (f *ClientFilter) Validate() {
|
|
var validIncludes []string
|
|
allowedIncludes := f.GetAllowedIncludes()
|
|
for _, include := range f.Includes {
|
|
if allowedIncludes[include] {
|
|
validIncludes = append(validIncludes, include)
|
|
}
|
|
}
|
|
f.Includes = validIncludes
|
|
}
|
|
|
|
func (f *ClientFilter) GetAllowedIncludes() map[string]bool {
|
|
return map[string]bool{
|
|
// "Roles": true,
|
|
}
|
|
}
|