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