github.com/lenfree/buffalo@v0.7.3-0.20170207163156-891616ea4064/examples/html-crud/models/user.go (about)

     1  package models
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/markbates/pop"
     9  	"github.com/markbates/validate"
    10  	"github.com/markbates/validate/validators"
    11  )
    12  
    13  // User model stores information about a user account
    14  type User struct {
    15  	ID        int       `json:"id" db:"id" schema:"-"`
    16  	CreatedAt time.Time `json:"created_at" db:"created_at"`
    17  	UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
    18  	FirstName string    `json:"first_name" db:"first_name"`
    19  	LastName  string    `json:"last_name" db:"last_name"`
    20  	Email     string    `json:"email" db:"email"`
    21  }
    22  
    23  // String is not required by pop and may be deleted
    24  func (u User) String() string {
    25  	b, _ := json.Marshal(u)
    26  	return string(b)
    27  }
    28  
    29  // FullName returns the first name and last name, separated by a space
    30  func (u User) FullName() string {
    31  	return fmt.Sprintf("%s %s", u.FirstName, u.LastName)
    32  }
    33  
    34  // ValidateNew validates the User fields and checks whether a user has
    35  // already claimed that email address
    36  func (u *User) ValidateNew(tx *pop.Connection) (*validate.Errors, error) {
    37  	verrs, err := u.validateCommon(tx)
    38  	verrs.Append(validate.Validate(
    39  		&validators.FuncValidator{
    40  			Fn: func() bool {
    41  				var b bool
    42  				if u.Email != "" {
    43  					b, err = tx.Where("email = ?", u.Email).Exists(u)
    44  				}
    45  				return !b
    46  			},
    47  			Field:   "Email",
    48  			Message: "%s was already taken.",
    49  		},
    50  	))
    51  	return verrs, err
    52  }
    53  
    54  // ValidateUpdate validates the User fields and confirms that the email has
    55  // not been claimed by a different user
    56  func (u *User) ValidateUpdate(tx *pop.Connection) (*validate.Errors, error) {
    57  	verrs, err := u.validateCommon(tx)
    58  	verrs.Append(validate.Validate(
    59  		&validators.FuncValidator{
    60  			Fn: func() bool {
    61  				var b bool
    62  				if u.Email != "" {
    63  					// Differs from ValidateNew here - only check whether *other* users claimed email
    64  					b, err = tx.Where("email = ? and id != ?", u.Email, u.ID).Exists(u)
    65  				}
    66  				return !b
    67  			},
    68  			Field:   "Email",
    69  			Message: "%s was already taken.",
    70  		},
    71  	))
    72  	return verrs, err
    73  }
    74  
    75  func (u *User) validateCommon(tx *pop.Connection) (*validate.Errors, error) {
    76  	verrs := validate.Validate(
    77  		&validators.StringIsPresent{Name: "First Name", Field: u.FirstName},
    78  		&validators.StringIsPresent{Name: "Last Name", Field: u.LastName},
    79  		&validators.StringIsPresent{Name: "Email", Field: u.Email},
    80  	)
    81  	return verrs, nil
    82  }
    83  
    84  // Users is not required by pop and may be deleted
    85  type Users []User
    86  
    87  // String is not required by pop and may be deleted
    88  func (u Users) String() string {
    89  	b, _ := json.Marshal(u)
    90  	return string(b)
    91  }