-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontact.go
More file actions
73 lines (59 loc) · 1.53 KB
/
Copy pathcontact.go
File metadata and controls
73 lines (59 loc) · 1.53 KB
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
package dbModel
import (
"time"
"github.com/ntorga/clean-ddd-full-stack-go-poc/src/domain/entity"
"github.com/ntorga/clean-ddd-full-stack-go-poc/src/domain/valueObject"
)
type Contact struct {
ID uint `gorm:"primarykey"`
Name string `gorm:"not null"`
Nickname string `gorm:"not null"`
Phone string `gorm:"not null"`
CreatedAt time.Time
UpdatedAt time.Time
}
func (Contact) TableName() string {
return "contacts"
}
func (model Contact) InitialEntries() []interface{} {
initialEntities := entity.Contact{}.InitialEntities()
initialEntries := []interface{}{}
for _, entity := range initialEntities {
entry, _ := model.ToModel(entity)
initialEntries = append(initialEntries, entry)
}
return initialEntries
}
func (Contact) ToModel(entity entity.Contact) (Contact, error) {
return Contact{
ID: uint(entity.Id),
Name: entity.Name.String(),
Nickname: entity.Nickname.String(),
Phone: entity.Phone.String(),
}, nil
}
func (model Contact) ToEntity() (entity.Contact, error) {
var contactEntity entity.Contact
contactId, err := valueObject.NewContactId(model.ID)
if err != nil {
return contactEntity, err
}
name, err := valueObject.NewPersonName(model.Name)
if err != nil {
return contactEntity, err
}
nickname, err := valueObject.NewNickname(model.Nickname)
if err != nil {
return contactEntity, err
}
phone, err := valueObject.NewPhoneNumber(model.Phone)
if err != nil {
return contactEntity, err
}
return entity.NewContact(
contactId,
name,
nickname,
phone,
), nil
}