go-mcm/model/django/django.go

166 lines
4.5 KiB
Go

package django
import (
"fmt"
"strings"
)
type Field string
const (
ID Field = "id"
Link Field = "link"
)
type Builder struct {
Params []string
}
func NewBuilder(token string) *Builder {
return &Builder{[]string{fmt.Sprintf("api=%s", token)}}
}
func (db *Builder) QueryString() string {
return "?" + strings.Join(db.Params, "&")
}
func (db *Builder) Exact(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__exact=%s", field, value))
return db
}
func (db *Builder) IExact(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__iexact=%s", field, value))
return db
}
func (db *Builder) Contains(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__contains=%s", field, value))
return db
}
func (db *Builder) IContains(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__icontains=%s", field, value))
return db
}
func (db *Builder) In(field string, values ...string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__in=[%s]", field, strings.Join(values, ",")))
return db
}
func (db *Builder) Gt(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__gt=%s", field, value))
return db
}
func (db *Builder) Gte(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__gte=%s", field, value))
return db
}
func (db *Builder) Lt(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__lt=%s", field, value))
return db
}
func (db *Builder) Lte(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__lte=%s", field, value))
return db
}
func (db *Builder) Startswith(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__startswith=%s", field, value))
return db
}
func (db *Builder) IStartswith(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__istartswith=%s", field, value))
return db
}
func (db *Builder) Endswith(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__endswith=%s", field, value))
return db
}
func (db *Builder) IEndswith(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__iendswith=%s", field, value))
return db
}
func (db *Builder) Range(field Field, from, to string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__range=(%s,%s)", field, from, to))
return db
}
func (db *Builder) Date(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__date=%s", field, value))
return db
}
func (db *Builder) Year(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__year=%s", field, value))
return db
}
func (db *Builder) Month(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__month=%s", field, value))
return db
}
func (db *Builder) Day(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__day=%s", field, value))
return db
}
func (db *Builder) Week(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__week=%s", field, value))
return db
}
func (db *Builder) WeekDay(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__week_day=%s", field, value))
return db
}
func (db *Builder) Quarter(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__quarter=%s", field, value))
return db
}
func (db *Builder) Time(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__time=%s", field, value))
return db
}
func (db *Builder) Hour(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__hour=%s", field, value))
return db
}
func (db *Builder) Minute(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__minute=%s", field, value))
return db
}
func (db *Builder) Second(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__second=%s", field, value))
return db
}
func (db *Builder) IsNull(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__isnull=%s", field, value))
return db
}
func (db *Builder) Regex(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__regex=r'%s'", field, value))
return db
}
func (db *Builder) IRegex(field Field, value string) *Builder {
db.Params = append(db.Params, fmt.Sprintf("%s__iregex=r'%s'", field, value))
return db
}