80 lines
2.3 KiB
Go
80 lines
2.3 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"
|
|
pubtypes "git.zervo.org/zervo/GoSkola24API/types"
|
|
)
|
|
|
|
func GetRooms(school pubtypes.School, checkAvailability bool) (_result []pubtypes.Room, _error error) {
|
|
if checkAvailability && !school.AvailableData.HasRooms {
|
|
return nil, errors.New("availability check failed: school does not provide room data")
|
|
}
|
|
|
|
filters := types.RequestFilters{
|
|
Class: false,
|
|
Course: false,
|
|
Group: false,
|
|
Period: false,
|
|
Room: true,
|
|
Student: false,
|
|
Subject: false,
|
|
Teacher: false,
|
|
}
|
|
|
|
responseMap, err := GetGenericSelection(school, filters)
|
|
if err != nil {
|
|
return nil, errors.New("failed to get rooms: " + err.Error())
|
|
}
|
|
|
|
// Extract rooms as []interface{}
|
|
roomsRaw, ok := responseMap["rooms"].([]interface{})
|
|
if !ok {
|
|
return nil, errors.New("missing or invalid \"rooms\" field in response")
|
|
}
|
|
|
|
// Convert raw rooms into usable room type
|
|
var rooms []pubtypes.Room
|
|
for _, roomRaw := range roomsRaw {
|
|
roomMap, ok := roomRaw.(map[string]interface{})
|
|
if !ok {
|
|
return nil, errors.New("unexpected room format")
|
|
}
|
|
|
|
// Create room struct
|
|
room := pubtypes.Room{
|
|
Name: func() string { val, _ := roomMap["name"].(string); return val }(),
|
|
RoomId: func() string { val, _ := roomMap["eid"].(string); return val }(),
|
|
External: func() bool { val, _ := roomMap["external"].(bool); return val }(),
|
|
}
|
|
|
|
rooms = append(rooms, room)
|
|
}
|
|
|
|
return rooms, nil
|
|
}
|