Require an ORM in all models

This commit is contained in:
Lukas Bachschwell 2018-11-07 20:23:46 +01:00
parent 3347161ae9
commit 2adb24d8ce
5 changed files with 20 additions and 40 deletions

View File

@ -11,7 +11,7 @@ import (
) )
func init() { 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() auth.InitJWTService()
orm.DefaultTimeLoc = time.UTC orm.DefaultTimeLoc = time.UTC
if beego.BConfig.RunMode == "dev" { if beego.BConfig.RunMode == "dev" {

View File

@ -29,16 +29,14 @@ func init() {
// AddCompanyData insert a new CompanyData into database and returns // AddCompanyData insert a new CompanyData into database and returns
// last inserted Id on success. // last inserted Id on success.
func AddCompanyData(m *CompanyData) (id int64, err error) { func AddCompanyData(o orm.Ormer, m *CompanyData) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m) id, err = o.Insert(m)
return return
} }
// GetCompanyDataById retrieves CompanyData by Id. Returns error if // GetCompanyDataById retrieves CompanyData by Id. Returns error if
// Id doesn't exist // Id doesn't exist
func GetCompanyDataById(id int) (v *CompanyData, err error) { func GetCompanyDataById(o orm.Ormer, id int) (v *CompanyData, err error) {
o := orm.NewOrm()
v = &CompanyData{Id: id} v = &CompanyData{Id: id}
if err = o.Read(v); err == nil { if err = o.Read(v); err == nil {
return v, 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 // GetAllCompanyData retrieves all CompanyData matches certain condition. Returns empty list if
// no records exist // 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) { offset int64, limit int64) (ml []interface{}, err error) {
o := orm.NewOrm()
qs := o.QueryTable(new(CompanyData)) qs := o.QueryTable(new(CompanyData))
// query k=v // query k=v
for k, v := range query { 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 // UpdateCompanyData updates CompanyData by Id and returns error if
// the record to be updated doesn't exist // the record to be updated doesn't exist
func UpdateCompanyDataById(m *CompanyData) (err error) { func UpdateCompanyDataById(o orm.Ormer, m *CompanyData) (err error) {
o := orm.NewOrm()
v := CompanyData{Id: m.Id} v := CompanyData{Id: m.Id}
// ascertain id exists in the database // ascertain id exists in the database
if err = o.Read(&v); err == nil { 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 // DeleteCompanyData deletes CompanyData by Id and returns error if
// the record to be deleted doesn't exist // the record to be deleted doesn't exist
func DeleteCompanyData(id int) (err error) { func DeleteCompanyData(o orm.Ormer, id int) (err error) {
o := orm.NewOrm()
v := CompanyData{Id: id} v := CompanyData{Id: id}
// ascertain id exists in the database // ascertain id exists in the database
if err = o.Read(&v); err == nil { if err = o.Read(&v); err == nil {

View File

@ -29,16 +29,14 @@ func init() {
// AddCompanyUser insert a new CompanyUser into database and returns // AddCompanyUser insert a new CompanyUser into database and returns
// last inserted Id on success. // last inserted Id on success.
func AddCompanyUser(m *CompanyUser) (id int64, err error) { func AddCompanyUser(o orm.Ormer, m *CompanyUser) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m) id, err = o.Insert(m)
return return
} }
// GetCompanyUserById retrieves CompanyUser by Id. Returns error if // GetCompanyUserById retrieves CompanyUser by Id. Returns error if
// Id doesn't exist // Id doesn't exist
func GetCompanyUserById(id int) (v *CompanyUser, err error) { func GetCompanyUserById(o orm.Ormer, id int) (v *CompanyUser, err error) {
o := orm.NewOrm()
v = &CompanyUser{Id: id} v = &CompanyUser{Id: id}
if err = o.Read(v); err == nil { if err = o.Read(v); err == nil {
return v, 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 // GetAllCompanyUser retrieves all CompanyUser matches certain condition. Returns empty list if
// no records exist // 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) { offset int64, limit int64) (ml []interface{}, err error) {
o := orm.NewOrm()
qs := o.QueryTable(new(CompanyUser)) qs := o.QueryTable(new(CompanyUser))
// query k=v // query k=v
for k, v := range query { for k, v := range query {
@ -124,10 +121,8 @@ func GetAllCompanyUser(query map[string]string, fields []string, sortby []string
return nil, err return nil, err
} }
// UpdateCompanyUser updates CompanyUser by Id and returns error if // UpdateCompanyUserById updates CompanyUser by Id and returns error if the record to be updated doesn't exist
// the record to be updated doesn't exist func UpdateCompanyUserById(o orm.Ormer, m *CompanyUser) (err error) {
func UpdateCompanyUserById(m *CompanyUser) (err error) {
o := orm.NewOrm()
v := CompanyUser{Id: m.Id} v := CompanyUser{Id: m.Id}
// ascertain id exists in the database // ascertain id exists in the database
if err = o.Read(&v); err == nil { 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 // DeleteCompanyUser deletes CompanyUser by Id and returns error if
// the record to be deleted doesn't exist // the record to be deleted doesn't exist
func DeleteCompanyUser(id int) (err error) { func DeleteCompanyUser(o orm.Ormer, id int) (err error) {
o := orm.NewOrm()
v := CompanyUser{Id: id} v := CompanyUser{Id: id}
// ascertain id exists in the database // ascertain id exists in the database
if err = o.Read(&v); err == nil { if err = o.Read(&v); err == nil {

View File

@ -32,8 +32,7 @@ func init() {
// AddContact insert a new Contact into database and returns // AddContact insert a new Contact into database and returns
// last inserted Id on success. // last inserted Id on success.
func AddContact(m *Contact) (id int64, err error) { func AddContact(o orm.Ormer, m *Contact) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m) id, err = o.Insert(m)
return return
} }
@ -41,7 +40,6 @@ func AddContact(m *Contact) (id int64, err error) {
// GetContactById retrieves Contact by Id. Returns error if // GetContactById retrieves Contact by Id. Returns error if
// Id doesn't exist // Id doesn't exist
func GetContactById(o orm.Ormer, id int) (v *Contact, err error) { func GetContactById(o orm.Ormer, id int) (v *Contact, err error) {
//o := orm.NewOrm()
v = &Contact{Id: id} v = &Contact{Id: id}
if err = o.Read(v); err == nil { if err = o.Read(v); err == nil {
return v, 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 // UpdateContact updates Contact by Id and returns error if
// the record to be updated doesn't exist // the record to be updated doesn't exist
func UpdateContactById(m *Contact) (err error) { func UpdateContactById(o orm.Ormer, m *Contact) (err error) {
o := orm.NewOrm()
v := Contact{Id: m.Id} v := Contact{Id: m.Id}
// ascertain id exists in the database // ascertain id exists in the database
if err = o.Read(&v); err == nil { 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 // DeleteContact deletes Contact by Id and returns error if
// the record to be deleted doesn't exist // the record to be deleted doesn't exist
func DeleteContact(id int) (err error) { func DeleteContact(o orm.Ormer, id int) (err error) {
o := orm.NewOrm()
v := Contact{Id: id} v := Contact{Id: id}
// ascertain id exists in the database // ascertain id exists in the database
if err = o.Read(&v); err == nil { if err = o.Read(&v); err == nil {

View File

@ -29,16 +29,14 @@ func init() {
// AddPost insert a new Post into database and returns // AddPost insert a new Post into database and returns
// last inserted Id on success. // last inserted Id on success.
func AddPost(m *Post) (id int64, err error) { func AddPost(o orm.Ormer, m *Post) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m) id, err = o.Insert(m)
return return
} }
// GetPostById retrieves Post by Id. Returns error if // GetPostById retrieves Post by Id. Returns error if
// Id doesn't exist // Id doesn't exist
func GetPostById(id int) (v *Post, err error) { func GetPostById(o orm.Ormer, id int) (v *Post, err error) {
o := orm.NewOrm()
v = &Post{Id: id} v = &Post{Id: id}
if err = o.Read(v); err == nil { if err = o.Read(v); err == nil {
return v, 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 // GetAllPost retrieves all Post matches certain condition. Returns empty list if
// no records exist // 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) { offset int64, limit int64) (ml []interface{}, err error) {
o := orm.NewOrm()
qs := o.QueryTable(new(Post)) qs := o.QueryTable(new(Post))
// query k=v // query k=v
for k, v := range query { 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 // UpdatePost updates Post by Id and returns error if
// the record to be updated doesn't exist // the record to be updated doesn't exist
func UpdatePostById(m *Post) (err error) { func UpdatePostById(o orm.Ormer, m *Post) (err error) {
o := orm.NewOrm()
v := Post{Id: m.Id} v := Post{Id: m.Id}
// ascertain id exists in the database // ascertain id exists in the database
if err = o.Read(&v); err == nil { 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 // DeletePost deletes Post by Id and returns error if
// the record to be deleted doesn't exist // the record to be deleted doesn't exist
func DeletePost(id int) (err error) { func DeletePost(o orm.Ormer, id int) (err error) {
o := orm.NewOrm()
v := Post{Id: id} v := Post{Id: id}
// ascertain id exists in the database // ascertain id exists in the database
if err = o.Read(&v); err == nil { if err = o.Read(&v); err == nil {