MSP-623 reimplement application repository
This commit is contained in:
1 parent
0f4c57cb67
commit
a7c58f86ec
2 files changed
+155
-83
No files matched your search
@@ -1,83 +0,0 @@
|
|||||||
package model
|
|
||||||
|
|
||||||
import (
|
|
||||||
"isp-system-service/entity"
|
|
||||||
|
|
||||||
"github.com/go-pg/pg/v9"
|
|
||||||
"github.com/go-pg/pg/v9/orm"
|
|
||||||
"github.com/integration-system/isp-lib/v2/database"
|
|
||||||
)
|
|
||||||
|
|
||||||
var emptyApplication = (*entity.Application)(nil)
|
|
||||||
|
|
||||||
type AppRepository struct {
|
|
||||||
DB orm.DB
|
|
||||||
rxClient *database.RxDbClient
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *AppRepository) GetApplications(list []int32) ([]entity.Application, error) {
|
|
||||||
res := make([]entity.Application, 0)
|
|
||||||
q := r.getDb().Model(&res)
|
|
||||||
if len(res) > 0 {
|
|
||||||
q = q.Where("id IN (?)", pg.In(list))
|
|
||||||
}
|
|
||||||
err := q.Order("created_at DESC").Select()
|
|
||||||
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *AppRepository) GetApplicationsByServiceId(serviceId ...int32) ([]entity.Application, error) {
|
|
||||||
res := make([]entity.Application, 0)
|
|
||||||
err := r.getDb().Model(&res).Where("service_id IN (?)", pg.In(serviceId)).Order("created_at DESC").Select()
|
|
||||||
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *AppRepository) CreateApplication(service entity.Application) (entity.Application, error) {
|
|
||||||
_, err := r.getDb().Model(&service).Insert()
|
|
||||||
|
|
||||||
return service, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *AppRepository) GetApplicationByNameAndServiceId(name string, serviceId int32) (*entity.Application, error) {
|
|
||||||
app := new(entity.Application)
|
|
||||||
err := r.getDb().Model(app).Where("name = ? AND service_id = ?", name, serviceId).First()
|
|
||||||
if err == pg.ErrNoRows {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return app, err
|
|
||||||
}
|
|
||||||
|
|
||||||
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 (r *AppRepository) GetApplicationById(id int32) (*entity.Application, error) {
|
|
||||||
app := &entity.Application{Id: id}
|
|
||||||
err := r.getDb().Select(app)
|
|
||||||
if err == pg.ErrNoRows {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return app, err
|
|
||||||
}
|
|
||||||
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,155 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
|
||||||
|
"github.com/Masterminds/squirrel"
|
||||||
|
"github.com/integration-system/isp-kit/db"
|
||||||
|
"github.com/integration-system/isp-kit/db/query"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"isp-system-service/domain"
|
||||||
|
"isp-system-service/entity"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Application struct {
|
||||||
|
db db.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewApplication(db db.DB) Application {
|
||||||
|
return Application{
|
||||||
|
db: db,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Application) GetApplicationById(ctx context.Context, id int) (*entity.Application, error) {
|
||||||
|
q := `
|
||||||
|
SELECT id, name, description, service_id, type, created_at, updated_at
|
||||||
|
FROM application
|
||||||
|
WHERE id = $1
|
||||||
|
`
|
||||||
|
result := entity.Application{}
|
||||||
|
err := r.db.SelectRow(ctx, &result, q, id)
|
||||||
|
if err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return nil, domain.ErrApplicationNotFound
|
||||||
|
}
|
||||||
|
return nil, errors.WithMessage(err, "select row db")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Application) GetApplicationByIdList(ctx context.Context, idList []int) ([]entity.Application, error) {
|
||||||
|
q, args, err := query.New().
|
||||||
|
Select("id", "name", "description", "service_id", "type", "created_at", "updated_at").
|
||||||
|
From("application").
|
||||||
|
Where(squirrel.Eq{"id": idList}).
|
||||||
|
OrderBy("created_at DESC").
|
||||||
|
ToSql()
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.WithMessage(err, "build query")
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make([]entity.Application, 0)
|
||||||
|
err = r.db.Select(ctx, &result, q, args...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.WithMessagef(err, "select db")
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Application) GetApplicationByServiceIdList(ctx context.Context, serviceIdList []int) ([]entity.Application, error) {
|
||||||
|
q, args, err := query.New().
|
||||||
|
Select("id", "name", "description", "service_id", "type", "created_at", "updated_at").
|
||||||
|
From("application").
|
||||||
|
Where(squirrel.Eq{"service_id": serviceIdList}).
|
||||||
|
OrderBy("created_at DESC").
|
||||||
|
ToSql()
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.WithMessage(err, "build query")
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make([]entity.Application, 0)
|
||||||
|
err = r.db.Select(ctx, &result, q, args...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.WithMessagef(err, "select db")
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Application) GetApplicationByNameAndServiceId(ctx context.Context, name string, serviceId int) (*entity.Application, error) {
|
||||||
|
q := `
|
||||||
|
SELECT id, name, description, service_id, type, created_at, updated_at
|
||||||
|
FROM application
|
||||||
|
WHERE name = $1 AND service_id = $2
|
||||||
|
`
|
||||||
|
result := entity.Application{}
|
||||||
|
err := r.db.SelectRow(ctx, &result, q, name, serviceId)
|
||||||
|
if err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return nil, errors.WithMessagef(err, "select row db")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Application) CreateApplication(ctx context.Context, name string, desc string, serviceId int, appType string) (
|
||||||
|
*entity.Application, error) {
|
||||||
|
q := `
|
||||||
|
INSERT INTO application
|
||||||
|
(name, description, service_id, type)
|
||||||
|
VALUES ($1, $2, $3, $4)
|
||||||
|
RETURNING id, name, description, service_id, type, created_at, updated_at
|
||||||
|
`
|
||||||
|
result := entity.Application{}
|
||||||
|
err := r.db.SelectRow(ctx, &result, q, name, desc, serviceId, appType)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.WithMessagef(err, "select row db")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Application) UpdateApplication(ctx context.Context, id int, name string, description string) (*entity.Application, error) {
|
||||||
|
q := `
|
||||||
|
UPDATE application
|
||||||
|
SET name = $2,
|
||||||
|
description = $3
|
||||||
|
WHERE id = $1
|
||||||
|
RETURNING id, name, description, service_id, type, created_at, updated_at
|
||||||
|
`
|
||||||
|
result := entity.Application{}
|
||||||
|
err := r.db.SelectRow(ctx, &result, q, id, name, description)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.WithMessagef(err, "select row db")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Application) DeleteApplicationByIdList(ctx context.Context, idList []int) (int, error) {
|
||||||
|
q, args, err := query.New().
|
||||||
|
Delete("application").
|
||||||
|
Where(squirrel.Eq{"id": idList}).
|
||||||
|
ToSql()
|
||||||
|
if err != nil {
|
||||||
|
return 0, errors.WithMessage(err, "build query")
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := r.db.Exec(ctx, q, args...)
|
||||||
|
if err != nil {
|
||||||
|
return 0, errors.WithMessage(err, "exec db")
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected, err := result.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return 0, errors.WithMessage(err, "get rows affected")
|
||||||
|
}
|
||||||
|
|
||||||
|
return int(rowsAffected), nil
|
||||||
|
}
|
||||||
Reference in new issue
Block a user