Files
htwkalender/services/ical/service/connector/grpc/modules.go
2024-07-13 16:46:28 +02:00

107 lines
3.1 KiB
Go

//Calendar implementation for the HTWK Leipzig timetable. Evaluation and display of the individual dates in iCal format.
//Copyright (C) 2024 HTWKalender support@htwkalender.de
//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU Affero General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//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 for more details.
//You should have received a copy of the GNU Affero General Public License
//along with this program. If not, see <https://www.gnu.org/licenses/>.
package grpc
import (
"context"
"google.golang.org/grpc"
pb "htwkalender/common/genproto/modules"
"htwkalender/ical/model"
"log/slog"
"time"
)
func GetModuleWithEvents(module model.FeedModule, conn *grpc.ClientConn) (model.Module, error) {
c := pb.NewModuleServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.GetModule(ctx, &pb.GetModuleRequest{Uuid: module.UUID})
if err != nil {
slog.Error("could not get module: %v", "error", err)
}
events := make([]model.Event, 0)
for _, event := range r.GetModule().Events {
events = append(events, protoToEvent(event))
}
return model.Module{
UUID: r.GetModule().Uuid,
Name: r.GetModule().Name,
Prof: r.GetModule().Prof,
Course: r.GetModule().Course,
Semester: r.GetModule().Semester,
Events: events,
}, nil
}
func GetModulesWithEvents(modules []model.FeedModule, conn *grpc.ClientConn) ([]model.Module, error) {
c := pb.NewModuleServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
// List of uuids
uuids := make([]string, 0)
for _, module := range modules {
uuids = append(uuids, module.UUID)
}
r, err := c.GetModules(ctx, &pb.GetModulesRequest{Uuids: uuids})
if err != nil {
slog.Error("could not get modules: %v", "error", err)
}
moduleList := make([]model.Module, 0)
for _, module := range r.GetModules() {
events := make([]model.Event, 0)
for _, event := range module.Events {
events = append(events, protoToEvent(event))
}
moduleList = append(moduleList, model.Module{
UUID: module.Uuid,
Name: module.Name,
Prof: module.Prof,
Course: module.Course,
Semester: module.Semester,
Events: events,
})
}
return moduleList, nil
}
func protoToEvent(event *pb.Event) model.Event {
return model.Event{
UUID: event.Uuid,
Day: event.Day,
Week: event.Week,
Start: model.ToJSONTime(event.Start),
End: model.ToJSONTime(event.End),
Name: event.Name,
EventType: event.EventType,
Compulsory: event.Compulsory,
Prof: event.Prof,
Rooms: event.Rooms,
Notes: event.Notes,
BookedAt: event.BookedAt,
Course: event.Course,
Semester: event.Semester,
}
}