github.com/bitcubate/cryptojournal@v1.2.5-0.20171102134152-f578b3d788ab/src/stories/query.go (about) 1 package stories 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 = "stories" 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 the cols editable by everyone 22 func AllowedParams() []string { 23 return []string{"name", "summary", "url"} 24 } 25 26 // AllowedParamsAdmin returns the cols editable by admins 27 func AllowedParamsAdmin() []string { 28 return []string{"status", "comment_count", "name", "points", "rank", "summary", "url", "user_id", "user_name"} 29 } 30 31 // NewWithColumns creates a new story instance and fills it with data from the database cols provided. 32 func NewWithColumns(cols map[string]interface{}) *Story { 33 34 story := New() 35 story.ID = resource.ValidateInt(cols["id"]) 36 story.CreatedAt = resource.ValidateTime(cols["created_at"]) 37 story.UpdatedAt = resource.ValidateTime(cols["updated_at"]) 38 story.Status = resource.ValidateInt(cols["status"]) 39 story.CommentCount = resource.ValidateInt(cols["comment_count"]) 40 story.Name = resource.ValidateString(cols["name"]) 41 story.Points = resource.ValidateInt(cols["points"]) 42 story.Rank = resource.ValidateInt(cols["rank"]) 43 story.Summary = resource.ValidateString(cols["summary"]) 44 story.URL = resource.ValidateString(cols["url"]) 45 story.UserID = resource.ValidateInt(cols["user_id"]) 46 story.UserName = resource.ValidateString(cols["user_name"]) 47 48 return story 49 } 50 51 // New creates and initialises a new story instance. 52 func New() *Story { 53 story := &Story{} 54 story.CreatedAt = time.Now() 55 story.UpdatedAt = time.Now() 56 story.TableName = TableName 57 story.KeyName = KeyName 58 story.Status = status.Draft 59 return story 60 } 61 62 // FindFirst fetches a single story record from the database using 63 // a where query with the format and args provided. 64 func FindFirst(format string, args ...interface{}) (*Story, error) { 65 result, err := Query().Where(format, args...).FirstResult() 66 if err != nil { 67 return nil, err 68 } 69 return NewWithColumns(result), nil 70 } 71 72 // Find fetches a single story record from the database by id. 73 func Find(id int64) (*Story, error) { 74 result, err := Query().Where("id=?", id).FirstResult() 75 if err != nil { 76 return nil, err 77 } 78 return NewWithColumns(result), nil 79 } 80 81 // FindAll fetches all story records matching this query from the database. 82 func FindAll(q *query.Query) ([]*Story, error) { 83 84 // Fetch query.Results from query 85 results, err := q.Results() 86 if err != nil { 87 return nil, err 88 } 89 90 // Return an array of stories constructed from the results 91 var stories []*Story 92 for _, cols := range results { 93 p := NewWithColumns(cols) 94 stories = append(stories, p) 95 } 96 97 return stories, nil 98 } 99 100 // Popular returns a query for all stories with points over a certain threshold 101 func Popular() *query.Query { 102 return Query().Where("points > 2") 103 } 104 105 // Query returns a new query for stories with a default order. 106 func Query() *query.Query { 107 return query.New(TableName, KeyName).Order(Order) 108 } 109 110 // Where returns a new query for stories with the format and arguments supplied. 111 func Where(format string, args ...interface{}) *query.Query { 112 return Query().Where(format, args...) 113 } 114 115 // Published returns a query for all stories with status >= published. 116 func Published() *query.Query { 117 return Query().Where("status>=?", status.Published) 118 }