niki/patientapp/repository/mysql/helper.go

57 lines
1.0 KiB
Go

package mysql
import (
"encoding/json"
"fmt"
"os"
"strings"
"git.gocasts.ir/ebhomengo/niki/patientapp/service/entity"
)
type whereClause struct {
sql strings.Builder
args []any
}
func (w *whereClause) add(cond string, args ...any) {
if w.sql.Len() == 0 {
w.sql.WriteString(" WHERE ")
} else {
w.sql.WriteString(" AND ")
}
w.sql.WriteString(cond)
w.args = append(w.args, args...)
}
func (w *whereClause) String() string { return w.sql.String() }
func clampLimitOffset(limit, offset int) (int, int) {
if limit <= 0 {
limit = 50
}
if limit > 100 {
limit = 100
}
if offset < 0 {
offset = 0
}
return limit, offset
}
func LoadPatientsFromJSON(filePath string) ([]entity.Patient, error) {
// Read the file
data, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}
// Unmarshal into slice of Patient
var patients []entity.Patient
if err := json.Unmarshal(data, &patients); err != nil {
return nil, fmt.Errorf("failed to unmarshal JSON: %w", err)
}
return patients, nil
}