Skip to content

Commit

Permalink
made sure that the student clubs are localised
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Sep 23, 2024
1 parent a928e68 commit 5f3de4a
Show file tree
Hide file tree
Showing 12 changed files with 516 additions and 354 deletions.
756 changes: 411 additions & 345 deletions server/api/tumdev/campus_backend.pb.go

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions server/api/tumdev/campus_backend.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion server/api/tumdev/campus_backend.proto
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,17 @@ message GetCanteenHeadCountReply {
// A time stamp indicating how up to date the response is. Only valid in case percent != -1.
google.protobuf.Timestamp timestamp = 4;
}
message ListStudentClubRequest {}

enum Language {
German = 0;
English = 1;
}

message ListStudentClubRequest {
// Language of the student clubs and categories
// Defaults to german
optional Language language = 1;
}
message ListStudentClubReply {
repeated StudentClubCollection collections = 1;
}
Expand Down
22 changes: 22 additions & 0 deletions server/api/tumdev/campus_backend.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,20 @@
}
}
},
"parameters": [
{
"name": "language",
"description": "Language of the student clubs and categories\nDefaults to german",
"in": "query",
"required": false,
"type": "string",
"enum": [
"German",
"English"
],
"default": "German"
}
],
"tags": [
"Campus"
]
Expand Down Expand Up @@ -1172,6 +1186,14 @@
}
}
},
"apiLanguage": {
"type": "string",
"enum": [
"German",
"English"
],
"default": "German"
},
"apiListAvailableCanteenTagsReply": {
"type": "object",
"properties": {
Expand Down
4 changes: 3 additions & 1 deletion server/backend/cron/cronjobs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cron

import (
pb "github.com/TUM-Dev/Campus-Backend/server/api/tumdev"
"time"

"github.com/TUM-Dev/Campus-Backend/server/model"
Expand Down Expand Up @@ -69,7 +70,8 @@ func (c *CronService) Run() error {
// Run each job in a separate goroutine, so we can parallelize them
switch cronjob.Type.String {
case StudentClubType:
g.Go(func() error { return c.studentClubCron() })
g.Go(func() error { return c.studentClubCron(pb.Language_German) })
g.Go(func() error { return c.studentClubCron(pb.Language_English) })
case NewsType:
// if this is not copied here, this may not be threads save due to go's guarantees
// loop variable cronjob captured by func literal (govet)
Expand Down
9 changes: 6 additions & 3 deletions server/backend/cron/student_clubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/md5"
"errors"
"fmt"
pb "github.com/TUM-Dev/Campus-Backend/server/api/tumdev"
"io"
"strings"

Expand All @@ -20,7 +21,7 @@ const (
StudentClubImageDirectory = "student_club/"
)

func (c *CronService) studentClubCron() error {
func (c *CronService) studentClubCron(language pb.Language) error {
body, err := student_club_parsers.DownloadHtml("https://www.sv.tum.de/sv/hochschulgruppen/")
defer func(Body io.ReadCloser) {
if err := Body.Close(); err != nil {
Expand All @@ -37,15 +38,16 @@ func (c *CronService) studentClubCron() error {

// save the result of the previous steps (🎉)
if err := c.db.Transaction(func(tx *gorm.DB) error {
if err := tx.Where("1 = 1").Delete(&model.StudentClub{}).Error; err != nil {
if err := tx.Where("language = ?", language.String()).Delete(&model.StudentClub{}).Error; err != nil {
return err
}
if err := tx.Where("1 = 1").Delete(&model.StudentClubCollection{}).Error; err != nil {
if err := tx.Where("language = ?", language.String()).Delete(&model.StudentClubCollection{}).Error; err != nil {
return err
}
for _, scrapedCollection := range scrapedCollections {
collection := model.StudentClubCollection{
ID: scrapedCollection.Name,
Language: language.String(),
Description: scrapedCollection.Description,
}
if err := tx.Create(&collection).Error; err != nil {
Expand All @@ -54,6 +56,7 @@ func (c *CronService) studentClubCron() error {
}
for _, scrapedClub := range scrapedClubs {
club := model.StudentClub{
Language: language.String(),
Name: scrapedClub.Name,
Description: scrapedClub.Description,
LinkUrl: scrapedClub.LinkUrl,
Expand Down
11 changes: 8 additions & 3 deletions server/backend/cron/student_clubs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cron

import (
"context"
pb "github.com/TUM-Dev/Campus-Backend/server/api/tumdev"
"testing"

"github.com/TUM-Dev/Campus-Backend/server/model"
Expand All @@ -15,9 +16,13 @@ func TestCronService_studentClubCron(t *testing.T) {
db := utils.SetupTestContainer(ctx, t)
service := New(db)
var clubs []model.StudentClub
db.WithContext(ctx).Find(&clubs)
require.NoError(t, db.WithContext(ctx).Find(&clubs).Error)
require.Equal(t, []model.StudentClub{}, clubs)
require.NoError(t, service.studentClubCron())
db.WithContext(ctx).Find(&clubs)
require.NoError(t, service.studentClubCron(pb.Language_German))
require.NoError(t, db.WithContext(ctx).Find(&clubs).Error)
require.True(t, len(clubs) > 0)
require.NoError(t, db.WithContext(ctx).Where("1=1").Delete(&model.StudentClub{}).Error)
require.NoError(t, service.studentClubCron(pb.Language_English))
require.NoError(t, db.WithContext(ctx).Find(&clubs).Error)
require.True(t, len(clubs) > 0)
}
32 changes: 32 additions & 0 deletions server/backend/migration/20241023000000.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package migration

import (
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)

// migrate20241023000000
// - made sure that student clubs are localised
func migrate20241023000000() *gormigrate.Migration {
return &gormigrate.Migration{
ID: "20241023000000",
Migrate: func(tx *gorm.DB) error {
if err := tx.Exec("alter table student_clubs add language enum ('German', 'English') default 'German' not null").Error; err != nil {
return err
}
if err := tx.Exec("alter table student_club_collections add language enum ('German', 'English') default 'German' not null").Error; err != nil {
return err
}
return nil
},
Rollback: func(tx *gorm.DB) error {
if err := tx.Exec("alter table student_clubs drop column language").Error; err != nil {
return err
}
if err := tx.Exec("alter table student_club_collections drop column language").Error; err != nil {
return err
}
return nil
},
}
}
1 change: 1 addition & 0 deletions server/backend/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func manualMigrate(db *gorm.DB) error {
migrate20240512000000(),
migrate20240706000000(),
migrate20240824000000(),
migrate20241023000000(),
}
return gormigrate.New(db, gormigrateOptions, migrations).Migrate()
}
Expand Down
3 changes: 2 additions & 1 deletion server/backend/student_club.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
"google.golang.org/grpc/status"
)

func (s *CampusServer) ListStudentClub(ctx context.Context, _ *pb.ListStudentClubRequest) (*pb.ListStudentClubReply, error) {
func (s *CampusServer) ListStudentClub(ctx context.Context, req *pb.ListStudentClubRequest) (*pb.ListStudentClubReply, error) {
var dbClubs []model.StudentClub
if err := s.db.WithContext(ctx).
Where("language = ?", req.GetLanguage().String()).
Joins("Image").
Joins("StudentClubCollection").
Find(&dbClubs).Error; err != nil {
Expand Down
1 change: 1 addition & 0 deletions server/model/student_club.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type StudentClub struct {
gorm.Model
Name string
Language string `gorm:"type:enum('German','English');default:'German'"`
Description null.String
LinkUrl null.String `gorm:"type:varchar(190);unique;"`
ImageID null.Int
Expand Down
1 change: 1 addition & 0 deletions server/model/student_club_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
// StudentClubCollection stores what collection a club belongs to
type StudentClubCollection struct {
ID string `gorm:"primaryKey;type:varchar(100)"`
Language string `gorm:"type:enum('German','English');default:'German'"`
Description string
CreatedAt time.Time
UpdatedAt time.Time
Expand Down

0 comments on commit 5f3de4a

Please sign in to comment.