GoSkola24API/internal/requests/get_terms.go
2024-12-06 21:43:00 +01:00

91 lines
2.7 KiB
Go

/*
GoSkola24API
Copyright (C) 2024, Zervó Zadachin
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License version 3
as published by the Free Software Foundation.
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 version 3 for more details.
This program incorporates external libraries for certain functionalities.
These libraries are covered by their respective licenses, and their usage
agreements are as outlined in their respective documentation or source
code.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package requests
import (
"errors"
"git.zervo.org/zervo/GoSkola24API/internal/types"
"git.zervo.org/zervo/GoSkola24API/internal/utility"
pubtypes "git.zervo.org/zervo/GoSkola24API/pkg/goskola24api/types"
)
func GetTerms(api_host string) (_result pubtypes.Terms, _error error) {
request := types.RequestTerms{
HostName: api_host,
CheckSchoolYearsFeatures: false,
}
response, err := utility.Request(request, "get/active/school/years")
if err != nil {
return pubtypes.Terms{}, errors.New("could not get terms: " + err.Error())
}
// Assert the response type to a map[string]interface{}
responseMap, ok := response.(map[string]interface{})
if !ok {
return pubtypes.Terms{}, errors.New("unexpected response type")
}
// Assert useSchoolYearsFeatures to bool
useYears, ok := responseMap["useSchoolYearsFeatures"].(bool)
if !ok {
useYears = false
}
// Extract the "activeSchoolYears" value using type assertion
extractedYears, ok := responseMap["activeSchoolYears"].([]interface{})
if !ok {
return pubtypes.Terms{}, errors.New("missing or invalid \"activeSchoolYears\" field in response")
}
var schoolTerms []pubtypes.SchoolTerm
// Iterate over the school years
for _, year := range extractedYears {
yearMap, ok := year.(map[string]interface{})
if !ok {
return pubtypes.Terms{}, errors.New("unexpected type in activeSchoolYears slice")
}
// Map to the struct
schoolTerm := pubtypes.SchoolTerm{
Start: yearMap["from"].(string),
TermId: yearMap["guid"].(string),
Name: yearMap["name"].(string),
End: yearMap["to"].(string),
}
schoolTerms = append(schoolTerms, schoolTerm)
}
// Assemble result
result := pubtypes.Terms{
ActiveTerms: schoolTerms,
UseSchoolYearsFeatures: useYears,
}
return result, nil
}