From cdca62884694f193f55bcd7ac408d2a36aa6042d Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Tue, 18 Oct 2016 20:26:17 +0200 Subject: [PATCH] [host] models relink with foreignkey --- modul/host/apidatabase.go | 4 ++-- modul/host/apidomain.go | 2 +- modul/host/models.go | 33 ++++++++++++++++++++------------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/modul/host/apidatabase.go b/modul/host/apidatabase.go index 37937d3..4d9c4b5 100644 --- a/modul/host/apidatabase.go +++ b/modul/host/apidatabase.go @@ -42,9 +42,9 @@ func databaseList(ctx context.Context, w http.ResponseWriter, r *http.Request) ( logger := log.GetLog(r, "databaselist") var database []*Database if login.Superadmin && r.URL.Query().Get("filter") == "all" { - dbconnection.Find(&database) + dbconnection.Preload("Profil").Preload("Profil.Login").Find(&database) } else { - dbconnection.Where("profil = ?", profil.ID).Find(&database) + dbconnection.Where("profil = ?", profil.ID).Preload("Profil").Find(&database) } logger.Info("done") returndata = database diff --git a/modul/host/apidomain.go b/modul/host/apidomain.go index eb4e637..ce1fa05 100644 --- a/modul/host/apidomain.go +++ b/modul/host/apidomain.go @@ -42,7 +42,7 @@ func domainList(ctx context.Context, w http.ResponseWriter, r *http.Request) (re logger := log.GetLog(r, "domainlist") var domain []*Domain if login.Superadmin && r.URL.Query().Get("filter") == "all" { - dbconnection.Find(&domain) + dbconnection.Preload("Profil").Preload("Profil.Login").Find(&domain) } else { dbconnection.Where("profil = ?", profil.ID).Find(&domain) } diff --git a/modul/host/models.go b/modul/host/models.go index 7eb8292..551f7b8 100644 --- a/modul/host/models.go +++ b/modul/host/models.go @@ -2,13 +2,16 @@ package host import ( "github.com/jinzhu/gorm" + + "dev.sum7.eu/sum7/warehost/system" ) // Profil struct type Profil struct { ID int64 - LoginID int64 `sql:"type:bigint NOT NULL UNIQUE REFERENCES login(id) ON UPDATE CASCADE ON DELETE CASCADE;column:login" json:"login"` - Reseller bool `sql:"default:false;column:reseller" json:"reseller"` + LoginID int64 `sql:"type:bigint NOT NULL UNIQUE REFERENCES login(id) ON UPDATE CASCADE ON DELETE CASCADE;column:login" json:"-"` + Login *system.Login `gorm:"foreignkey:Login;" json:"login"` + Reseller bool `sql:"default:false;column:reseller" json:"reseller"` } // TableName of struct @@ -17,12 +20,13 @@ func (Profil) TableName() string { return "host_profil" } // Domain struct type Domain struct { ID int64 - ProfilID int64 `sql:"type:bigint NOT NULL REFERENCES host_profil(id) ON UPDATE CASCADE ON DELETE CASCADE;column:profil" json:"profil"` - FQDN string `sql:"type:varchar(255) NOT NULL UNIQUE;column:fqdn" json:"fqdn"` - Code string `sql:"type:varchar(255);column:code" json:"-"` - Active bool `sql:"default:false;column:active" json:"active"` - Mail bool `sql:"default:false;column:mail" json:"mail"` - Web bool `sql:"default:false;column:web" json:"web"` + ProfilID int64 `sql:"type:bigint NOT NULL REFERENCES host_profil(id) ON UPDATE CASCADE ON DELETE CASCADE;column:profil" json:"-"` + Profil *Profil `gorm:"foreignkey:Profil;" json:"profil"` + FQDN string `sql:"type:varchar(255) NOT NULL UNIQUE;column:fqdn" json:"fqdn"` + Code string `sql:"type:varchar(255);column:code" json:"-"` + Active bool `sql:"default:false;column:active" json:"active"` + Mail bool `sql:"default:false;column:mail" json:"mail"` + Web bool `sql:"default:false;column:web" json:"web"` } // TableName of struct @@ -31,7 +35,8 @@ func (Domain) TableName() string { return "host_domain" } // Web struct type Web struct { ID int64 - DomainID int64 `sql:"type:bigint NOT NULL REFERENCES host_domain(id) ON UPDATE CASCADE ON DELETE CASCADE;column:domain" json:"domain"` + DomainID int64 `sql:"type:bigint NOT NULL REFERENCES host_domain(id) ON UPDATE CASCADE ON DELETE CASCADE;column:domain" json:"-"` + Domain *Domain `gorm:"foreignkey:Domain;" json:"domain"` Subdomain string `sql:"type:varchar(255);column:subdomain" json:"subdomain"` PHP bool `sql:"default:false;column:php" json:"php"` SSL bool `sql:"default:true;column:ssl" json:"ssl"` @@ -48,7 +53,8 @@ func (Web) TableName() string { return "host_web" } // Mail struct type Mail struct { ID int64 - DomainID int64 `sql:"type:bigint NOT NULL REFERENCES host_domain(id) ON UPDATE CASCADE ON DELETE CASCADE;column:domain" json:"domain"` + DomainID int64 `sql:"type:bigint NOT NULL REFERENCES host_domain(id) ON UPDATE CASCADE ON DELETE CASCADE;column:domain" json:"-"` + Domain *Domain `gorm:"foreignkey:Domain;" json:"domain"` Name string `sql:"type:varchar(255);column:name" json:"name"` Forward []string `sql:"type:varchar(255)[];column:forward" json:"forward"` LoginID int64 `sql:"type:bigint NOT NULL REFERENCES login(id) ON UPDATE CASCADE ON DELETE CASCADE;column:login" json:"login"` @@ -60,9 +66,10 @@ func (Mail) TableName() string { return "host_mail" } // Database struct type Database struct { ID int64 - ProfilID int64 `sql:"type:bigint NOT NULL REFERENCES host_profil(id) ON UPDATE CASCADE ON DELETE CASCADE;column:profil" json:"profil"` - Password string `sql:"type:varchar(255);column:password" json:"password"` - Comment string `sql:"type:varchar(255);column:comment" json:"comment"` + ProfilID int64 `sql:"type:bigint NOT NULL REFERENCES host_profil(id) ON UPDATE CASCADE ON DELETE CASCADE;column:profil" json:"-"` + Profil *Profil `gorm:"foreignkey:Profil;" json:"profil"` + Password string `sql:"type:varchar(255);column:password" json:"password"` + Comment string `sql:"type:varchar(255);column:comment" json:"comment"` } // TableName of struct