111 lines
2.5 KiB
Go
111 lines
2.5 KiB
Go
package tindakan
|
|
|
|
import (
|
|
"antrian-operasi/internal/database"
|
|
|
|
queryUtils "antrian-operasi/internal/utils/query"
|
|
|
|
"errors"
|
|
|
|
"context"
|
|
)
|
|
|
|
const DB_NAME = "db_simrs"
|
|
const TBL_NAME = "icd_cm"
|
|
|
|
type ITindakanRepository interface {
|
|
SearchableListTindakan(c context.Context, search string) (ListTindakanModel, error)
|
|
GetTindakanByKode(c context.Context, kode string) (TindakanModel, error)
|
|
}
|
|
|
|
type tindakanRepo struct {
|
|
queryBuilder *queryUtils.QueryBuilder
|
|
db database.Service
|
|
}
|
|
|
|
func NewRepository(dbService database.Service) ITindakanRepository {
|
|
queryBuilder := queryUtils.NewQueryBuilder(queryUtils.DBTypePostgreSQL).
|
|
SetAllowedColumns([]string{
|
|
"kode", "keterangan",
|
|
}).
|
|
SetSecurityOptions(false, 100)
|
|
|
|
return tindakanRepo{
|
|
queryBuilder: queryBuilder,
|
|
db: dbService,
|
|
}
|
|
}
|
|
|
|
func (r tindakanRepo) SearchableListTindakan(c context.Context, search string) (ListTindakanModel, error) {
|
|
var result ListTindakanModel
|
|
|
|
query := queryUtils.DynamicQuery{
|
|
From: TBL_NAME,
|
|
Fields: []queryUtils.SelectField{
|
|
{Expression: "kode"},
|
|
{Expression: "keterangan"},
|
|
},
|
|
Limit: 10,
|
|
}
|
|
|
|
if search != "" {
|
|
searchFilters := []queryUtils.DynamicFilter{
|
|
{Column: "kode", Operator: queryUtils.OpILike, Value: "%" + search + "%"},
|
|
{Column: "keterangan", Operator: queryUtils.OpILike, Value: "%" + search + "%"},
|
|
}
|
|
query.Filters = append(query.Filters, queryUtils.FilterGroup{Filters: searchFilters, LogicOp: "OR"})
|
|
}
|
|
|
|
dbconn, err := r.db.GetSQLXDB(DB_NAME)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
|
|
err = r.queryBuilder.ExecuteQuery(
|
|
c, dbconn, query, &result)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (r tindakanRepo) GetTindakanByKode(c context.Context, kode string) (TindakanModel, error) {
|
|
var resultQuery []TindakanModel
|
|
var result TindakanModel
|
|
|
|
query := queryUtils.DynamicQuery{
|
|
From: TBL_NAME,
|
|
Fields: []queryUtils.SelectField{
|
|
{Expression: "kode"},
|
|
{Expression: "keterangan"},
|
|
},
|
|
Filters: []queryUtils.FilterGroup{
|
|
{
|
|
Filters: []queryUtils.DynamicFilter{
|
|
{Column: "kode", Operator: queryUtils.OpILike, Value: kode},
|
|
}, LogicOp: "AND",
|
|
},
|
|
},
|
|
Limit: 1,
|
|
}
|
|
|
|
dbconn, err := r.db.GetSQLXDB(DB_NAME)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
|
|
err = r.queryBuilder.ExecuteQuery(
|
|
c, dbconn, query, &resultQuery)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
|
|
if len(resultQuery) == 0 {
|
|
notFoundError := errors.New("tindakan not found : " + kode)
|
|
return result, notFoundError
|
|
}
|
|
|
|
return resultQuery[0], nil
|
|
}
|