-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsyracuse.go
65 lines (53 loc) · 1.47 KB
/
syracuse.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
package syracuse
import (
"time"
"github.com/go-toschool/syracuse/citizens"
)
// Citizen represents a user.
type Citizen struct {
ID string `json:"id" db:"id"`
Email string `json:"email" db:"email"`
FullName string `json:"full_name" db:"full_name"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
DeletedAt *time.Time `json:"-" db:"deleted_at"`
}
// Citizens basic method that need to be implemented in order to operate over users.
type Citizens interface {
CitizensGetter
Select() ([]*Citizen, error)
Create(*Citizen) error
Update(*Citizen) error
Delete(*Citizen) error
}
// CitizensGetter getter define behavior to get records.
type CitizensGetter interface {
GetByID(id string) (*Citizen, error)
GetByEmail(email string) (*Citizen, error)
GetByFullname(fullname string) (*Citizen, error)
}
// CitizensQuery represents queries that helps to query users.
type CitizensQuery struct {
ID string
Email string
FullName string
}
// ToProto ...
func (c *Citizen) ToProto() *citizens.Citizen {
return &citizens.Citizen{
Id: c.ID,
FullName: c.FullName,
Email: c.Email,
CreatedAt: c.CreatedAt.UnixNano(),
UpdatedAt: c.UpdatedAt.UnixNano(),
}
}
// FromProto ...
func (c *Citizen) FromProto(cc *citizens.Citizen) *Citizen {
c.ID = cc.Id
c.FullName = cc.FullName
c.Email = cc.Email
c.CreatedAt = time.Unix(cc.CreatedAt, 0)
c.UpdatedAt = time.Unix(cc.UpdatedAt, 0)
return c
}