update(new log, new db client)
This commit is contained in:
1 parent
25b61e7d1d
commit
d5289efddc
11 files changed
+200
-142
No files matched your search
+25
-19
@@ -1,24 +1,23 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/integration-system/isp-lib/database"
|
||||
"isp-system-service/entity"
|
||||
|
||||
"github.com/go-pg/pg"
|
||||
"github.com/go-pg/pg/orm"
|
||||
)
|
||||
|
||||
var (
|
||||
emptyApplication = &entity.Application{}
|
||||
AppRep AppRepository
|
||||
)
|
||||
var emptyApplication = (*entity.Application)(nil)
|
||||
|
||||
type AppRepository struct {
|
||||
DB orm.DB
|
||||
DB orm.DB
|
||||
rxClient *database.RxDbClient
|
||||
}
|
||||
|
||||
func (ar *AppRepository) GetApplications(list []int32) ([]entity.Application, error) {
|
||||
func (r *AppRepository) GetApplications(list []int32) ([]entity.Application, error) {
|
||||
res := make([]entity.Application, 0)
|
||||
q := ar.DB.Model(&res)
|
||||
q := r.getDb().Model(&res)
|
||||
if len(res) > 0 {
|
||||
q = q.Where("id IN (?)", pg.In(list))
|
||||
}
|
||||
@@ -26,44 +25,51 @@ func (ar *AppRepository) GetApplications(list []int32) ([]entity.Application, er
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (ar *AppRepository) GetApplicationsByServiceId(serviceId ...int32) ([]entity.Application, error) {
|
||||
func (r *AppRepository) GetApplicationsByServiceId(serviceId ...int32) ([]entity.Application, error) {
|
||||
res := make([]entity.Application, 0)
|
||||
err := ar.DB.Model(&res).Where("service_id IN (?)", pg.In(serviceId)).Order("created_at DESC").Select()
|
||||
err := r.getDb().Model(&res).Where("service_id IN (?)", pg.In(serviceId)).Order("created_at DESC").Select()
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (ar *AppRepository) CreateApplication(service entity.Application) (entity.Application, error) {
|
||||
_, err := ar.DB.Model(&service).Insert()
|
||||
func (r *AppRepository) CreateApplication(service entity.Application) (entity.Application, error) {
|
||||
_, err := r.getDb().Model(&service).Insert()
|
||||
return service, err
|
||||
}
|
||||
|
||||
func (ar *AppRepository) GetApplicationByNameAndServiceId(name string, serviceId int32) (*entity.Application, error) {
|
||||
func (r *AppRepository) GetApplicationByNameAndServiceId(name string, serviceId int32) (*entity.Application, error) {
|
||||
app := new(entity.Application)
|
||||
err := ar.DB.Model(app).Where("name = ? AND service_id = ?", name, serviceId).First()
|
||||
err := r.getDb().Model(app).Where("name = ? AND service_id = ?", name, serviceId).First()
|
||||
if err == pg.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
return app, err
|
||||
}
|
||||
|
||||
func (ar *AppRepository) UpdateApplication(app entity.Application) (entity.Application, error) {
|
||||
_, err := ar.DB.Model(&app).Column("name", "description").WherePK().Returning("*").Update()
|
||||
func (r *AppRepository) UpdateApplication(app entity.Application) (entity.Application, error) {
|
||||
_, err := r.getDb().Model(&app).Column("name", "description").WherePK().Returning("*").Update()
|
||||
return app, err
|
||||
}
|
||||
|
||||
func (ar *AppRepository) GetApplicationById(id int32) (*entity.Application, error) {
|
||||
func (r *AppRepository) GetApplicationById(id int32) (*entity.Application, error) {
|
||||
app := &entity.Application{Id: id}
|
||||
err := ar.DB.Select(app)
|
||||
err := r.getDb().Select(app)
|
||||
if err == pg.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
return app, err
|
||||
}
|
||||
|
||||
func (ar *AppRepository) DeleteApplications(list []int32) (int, error) {
|
||||
res, err := ar.DB.Model(emptyApplication).Where("id IN (?)", pg.In(list)).Delete()
|
||||
func (r *AppRepository) DeleteApplications(list []int32) (int, error) {
|
||||
res, err := r.getDb().Model(emptyApplication).Where("id IN (?)", pg.In(list)).Delete()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return res.RowsAffected(), nil
|
||||
}
|
||||
|
||||
func (r *AppRepository) getDb() orm.DB {
|
||||
if r.DB != nil {
|
||||
return r.DB
|
||||
}
|
||||
return r.rxClient.Unsafe()
|
||||
}
|
||||
Reference in new issue
Block a user