This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
backend/internal/models/profession.go

111 lines
4.1 KiB
Go
Raw Normal View History

2021-02-21 09:30:46 +00:00
package models
import (
"context"
"time"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
)
type Profession struct {
tableName struct{} `pg:"alias:profession" json:"tableName" xml:"tableName" gqlgen:"tableName"`
ID int `json:"id,omitempty" xml:"id" gqlgen:"id"`
Slug string `json:"slug" pg:",unique" xml:"slug" gqlgen:"slug"`
Name string `json:"name,omitempty" pg:",unique" xml:"name" gqlgen:"name"`
Description string `json:"description,omitempty" pg:",use_zero" xml:"description" gqlgen:"description"`
CreatedAt time.Time `json:"createdAt,omitempty" pg:"default:now()" xml:"createdAt" gqlgen:"createdAt"`
}
func (p *Profession) BeforeInsert(ctx context.Context) (context.Context, error) {
p.CreatedAt = time.Now()
return ctx, nil
}
type ProfessionInput struct {
Name string `json:"name,omitempty" xml:"name" gqlgen:"name"`
Description string `json:"description,omitempty" xml:"description" gqlgen:"description"`
}
type ProfessionFilter struct {
ID []int `gqlgen:"id" json:"id" xml:"id"`
IDNEQ []int `gqlgen:"idNEQ" json:"idNEQ" xml:"idNEQ"`
Slug []string `json:"slug" xml:"slug" gqlgen:"slug"`
SlugNEQ []string `json:"slugNEQ" xml:"slugNEQ" gqlgen:"slugNEQ"`
Name []string `gqlgen:"name" json:"name" xml:"name"`
NameNEQ []string `json:"nameNEQ" xml:"nameNEQ" gqlgen:"nameNEQ"`
NameMATCH string `json:"nameMATCH" xml:"nameMATCH" gqlgen:"nameMATCH"`
NameIEQ string `gqlgen:"nameIEQ" json:"nameIEQ" xml:"nameIEQ"`
DescriptionMATCH string `gqlgen:"descriptionMATCH" json:"descriptionMATCH" xml:"descriptionMATCH"`
DescriptionIEQ string `json:"descriptionIEQ" xml:"descriptionIEQ" gqlgen:"descriptionIEQ"`
CreatedAt time.Time `gqlgen:"createdAt" json:"createdAt" xml:"createdAt"`
CreatedAtGT time.Time `gqlgen:"createdAtGT" json:"createdAtGT" xml:"createdAtGT"`
CreatedAtGTE time.Time `json:"createdAtGTE" xml:"createdAtGTE" gqlgen:"createdAtGTE"`
CreatedAtLT time.Time `gqlgen:"createdAtLT" json:"createdAtLT" xml:"createdAtLT"`
CreatedAtLTE time.Time `json:"createdAtLTE" xml:"createdAtLTE" gqlgen:"createdAtLTE"`
}
func (f *ProfessionFilter) WhereWithAlias(q *orm.Query, alias string) (*orm.Query, error) {
if !isZero(f.ID) {
q = q.Where(buildConditionArray(addAliasToColumnName("id", alias)), pg.Array(f.ID))
}
if !isZero(f.IDNEQ) {
q = q.Where(buildConditionNotInArray(addAliasToColumnName("id", alias)), pg.Array(f.IDNEQ))
}
if !isZero(f.Slug) {
q = q.Where(buildConditionArray(addAliasToColumnName("slug", alias)), pg.Array(f.Slug))
}
if !isZero(f.SlugNEQ) {
q = q.Where(buildConditionNotInArray(addAliasToColumnName("slug", alias)), pg.Array(f.SlugNEQ))
}
if !isZero(f.Name) {
q = q.Where(buildConditionArray(addAliasToColumnName("name", alias)), pg.Array(f.Name))
}
if !isZero(f.NameNEQ) {
q = q.Where(buildConditionNotInArray(addAliasToColumnName("name", alias)), pg.Array(f.NameNEQ))
}
if !isZero(f.NameMATCH) {
q = q.Where(buildConditionMatch(addAliasToColumnName("name", alias)), f.NameMATCH)
}
if !isZero(f.NameIEQ) {
q = q.Where(buildConditionIEQ(addAliasToColumnName("name", alias)), f.NameIEQ)
}
if !isZero(f.DescriptionMATCH) {
q = q.Where(buildConditionMatch(addAliasToColumnName("description", alias)), f.DescriptionMATCH)
}
if !isZero(f.DescriptionIEQ) {
q = q.Where(buildConditionIEQ(addAliasToColumnName("description", alias)), f.DescriptionIEQ)
}
if !isZero(f.CreatedAt) {
q = q.Where(buildConditionEquals(addAliasToColumnName("created_at", alias)), f.CreatedAt)
}
if !isZero(f.CreatedAtGT) {
q = q.Where(buildConditionGT(addAliasToColumnName("created_at", alias)), f.CreatedAtGT)
}
if !isZero(f.CreatedAtGTE) {
q = q.Where(buildConditionGTE(addAliasToColumnName("created_at", alias)), f.CreatedAtGTE)
}
if !isZero(f.CreatedAtLT) {
q = q.Where(buildConditionLT(addAliasToColumnName("created_at", alias)), f.CreatedAtLT)
}
if !isZero(f.CreatedAtLTE) {
q = q.Where(buildConditionLTE(addAliasToColumnName("created_at", alias)), f.CreatedAtLTE)
}
return q, nil
}
func (f *ProfessionFilter) Where(q *orm.Query) (*orm.Query, error) {
return f.WhereWithAlias(q, "profession")
}