/
profileModel.go
72 lines (64 loc) · 1.75 KB
/
profileModel.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
package gormdal
import (
"github.com/gregod-com/grgd/interfaces"
"github.com/gregod-com/grgd/pkg/project"
"gorm.io/gorm"
)
// ProfileModelToIProfile ...
func profileModelToIProfile(in *ProfileModel, out interfaces.IProfile) error {
out.SetID(in.ID)
out.SetName(in.Name)
out.SetMetaData("grgdDir", in.HomeDir)
out.SetMetaData("hackDir", in.HackDir)
found := false
randomID := uint(0)
for _, proj := range in.Projects {
// todo make implementation agnostic?
if in.CurrentProjectID == proj.ID {
found = true
}
randomID = proj.ID
iproj := &project.Project{}
projectModelToIProject(proj, iproj)
out.AddProjectDirect(iproj)
}
if !found {
in.CurrentProjectID = randomID
}
out.SetCurrentProjectID(in.CurrentProjectID)
out.SetInitialized(in.Initialized)
return nil
}
// iprofileToProfileModel ...
func iprofileToProfileModel(in interfaces.IProfile, out *ProfileModel) error {
out.ID = in.GetID()
out.Name = in.GetName()
out.HomeDir = in.GetMetaData("grgdDir")
out.HackDir = in.GetMetaData("hackDir")
out.CurrentProjectID = in.GetCurrentProjectID()
for _, proj := range in.GetProjects() {
pmdl := &ProjectModel{}
iprojectToProjectModel(proj, pmdl)
out.Projects = append(out.Projects, pmdl)
}
out.Initialized = in.IsInitialized()
return nil
}
// ProfileModel ...
type ProfileModel struct {
gorm.Model
Name string `gorm:"unique;not null;default:null"`
HomeDir string
HackDir string
Projects []*ProjectModel `gorm:"constraint:OnUpdate:UPDATE,OnDelete:SET NULL;;many2many:profiles_projects;"`
CurrentProjectID uint
Initialized bool
}
// GetID ...
func (profile *ProfileModel) GetID() uint {
return profile.ID
}
// GetName ...
func (profile *ProfileModel) GetName() string {
return profile.Name
}