github.com/bitcubate/cryptojournal@v1.2.5-0.20171102134152-f578b3d788ab/src/users/query.go (about)

     1  package users
     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 = "users"
    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 acceptable params in update
    22  func AllowedParams() []string {
    23  	return []string{"name", "summary", "email", "text", "title", "password_hash"}
    24  }
    25  
    26  // NewWithColumns creates a new user instance and fills it with data from the database cols provided.
    27  func NewWithColumns(cols map[string]interface{}) *User {
    28  
    29  	user := New()
    30  	user.ID = resource.ValidateInt(cols["id"])
    31  	user.CreatedAt = resource.ValidateTime(cols["created_at"])
    32  	user.UpdatedAt = resource.ValidateTime(cols["updated_at"])
    33  	user.Status = resource.ValidateInt(cols["status"])
    34  	user.Email = resource.ValidateString(cols["email"])
    35  	user.Name = resource.ValidateString(cols["name"])
    36  	user.PasswordHash = resource.ValidateString(cols["password_hash"])
    37  	user.Points = resource.ValidateInt(cols["points"])
    38  	user.Role = resource.ValidateInt(cols["role"])
    39  	user.Summary = resource.ValidateString(cols["summary"])
    40  	user.Text = resource.ValidateString(cols["text"])
    41  	user.Title = resource.ValidateString(cols["title"])
    42  
    43  	return user
    44  }
    45  
    46  // New creates and initialises a new user instance.
    47  func New() *User {
    48  	user := &User{}
    49  	user.CreatedAt = time.Now()
    50  	user.UpdatedAt = time.Now()
    51  	user.TableName = TableName
    52  	user.KeyName = KeyName
    53  	user.Status = status.Draft
    54  	return user
    55  }
    56  
    57  // FindFirst fetches a single user record from the database using
    58  // a where query with the format and args provided.
    59  func FindFirst(format string, args ...interface{}) (*User, error) {
    60  	result, err := Query().Where(format, args...).FirstResult()
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	return NewWithColumns(result), nil
    65  }
    66  
    67  // Find fetches a single user record from the database by id.
    68  func Find(id int64) (*User, error) {
    69  	result, err := Query().Where("id=?", id).FirstResult()
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	return NewWithColumns(result), nil
    74  }
    75  
    76  // FindAll fetches all user records matching this query from the database.
    77  func FindAll(q *query.Query) ([]*User, error) {
    78  
    79  	// Fetch query.Results from query
    80  	results, err := q.Results()
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	// Return an array of users constructed from the results
    86  	var users []*User
    87  	for _, cols := range results {
    88  		p := NewWithColumns(cols)
    89  		users = append(users, p)
    90  	}
    91  
    92  	return users, nil
    93  }
    94  
    95  // Query returns a new query for users with a default order.
    96  func Query() *query.Query {
    97  	return query.New(TableName, KeyName).Order(Order)
    98  }
    99  
   100  // Where returns a new query for users with the format and arguments supplied.
   101  func Where(format string, args ...interface{}) *query.Query {
   102  	return Query().Where(format, args...)
   103  }
   104  
   105  // Published returns a query for all users with status >= published.
   106  func Published() *query.Query {
   107  	return Query().Where("status>=?", status.Published)
   108  }