api-cds-search/cmd/model/cdsView.go
2025-03-24 10:01:47 +01:00

98 lines
2.4 KiB
Go

/*
Copyright (C) 2025 snoutie
Authors: snoutie (copyright@achtarmig.org)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package model
import (
"api-cds-search/cmd/database/table"
"fmt"
"strings"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
type CDSViewFieldModel struct {
table.CDSViewField
DataTypeTitle string
FieldLengthOut string
DescriptionOut string
}
type CDSViewModel struct {
*table.CDSView
StateTitle string
Fields *[]CDSViewFieldModel
}
var englishCases = cases.Title(language.English)
func GetCDSViewModel(TechnicalName string) (*CDSViewModel, error) {
var model CDSViewModel
cdsView, err := table.GetCDSView(TechnicalName)
if err != nil {
return nil, err
}
model.CDSView = cdsView
model.StateTitle = englishCases.String(model.State)
fields, err := table.GetCDSViewFields(TechnicalName)
if err != nil {
return nil, err
}
var fieldModel CDSViewFieldModel
var fieldsModel []CDSViewFieldModel
for _, field := range *fields {
fieldModel.CDSViewField = field
fieldModel.DataTypeTitle = englishCases.String(field.DataType)
fieldModel.FieldLengthOut = strings.TrimLeft(field.FieldLength, "0")
fieldModel.DescriptionOut = field.Description
if fieldModel.DescriptionOut == "" {
fieldModel.DescriptionOut = "-"
}
fieldsModel = append(fieldsModel, fieldModel)
}
model.Fields = &fieldsModel
return &model, nil
}
func GetAllCDSViewModels() (*[]CDSViewModel, error) {
cdsViewTechnicalNames, err := table.QueryAllCDSViewTechnicalNames()
if err != nil {
return nil, err
}
var cdsViewModels []CDSViewModel
for _, cdsViewTechnicalName := range *cdsViewTechnicalNames {
cdsViewModel, err := GetCDSViewModel(cdsViewTechnicalName)
if err != nil {
fmt.Println(err)
continue
}
cdsViewModels = append(cdsViewModels, *cdsViewModel)
}
return &cdsViewModels, nil
}