From 8da5334e7828ccc72ee06bf93fc0f87c4384db26 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Sun, 22 Oct 2023 17:19:10 +0200 Subject: [PATCH] removed even more boilerplate --- server/backend/campus_api/campus_api.go | 5 ++++- server/backend/ios_notifications/interfaces.go | 13 ------------- .../new_exam_results_hook/scheduling/repository.go | 1 - .../new_exam_results_hook/scheduling/service.go | 8 +------- .../new_exam_results_hook/subscriber/repository.go | 12 ++++-------- .../new_exam_results_hook/subscriber/service.go | 6 ++---- 6 files changed, 11 insertions(+), 34 deletions(-) delete mode 100644 server/backend/ios_notifications/interfaces.go diff --git a/server/backend/campus_api/campus_api.go b/server/backend/campus_api/campus_api.go index 746455e4..003cc646 100644 --- a/server/backend/campus_api/campus_api.go +++ b/server/backend/campus_api/campus_api.go @@ -7,13 +7,16 @@ import ( "fmt" "io" "net/http" + "os" "github.com/TUM-Dev/Campus-Backend/server/model" log "github.com/sirupsen/logrus" ) -func FetchExamResultsPublished(token string) (*model.TUMAPIPublishedExamResults, error) { +// FetchExamResultsPublished fetches all published exam results from the TUM Campus API using CAMPUS_API_TOKEN. +func FetchExamResultsPublished() (*model.TUMAPIPublishedExamResults, error) { var examResultsPublished model.TUMAPIPublishedExamResults + token := os.Getenv("CAMPUS_API_TOKEN") err := RequestCampusApi("/wbservicesbasic.pruefungenErgebnisse", token, &examResultsPublished) if err != nil { return nil, err diff --git a/server/backend/ios_notifications/interfaces.go b/server/backend/ios_notifications/interfaces.go deleted file mode 100644 index 32532e15..00000000 --- a/server/backend/ios_notifications/interfaces.go +++ /dev/null @@ -1,13 +0,0 @@ -package ios_notifications - -import ( - "gorm.io/gorm" -) - -type BaseRepository interface { - NewRepository(db *gorm.DB) *BaseRepository -} - -type BaseService interface { - NewService(repository *BaseRepository) *BaseService -} diff --git a/server/backend/new_exam_results_hook/scheduling/repository.go b/server/backend/new_exam_results_hook/scheduling/repository.go index 558cb6c6..aaff769a 100644 --- a/server/backend/new_exam_results_hook/scheduling/repository.go +++ b/server/backend/new_exam_results_hook/scheduling/repository.go @@ -15,7 +15,6 @@ func (repository *Repository) StoreExamResultsPublished(examResultsPublished []m return db.Transaction(func(tx *gorm.DB) error { err := tx.Where("1 = 1").Delete(&model.PublishedExamResult{}).Error - if err != nil { return err } diff --git a/server/backend/new_exam_results_hook/scheduling/service.go b/server/backend/new_exam_results_hook/scheduling/service.go index e1b68c13..917f3dad 100644 --- a/server/backend/new_exam_results_hook/scheduling/service.go +++ b/server/backend/new_exam_results_hook/scheduling/service.go @@ -1,8 +1,6 @@ package scheduling import ( - "os" - "github.com/TUM-Dev/Campus-Backend/server/backend/campus_api" "github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/apns" "github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/device" @@ -11,10 +9,6 @@ import ( log "github.com/sirupsen/logrus" ) -var ( - CampusApiToken = os.Getenv("CAMPUS_API_TOKEN") -) - type Service struct { Repository *Repository DevicesRepository *device.Repository @@ -25,7 +19,7 @@ type Service struct { func (service *Service) HandleScheduledCron() error { log.Info("Fetching published exam results") - apiResult, err := campus_api.FetchExamResultsPublished(CampusApiToken) + apiResult, err := campus_api.FetchExamResultsPublished() if err != nil { return err } diff --git a/server/backend/new_exam_results_hook/subscriber/repository.go b/server/backend/new_exam_results_hook/subscriber/repository.go index a8059293..27667410 100644 --- a/server/backend/new_exam_results_hook/subscriber/repository.go +++ b/server/backend/new_exam_results_hook/subscriber/repository.go @@ -15,29 +15,25 @@ type Repository struct { } func (repository *Repository) FindAllSubscribers() (*[]model.NewExamResultsSubscriber, error) { - db := repository.DB - var subscribers []model.NewExamResultsSubscriber - - err := db.Find(&subscribers).Error + err := repository.DB.Find(&subscribers).Error return &subscribers, err } func (repository *Repository) NotifySubscriber(subscriber *model.NewExamResultsSubscriber, newGrades *[]model.PublishedExamResult) error { - url := subscriber.CallbackUrl - body, err := json.Marshal(newGrades) if err != nil { log.WithError(err).Error("Error while marshalling newGrades") return err } - req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(body)) + req, err := http.NewRequest(http.MethodPost, subscriber.CallbackUrl, bytes.NewBuffer(body)) if err != nil { log.WithError(err).Error("Error while creating request") return err } + req.Header.Set("Content-Type", "application/json") if subscriber.ApiKey.Valid { req.Header.Set("Authorization", subscriber.ApiKey.String) @@ -45,7 +41,7 @@ func (repository *Repository) NotifySubscriber(subscriber *model.NewExamResultsS _, err = http.DefaultClient.Do(req) if err != nil { - log.WithField("url", url).WithError(err).Error("Error while fetching url") + log.WithField("url", subscriber.CallbackUrl).WithError(err).Error("Error while fetching url") return err } diff --git a/server/backend/new_exam_results_hook/subscriber/service.go b/server/backend/new_exam_results_hook/subscriber/service.go index ef8509f6..55c6747f 100644 --- a/server/backend/new_exam_results_hook/subscriber/service.go +++ b/server/backend/new_exam_results_hook/subscriber/service.go @@ -10,15 +10,13 @@ type Service struct { } func (service *Service) NotifySubscribers(newGrades *[]model.PublishedExamResult) error { - repository := service.Repository - - subscribers, err := repository.FindAllSubscribers() + subscribers, err := service.Repository.FindAllSubscribers() if err != nil { return err } for _, subscriber := range *subscribers { - if err := repository.NotifySubscriber(&subscriber, newGrades); err != nil { + if err := service.Repository.NotifySubscriber(&subscriber, newGrades); err != nil { log.WithError(err).Error("Failed to notify subscriber") continue }