Course retrieval from redis, setup ICS command data with course detailsl

This commit is contained in:
2024-01-30 15:27:56 -06:00
parent 3ec5644345
commit 4ac28bd210
2 changed files with 32 additions and 11 deletions

23
api.go
View File

@@ -10,6 +10,7 @@ import (
"strconv"
"strings"
"github.com/redis/go-redis/v9"
"github.com/rs/zerolog/log"
)
@@ -458,3 +459,25 @@ func ResetDataForm() {
log.Fatal().Err(err).Msg("Failed to reset data form")
}
}
// GetCourse retrieves the course information.
// This course does not retrieve directly from the API, but rather uses scraped data stored in Redis.
func GetCourse(crn string) (*Course, error) {
// Retrieve raw data
result, err := kv.Get(ctx, fmt.Sprintf("class:%s", crn)).Result()
if err != nil {
if err == redis.Nil {
return nil, fmt.Errorf("course not found: %w", err)
}
return nil, fmt.Errorf("failed to get course: %w", err)
}
// Unmarshal the raw data
var course Course
err = json.Unmarshal([]byte(result), &course)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal course: %w", err)
}
return &course, nil
}