github.com/bitcubate/cryptojournal@v1.2.5-0.20171102134152-f578b3d788ab/src/lib/templates/fragmenta_resources/query.go.tmpl (about) 1 package [[ .fragmenta_resources ]] 2 3 import ( 4 "time" 5 6 "github.com/fragmenta/query" 7 8 "github.com/bitcubate/cryptojournal/src/lib/resource" 9 "github.com/bitcubate/cryptojournal/src/lib/status" 10 ) 11 12 const ( 13 // TableName is the database table for this resource 14 TableName = "[[ .fragmenta_resources ]]" 15 // KeyName is the primary key value for this resource 16 KeyName = "id" 17 // Order defines the default sort order in sql for this resource 18 Order = "name asc, id desc" 19 ) 20 21 // AllowedParams returns an array of allowed param keys for Update and Create. 22 func AllowedParams() []string { 23 return []string{"status", [[ .fragmenta_columns ]]} 24 } 25 26 // NewWithColumns creates a new [[ .fragmenta_resource ]] instance and fills it with data from the database cols provided. 27 func NewWithColumns(cols map[string]interface{}) *[[ .Fragmenta_Resource ]] { 28 29 [[ .fragmenta_resource ]] := New() 30 [[ .fragmenta_resource ]].ID = resource.ValidateInt(cols["id"]) 31 [[ .fragmenta_resource ]].CreatedAt = resource.ValidateTime(cols["created_at"]) 32 [[ .fragmenta_resource ]].UpdatedAt = resource.ValidateTime(cols["updated_at"]) 33 [[ .fragmenta_resource ]].Status = resource.ValidateInt(cols["status"]) 34 [[ .fragmenta_new_fields ]] 35 36 return [[ .fragmenta_resource ]] 37 } 38 39 // New creates and initialises a new [[ .fragmenta_resource ]] instance. 40 func New() *[[ .Fragmenta_Resource ]] { 41 [[ .fragmenta_resource ]] := &[[ .Fragmenta_Resource ]]{} 42 [[ .fragmenta_resource ]].CreatedAt = time.Now() 43 [[ .fragmenta_resource ]].UpdatedAt = time.Now() 44 [[ .fragmenta_resource ]].TableName = TableName 45 [[ .fragmenta_resource ]].KeyName = KeyName 46 [[ .fragmenta_resource ]].Status = status.Draft 47 return [[ .fragmenta_resource ]] 48 } 49 50 // FindFirst fetches a single [[ .fragmenta_resource ]] record from the database using 51 // a where query with the format and args provided. 52 func FindFirst(format string, args ...interface{}) (*[[ .Fragmenta_Resource ]], error) { 53 result, err := Query().Where(format, args...).FirstResult() 54 if err != nil { 55 return nil, err 56 } 57 return NewWithColumns(result), nil 58 } 59 60 // Find fetches a single [[ .fragmenta_resource ]] record from the database by id. 61 func Find(id int64) (*[[ .Fragmenta_Resource ]], error) { 62 result, err := Query().Where("id=?", id).FirstResult() 63 if err != nil { 64 return nil, err 65 } 66 return NewWithColumns(result), nil 67 } 68 69 // FindAll fetches all [[ .fragmenta_resource ]] records matching this query from the database. 70 func FindAll(q *query.Query) ([]*[[ .Fragmenta_Resource ]], error) { 71 72 // Fetch query.Results from query 73 results, err := q.Results() 74 if err != nil { 75 return nil, err 76 } 77 78 // Return an array of [[ .fragmenta_resources ]] constructed from the results 79 var [[ .fragmenta_resources ]] []*[[ .Fragmenta_Resource ]] 80 for _, cols := range results { 81 p := NewWithColumns(cols) 82 [[ .fragmenta_resources ]] = append([[ .fragmenta_resources ]], p) 83 } 84 85 return [[ .fragmenta_resources ]], nil 86 } 87 88 // Query returns a new query for [[ .fragmenta_resources ]] with a default order. 89 func Query() *query.Query { 90 return query.New(TableName, KeyName).Order(Order) 91 } 92 93 // Where returns a new query for [[ .fragmenta_resources ]] with the format and arguments supplied. 94 func Where(format string, args ...interface{}) *query.Query { 95 return Query().Where(format, args...) 96 } 97 98 // Published returns a query for all [[ .fragmenta_resources ]] with status >= published. 99 func Published() *query.Query { 100 return Query().Where("status>=?", status.Published) 101 }