From 2adb24d8cee253269785507ef713f9574bea7c7b Mon Sep 17 00:00:00 2001 From: Lukas Bachschwell Date: Wed, 7 Nov 2018 20:23:46 +0100 Subject: [PATCH] Require an ORM in all models --- main.go | 2 +- models/companyData.go | 15 +++++---------- models/companyUser.go | 18 ++++++------------ models/contact.go | 10 +++------- models/post.go | 15 +++++---------- 5 files changed, 20 insertions(+), 40 deletions(-) diff --git a/main.go b/main.go index 10db060..852b79b 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,7 @@ import ( ) func init() { - orm.RegisterDataBase("default", "postgres", "host=127.0.0.1 port=5435 user=postgres password=postgre dbname=company_template sslmode=disable") + orm.RegisterDataBase("default", "postgres", "host=127.0.0.1 port=5435 user=postgres password=postgre sslmode=disable") auth.InitJWTService() orm.DefaultTimeLoc = time.UTC if beego.BConfig.RunMode == "dev" { diff --git a/models/companyData.go b/models/companyData.go index cef30b0..4aeea47 100644 --- a/models/companyData.go +++ b/models/companyData.go @@ -29,16 +29,14 @@ func init() { // AddCompanyData insert a new CompanyData into database and returns // last inserted Id on success. -func AddCompanyData(m *CompanyData) (id int64, err error) { - o := orm.NewOrm() +func AddCompanyData(o orm.Ormer, m *CompanyData) (id int64, err error) { id, err = o.Insert(m) return } // GetCompanyDataById retrieves CompanyData by Id. Returns error if // Id doesn't exist -func GetCompanyDataById(id int) (v *CompanyData, err error) { - o := orm.NewOrm() +func GetCompanyDataById(o orm.Ormer, id int) (v *CompanyData, err error) { v = &CompanyData{Id: id} if err = o.Read(v); err == nil { return v, nil @@ -48,9 +46,8 @@ func GetCompanyDataById(id int) (v *CompanyData, err error) { // GetAllCompanyData retrieves all CompanyData matches certain condition. Returns empty list if // no records exist -func GetAllCompanyData(query map[string]string, fields []string, sortby []string, order []string, +func GetAllCompanyData(o orm.Ormer, query map[string]string, fields []string, sortby []string, order []string, offset int64, limit int64) (ml []interface{}, err error) { - o := orm.NewOrm() qs := o.QueryTable(new(CompanyData)) // query k=v for k, v := range query { @@ -126,8 +123,7 @@ func GetAllCompanyData(query map[string]string, fields []string, sortby []string // UpdateCompanyData updates CompanyData by Id and returns error if // the record to be updated doesn't exist -func UpdateCompanyDataById(m *CompanyData) (err error) { - o := orm.NewOrm() +func UpdateCompanyDataById(o orm.Ormer, m *CompanyData) (err error) { v := CompanyData{Id: m.Id} // ascertain id exists in the database if err = o.Read(&v); err == nil { @@ -141,8 +137,7 @@ func UpdateCompanyDataById(m *CompanyData) (err error) { // DeleteCompanyData deletes CompanyData by Id and returns error if // the record to be deleted doesn't exist -func DeleteCompanyData(id int) (err error) { - o := orm.NewOrm() +func DeleteCompanyData(o orm.Ormer, id int) (err error) { v := CompanyData{Id: id} // ascertain id exists in the database if err = o.Read(&v); err == nil { diff --git a/models/companyUser.go b/models/companyUser.go index c5eaaca..e8eebc5 100644 --- a/models/companyUser.go +++ b/models/companyUser.go @@ -29,16 +29,14 @@ func init() { // AddCompanyUser insert a new CompanyUser into database and returns // last inserted Id on success. -func AddCompanyUser(m *CompanyUser) (id int64, err error) { - o := orm.NewOrm() +func AddCompanyUser(o orm.Ormer, m *CompanyUser) (id int64, err error) { id, err = o.Insert(m) return } // GetCompanyUserById retrieves CompanyUser by Id. Returns error if // Id doesn't exist -func GetCompanyUserById(id int) (v *CompanyUser, err error) { - o := orm.NewOrm() +func GetCompanyUserById(o orm.Ormer, id int) (v *CompanyUser, err error) { v = &CompanyUser{Id: id} if err = o.Read(v); err == nil { return v, nil @@ -48,9 +46,8 @@ func GetCompanyUserById(id int) (v *CompanyUser, err error) { // GetAllCompanyUser retrieves all CompanyUser matches certain condition. Returns empty list if // no records exist -func GetAllCompanyUser(query map[string]string, fields []string, sortby []string, order []string, +func GetAllCompanyUser(o orm.Ormer, query map[string]string, fields []string, sortby []string, order []string, offset int64, limit int64) (ml []interface{}, err error) { - o := orm.NewOrm() qs := o.QueryTable(new(CompanyUser)) // query k=v for k, v := range query { @@ -124,10 +121,8 @@ func GetAllCompanyUser(query map[string]string, fields []string, sortby []string return nil, err } -// UpdateCompanyUser updates CompanyUser by Id and returns error if -// the record to be updated doesn't exist -func UpdateCompanyUserById(m *CompanyUser) (err error) { - o := orm.NewOrm() +// UpdateCompanyUserById updates CompanyUser by Id and returns error if the record to be updated doesn't exist +func UpdateCompanyUserById(o orm.Ormer, m *CompanyUser) (err error) { v := CompanyUser{Id: m.Id} // ascertain id exists in the database if err = o.Read(&v); err == nil { @@ -141,8 +136,7 @@ func UpdateCompanyUserById(m *CompanyUser) (err error) { // DeleteCompanyUser deletes CompanyUser by Id and returns error if // the record to be deleted doesn't exist -func DeleteCompanyUser(id int) (err error) { - o := orm.NewOrm() +func DeleteCompanyUser(o orm.Ormer, id int) (err error) { v := CompanyUser{Id: id} // ascertain id exists in the database if err = o.Read(&v); err == nil { diff --git a/models/contact.go b/models/contact.go index 150ed21..814ebcd 100644 --- a/models/contact.go +++ b/models/contact.go @@ -32,8 +32,7 @@ func init() { // AddContact insert a new Contact into database and returns // last inserted Id on success. -func AddContact(m *Contact) (id int64, err error) { - o := orm.NewOrm() +func AddContact(o orm.Ormer, m *Contact) (id int64, err error) { id, err = o.Insert(m) return } @@ -41,7 +40,6 @@ func AddContact(m *Contact) (id int64, err error) { // GetContactById retrieves Contact by Id. Returns error if // Id doesn't exist func GetContactById(o orm.Ormer, id int) (v *Contact, err error) { - //o := orm.NewOrm() v = &Contact{Id: id} if err = o.Read(v); err == nil { return v, nil @@ -128,8 +126,7 @@ func GetAllContact(o orm.Ormer, query map[string]string, fields []string, sortby // UpdateContact updates Contact by Id and returns error if // the record to be updated doesn't exist -func UpdateContactById(m *Contact) (err error) { - o := orm.NewOrm() +func UpdateContactById(o orm.Ormer, m *Contact) (err error) { v := Contact{Id: m.Id} // ascertain id exists in the database if err = o.Read(&v); err == nil { @@ -143,8 +140,7 @@ func UpdateContactById(m *Contact) (err error) { // DeleteContact deletes Contact by Id and returns error if // the record to be deleted doesn't exist -func DeleteContact(id int) (err error) { - o := orm.NewOrm() +func DeleteContact(o orm.Ormer, id int) (err error) { v := Contact{Id: id} // ascertain id exists in the database if err = o.Read(&v); err == nil { diff --git a/models/post.go b/models/post.go index 575f4bf..498ed17 100644 --- a/models/post.go +++ b/models/post.go @@ -29,16 +29,14 @@ func init() { // AddPost insert a new Post into database and returns // last inserted Id on success. -func AddPost(m *Post) (id int64, err error) { - o := orm.NewOrm() +func AddPost(o orm.Ormer, m *Post) (id int64, err error) { id, err = o.Insert(m) return } // GetPostById retrieves Post by Id. Returns error if // Id doesn't exist -func GetPostById(id int) (v *Post, err error) { - o := orm.NewOrm() +func GetPostById(o orm.Ormer, id int) (v *Post, err error) { v = &Post{Id: id} if err = o.Read(v); err == nil { return v, nil @@ -48,9 +46,8 @@ func GetPostById(id int) (v *Post, err error) { // GetAllPost retrieves all Post matches certain condition. Returns empty list if // no records exist -func GetAllPost(query map[string]string, fields []string, sortby []string, order []string, +func GetAllPost(o orm.Ormer, query map[string]string, fields []string, sortby []string, order []string, offset int64, limit int64) (ml []interface{}, err error) { - o := orm.NewOrm() qs := o.QueryTable(new(Post)) // query k=v for k, v := range query { @@ -126,8 +123,7 @@ func GetAllPost(query map[string]string, fields []string, sortby []string, order // UpdatePost updates Post by Id and returns error if // the record to be updated doesn't exist -func UpdatePostById(m *Post) (err error) { - o := orm.NewOrm() +func UpdatePostById(o orm.Ormer, m *Post) (err error) { v := Post{Id: m.Id} // ascertain id exists in the database if err = o.Read(&v); err == nil { @@ -141,8 +137,7 @@ func UpdatePostById(m *Post) (err error) { // DeletePost deletes Post by Id and returns error if // the record to be deleted doesn't exist -func DeletePost(id int) (err error) { - o := orm.NewOrm() +func DeletePost(o orm.Ormer, id int) (err error) { v := Post{Id: id} // ascertain id exists in the database if err = o.Read(&v); err == nil {