feat:#39 added module and prof fetch

This commit is contained in:
Elmar Kresse
2024-03-24 00:15:49 +01:00
parent 901ede6c97
commit 82cf9ce48c
12 changed files with 504 additions and 32 deletions

View File

@@ -0,0 +1,114 @@
package v3
import (
"encoding/json"
"htwkalender/service/functions"
"log/slog"
"net/http"
)
/*
{
"@context": "/api/contexts/Modul",
"@id": "/api/module",
"@type": "hydra:Collection",
"hydra:totalItems": 1678,
"hydra:member": [
{
"@id": "/api/module/0055CE2545A3411A00C32F30A645463B",
"@type": "Modul",
"id": "0055CE2545A3411A00C32F30A645463B",
"semester": "/api/semester/ws",
"fakultaet": "/api/fakultaeten/9B89016985E5156B1C033BB0FD3AF9B4",
"kuerzel": "PPKMSBM&SMM&STM3",
"bezeichnung": "W833 Produkt- und Prozesskostenmanagement SBM & SMM & STM 3. FS (wpf)",
"hinweisOne": "",
"internalUpdatedAt": "2023-03-07T07:35:34+01:00"
}
],
"hydra:view": {
"@id": "/api/module?itemsPerPage=100&page=1",
"@type": "hydra:PartialCollectionView",
"hydra:first": "/api/module?itemsPerPage=100&page=1",
"hydra:last": "/api/module?itemsPerPage=100&page=17",
"hydra:next": "/api/module?itemsPerPage=100&page=2"
},
"hydra:search": {
"@type": "hydra:IriTemplate",
"hydra:template": "/api/module{?veranstaltungen,veranstaltungen[]}",
"hydra:variableRepresentation": "BasicRepresentation",
"hydra:mapping": [
{
"@type": "IriTemplateMapping",
"variable": "veranstaltungen",
"property": "veranstaltungen",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "veranstaltungen[]",
"property": "veranstaltungen",
"required": false
}
]
}
}
*/
// Module Model for fetching json data
type Modules struct {
TotalItems int `json:"hydra:totalItems"`
Modules []Module `json:"hydra:member"`
}
type Module struct {
ID string `json:"id"`
Semester string `json:"semester"`
Faculty string `json:"fakultaet"`
ShortCut string `json:"kuerzel"`
Name string `json:"bezeichnung"`
Note string `json:"hinweisOne"`
Internal string `json:"internalUpdatedAt"`
}
func parseModules(url string, client *http.Client) (Modules, error) {
// the url is paginated, so we need to fetch all pages
// example url: https://luna.htwk-leipzig.de/api/module?page=1&itemsPerPage=100
// the itemsPerPage is set to 100, so we need to fetch all pages until we get an empty response
var fetchedModules Modules
var itemsPerPage = 999
responses, err := paginatedFetch(url, itemsPerPage, client)
if err != nil {
slog.Error("Error while fetching modules", err)
return Modules{}, err
}
for _, response := range responses {
var modules Modules
err = json.Unmarshal([]byte(response), &modules)
if err != nil {
slog.Error("Error while unmarshalling modules", err)
return Modules{}, err
}
// cut api iri prefix
for i, module := range modules.Modules {
modules.Modules[i].Faculty = functions.RemoveIriPrefix(module.Faculty, 32)
modules.Modules[i].Semester = functions.RemoveIriPrefix(module.Semester, 2)
}
fetchedModules.Modules = append(fetchedModules.Modules, modules.Modules...)
fetchedModules.TotalItems = modules.TotalItems
}
return fetchedModules, nil
}