/* 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 . */ package model import ( "api-cds-search/cmd/database/table" "encoding/base64" "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 TechnicalNameEncoded string NumberOfFields int } var englishCases = cases.Title(language.English) func GetCDSViewModelFields(TechnicalName string) (*[]CDSViewFieldModel, error) { 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) } return &fieldsModel, nil } 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) model.TechnicalNameEncoded = strings.Replace(base64.StdEncoding.EncodeToString([]byte(model.TechnicalName)), "=", "", -1) model.NumberOfFields, err = table.GetCDSViewNumberOfFields(TechnicalName) if err != nil { return nil, err } 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 }