feat:#39 added fetcher for Events

This commit is contained in:
Elmar Kresse
2024-03-23 19:55:27 +01:00
parent 48a8eb43c3
commit 38f9c4d71b
10 changed files with 359 additions and 187 deletions

View File

@@ -0,0 +1,100 @@
package v3
import (
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/models"
"golang.org/x/net/http2"
"htwkalender/model"
"htwkalender/service/db"
"log/slog"
"net/http"
"strconv"
)
func FetchAllResources(app *pocketbase.PocketBase) ([]*models.Record, error) {
var groups []model.SeminarGroup
client := &http.Client{}
client.Transport = &http2.Transport{}
apiUrl := "https://luna.htwk-leipzig.de/api/"
seminarUrl := apiUrl + "studierendengruppen"
parsedSeminarGroups, err := parseSeminarGroups(seminarUrl, client)
if err != nil {
slog.Error("Error while fetching seminar groups", err)
return nil, err
}
studyTypeUrl := apiUrl + "studiengaenge"
parsedStudyTypes, err := parseStudyTypes(studyTypeUrl, client)
if err != nil {
slog.Error("Error while fetching study types", err)
return nil, err
}
facultyUrl := apiUrl + "fakultaeten"
parsedFaculties, err := parseFaculties(facultyUrl, client)
if err != nil {
slog.Error("Error while fetching faculties", err)
return nil, err
}
slog.Info("Fetched study types: " + strconv.Itoa(len(parsedStudyTypes)))
slog.Info("Fetched seminar groups: " + strconv.Itoa(len(parsedSeminarGroups.Groups)) + " of " + strconv.Itoa(parsedSeminarGroups.TotalItems))
slog.Info("Fetched faculties: " + strconv.Itoa(len(parsedFaculties.Faculties)))
// map seminar groups to model seminar groups
for _, group := range parsedSeminarGroups.Groups {
var newGroup model.SeminarGroup
newGroup.University = "HTWK Leipzig"
newGroup.Course = group.SeminarGroup
newGroup.Faculty = group.Faculty
newGroup.Semester = group.Semester
// find corresponding study type by studiengang in parsedStudyTypes
for _, studyTypeItem := range parsedStudyTypes {
if studyTypeItem.ID == group.Studiengang {
newGroup.GroupShortcut = studyTypeItem.Description
newGroup.GroupId = studyTypeItem.GroupID
break
}
}
for _, facultyItem := range parsedFaculties.Faculties {
if facultyItem.ID == group.Faculty {
newGroup.FacultyId = facultyItem.ShortCut
newGroup.Faculty = facultyItem.Description
break
}
}
groups = append(groups, newGroup)
}
collection, dbError := db.FindCollection(app, "groups")
if dbError != nil {
slog.Error("Error while searching collection groups", dbError)
return nil, err
}
var insertedGroups []*models.Record
insertedGroups, dbError = db.SaveGroups(groups, collection, app)
if dbError != nil {
slog.Error("Error while saving groups", dbError)
return nil, err
}
//Now fetch all events
eventUrl := apiUrl + "veranstaltungen"
parsedEvents, err := parseEvents(eventUrl, client)
slog.Info("Fetched events: " + strconv.Itoa(len(parsedEvents.Events)) + " of " + strconv.Itoa(parsedEvents.TotalItems))
return insertedGroups, nil
}