/
person.go
85 lines (73 loc) · 1.94 KB
/
person.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2012 GAEGo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package user
import (
"appengine"
"appengine/datastore"
"errors"
dserrors "github.com/gaego/ds/errors"
"github.com/gaego/user/email"
"github.com/gaego/person"
)
var (
ErrEmailInUse = errors.New("user: email in use")
)
func validatePerson(p *person.Person) (err error) {
// Ensure that the email is an actually email.
if err = email.Validate(p.Email); err != nil {
return
}
return
}
// CreateFromPerson Creates a User from a Person object
func CreateFromPerson(c appengine.Context, p *person.Person) (u *User, err error) {
if err = validatePerson(p); err != nil {
return
}
// Transaction Action
err = datastore.RunInTransaction(c, func(c appengine.Context) error {
// Get the email
e, err := email.Get(c, p.Email)
// An error that is not an ErrNoSuchEntity indicates an an internal error
// and it should be returned.
if err != nil && err != dserrors.ErrNoSuchEntity {
return err
}
// Lack of an error indicates that the email existing in the ds.
if err == nil {
return ErrEmailInUse
}
// Create a new User
u = New()
u.Person = p
u.Email = p.Email
if err = u.Put(c); err != nil {
return err
}
// Update the Email with UserID.
e.UserID = u.Key.StringID()
return e.Put(c)
// XG transation
}, &datastore.TransactionOptions{XG: true})
return u, err
}
func UpdateFromPerson(c appengine.Context, p *person.Person) (u *User, err error) {
if err = validatePerson(p); err != nil {
return
}
// Transaction Action
err = datastore.RunInTransaction(c, func(c appengine.Context) error {
// Get the user
u, err = Get(c, p.ID)
if err != nil {
return err
}
u.Person = p
// TODO more care needs to be taken when changing emails.
u.Email = p.Email
return u.Put(c)
// XG transation
}, &datastore.TransactionOptions{XG: false})
return u, err
}