90 lines
3.5 KiB
Go
90 lines
3.5 KiB
Go
|
/*
|
||
|
GoSkola24API
|
||
|
Copyright (C) 2024, Marco Vitchi Thulin
|
||
|
|
||
|
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"
|
||
|
pubtypes "git.zervo.org/zervo/GoSkola24API/pkg/goskola24api/types"
|
||
|
)
|
||
|
|
||
|
func GetClasses(school pubtypes.School, checkAvailability bool) (_result []pubtypes.Class, _error error) {
|
||
|
if checkAvailability && !school.AvailableData.HasClasses {
|
||
|
return nil, errors.New("availability check failed: school does not provide class data")
|
||
|
}
|
||
|
|
||
|
filters := types.RequestFilters{
|
||
|
Class: true,
|
||
|
Course: false,
|
||
|
Group: false,
|
||
|
Period: false,
|
||
|
Room: false,
|
||
|
Student: false,
|
||
|
Subject: false,
|
||
|
Teacher: false,
|
||
|
}
|
||
|
|
||
|
responseMap, err := GetGenericSelection(school, filters)
|
||
|
if err != nil {
|
||
|
return nil, errors.New("failed to get classes: " + err.Error())
|
||
|
}
|
||
|
|
||
|
// Extract classes as []interface{}
|
||
|
classesRaw, ok := responseMap["classes"].([]interface{})
|
||
|
if !ok {
|
||
|
return nil, errors.New("missing or invalid \"classes\" field in response")
|
||
|
}
|
||
|
|
||
|
// Convert raw classes into usable class type
|
||
|
var classes []pubtypes.Class
|
||
|
for _, classRaw := range classesRaw {
|
||
|
classMap, ok := classRaw.(map[string]interface{})
|
||
|
if !ok {
|
||
|
return nil, errors.New("unexpected room format")
|
||
|
}
|
||
|
|
||
|
// Create class instance from data
|
||
|
class := pubtypes.Class{
|
||
|
Name: func() string { val, _ := classMap["groupName"].(string); return val }(),
|
||
|
ClassId: func() string { val, _ := classMap["groupGuid"].(string); return val }(),
|
||
|
AbsenceMessageNotDeliveredCount: func() int { val, _ := classMap["absenceMessageNotDeliveredCount"].(int); return val }(),
|
||
|
IsResponsible: func() bool { val, _ := classMap["isResponsible"].(bool); return val }(),
|
||
|
IsClass: func() bool { val, _ := classMap["isClass"].(bool); return val }(),
|
||
|
IsAdmin: func() bool { val, _ := classMap["isAdmin"].(bool); return val }(),
|
||
|
IsPrincipal: func() bool { val, _ := classMap["isPrincipal"].(bool); return val }(),
|
||
|
IsMentor: func() bool { val, _ := classMap["isMentor"].(bool); return val }(),
|
||
|
IsPreschoolGroup: func() bool { val, _ := classMap["isPreschoolGroup"].(bool); return val }(),
|
||
|
Teachers: classMap["teachers"], // TODO: MAKE TYPED
|
||
|
SubstituteTeacherId: func() string { val, _ := classMap["substituteTeacherGuid"].(string); return val }(),
|
||
|
TeacherChangeStudents: func() int { val, _ := classMap["teacherChangeStudentsInGroup"].(int); return val }(),
|
||
|
}
|
||
|
|
||
|
classes = append(classes, class)
|
||
|
}
|
||
|
|
||
|
return classes, nil
|
||
|
}
|