github.com/saadullahsaeed/fragmenta-cms@v1.5.4/src/pages/query.go (about) 1 package pages 2 3 import ( 4 "time" 5 6 "github.com/fragmenta/query" 7 8 "github.com/fragmenta/fragmenta-cms/src/lib/resource" 9 "github.com/fragmenta/fragmenta-cms/src/lib/status" 10 ) 11 12 const ( 13 // TableName is the database table for this resource 14 TableName = "pages" 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", "author_id", "keywords", "name", "status", "summary", "template", "text", "url"} 24 } 25 26 // NewWithColumns creates a new page instance and fills it with data from the database cols provided. 27 func NewWithColumns(cols map[string]interface{}) *Page { 28 29 page := New() 30 page.ID = resource.ValidateInt(cols["id"]) 31 page.CreatedAt = resource.ValidateTime(cols["created_at"]) 32 page.UpdatedAt = resource.ValidateTime(cols["updated_at"]) 33 page.Status = resource.ValidateInt(cols["status"]) 34 page.AuthorID = resource.ValidateInt(cols["author_id"]) 35 page.Keywords = resource.ValidateString(cols["keywords"]) 36 page.Name = resource.ValidateString(cols["name"]) 37 page.Status = resource.ValidateInt(cols["status"]) 38 page.Summary = resource.ValidateString(cols["summary"]) 39 page.Template = resource.ValidateString(cols["template"]) 40 page.Text = resource.ValidateString(cols["text"]) 41 page.URL = resource.ValidateString(cols["url"]) 42 43 return page 44 } 45 46 // New creates and initialises a new page instance. 47 func New() *Page { 48 page := &Page{} 49 page.CreatedAt = time.Now() 50 page.UpdatedAt = time.Now() 51 page.TableName = TableName 52 page.KeyName = KeyName 53 page.Status = status.Draft 54 page.Template = "pages/views/templates/default.html.got" 55 return page 56 } 57 58 // FindFirst fetches a single page record from the database using 59 // a where query with the format and args provided. 60 func FindFirst(format string, args ...interface{}) (*Page, error) { 61 result, err := Query().Where(format, args...).FirstResult() 62 if err != nil { 63 return nil, err 64 } 65 return NewWithColumns(result), nil 66 } 67 68 // Find fetches a single page record from the database by id. 69 func Find(id int64) (*Page, error) { 70 result, err := Query().Where("id=?", id).FirstResult() 71 if err != nil { 72 return nil, err 73 } 74 return NewWithColumns(result), nil 75 } 76 77 // FindAll fetches all page records matching this query from the database. 78 func FindAll(q *query.Query) ([]*Page, error) { 79 80 // Fetch query.Results from query 81 results, err := q.Results() 82 if err != nil { 83 return nil, err 84 } 85 86 // Return an array of pages constructed from the results 87 var pages []*Page 88 for _, cols := range results { 89 p := NewWithColumns(cols) 90 pages = append(pages, p) 91 } 92 93 return pages, nil 94 } 95 96 // Query returns a new query for pages with a default order. 97 func Query() *query.Query { 98 return query.New(TableName, KeyName).Order(Order) 99 } 100 101 // Where returns a new query for pages with the format and arguments supplied. 102 func Where(format string, args ...interface{}) *query.Query { 103 return Query().Where(format, args...) 104 } 105 106 // Published returns a query for all pages with status >= published. 107 func Published() *query.Query { 108 return Query().Where("status>=?", status.Published) 109 }