github.com/robryk/drone@v0.2.1-0.20140602202253-40fe4305815d/pkg/model/team.go (about)

     1  package model
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"time"
     7  )
     8  
     9  // ErrInvalidTeamName is returned by the Team validation function
    10  // when a team name is invalid.
    11  var ErrInvalidTeamName = errors.New("Invalid Team Name")
    12  
    13  type Team struct {
    14  	ID       int64     `meddler:"id,pk"           json:"id"`
    15  	Slug     string    `meddler:"slug"            json:"slug"`
    16  	Name     string    `meddler:"name"            json:"name"`
    17  	Email    string    `meddler:"email"           json:"email"`
    18  	Gravatar string    `meddler:"gravatar"        json:"gravatar"`
    19  	Created  time.Time `meddler:"created,utctime" json:"created"`
    20  	Updated  time.Time `meddler:"updated,utctime" json:"updated"`
    21  }
    22  
    23  // Creates a new team with the specified email address,
    24  // and team name.
    25  func NewTeam(name, email string) *Team {
    26  	team := Team{}
    27  	team.SetEmail(email)
    28  	team.SetName(name)
    29  	return &team
    30  }
    31  
    32  // Returns the Gravatar Image URL.
    33  func (t *Team) Image() string      { return fmt.Sprintf(GravatarPattern, t.Gravatar, 42) }
    34  func (t *Team) ImageSmall() string { return fmt.Sprintf(GravatarPattern, t.Gravatar, 32) }
    35  func (t *Team) ImageLarge() string { return fmt.Sprintf(GravatarPattern, t.Gravatar, 160) }
    36  
    37  // Set the name and calculate the slug value.
    38  func (t *Team) SetName(name string) {
    39  	t.Name = name
    40  	t.Slug = createSlug(name)
    41  }
    42  
    43  // Set the email address and calculate the
    44  // Gravatar hash.
    45  func (t *Team) SetEmail(email string) {
    46  	t.Email = email
    47  	t.Gravatar = createGravatar(email)
    48  }
    49  
    50  // ValidatePassword will compares the supplied password to
    51  // the user password stored in the database.
    52  func (t *Team) Validate() error {
    53  	switch {
    54  	case len(t.Slug) == 0:
    55  		return ErrInvalidTeamName
    56  	case len(t.Slug) >= 255:
    57  		return ErrInvalidTeamName
    58  	case len(t.Email) == 0:
    59  		return ErrInvalidEmail
    60  	case len(t.Email) >= 255:
    61  		return ErrInvalidEmail
    62  	case RegexpEmail.MatchString(t.Email) == false:
    63  		return ErrInvalidEmail
    64  	default:
    65  		return nil
    66  	}
    67  }