mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender.git
synced 2025-08-02 17:59:14 +02:00
added function chaining for db query
This commit is contained in:
@@ -77,24 +77,50 @@ func findEventByDayWeekStartEndNameCourse(event model.Event, course string, app
|
||||
return &event, err
|
||||
}
|
||||
|
||||
func buildIcalQueryForModules(modules []model.FeedCollection) dbx.Expression {
|
||||
|
||||
// build where conditions for each module
|
||||
|
||||
//first check if modules is empty
|
||||
if len(modules) == 0 {
|
||||
return dbx.HashExp{}
|
||||
}
|
||||
|
||||
//second check if modules has only one element
|
||||
if len(modules) == 1 {
|
||||
return dbx.And(
|
||||
dbx.HashExp{"Name": modules[0].Name},
|
||||
dbx.HashExp{"course": modules[0].Course},
|
||||
)
|
||||
}
|
||||
|
||||
//third check if modules has more than one element
|
||||
var wheres []dbx.Expression
|
||||
|
||||
for _, module := range modules {
|
||||
where := dbx.And(
|
||||
dbx.HashExp{"Name": module.Name},
|
||||
dbx.HashExp{"course": module.Course},
|
||||
)
|
||||
wheres = append(wheres, where)
|
||||
}
|
||||
|
||||
// Use dbx.And or dbx.Or to combine the where conditions as needed
|
||||
where := dbx.Or(wheres...)
|
||||
|
||||
return where
|
||||
|
||||
}
|
||||
|
||||
// GetPlanForModules returns all events for the given modules with the given course
|
||||
// used for the ical feed
|
||||
func GetPlanForModules(app *pocketbase.PocketBase, modules []model.FeedCollection) model.Events {
|
||||
|
||||
// build query functions with name equals elements in modules for dbx query
|
||||
|
||||
var queryString string
|
||||
for i, module := range modules {
|
||||
if i == 0 {
|
||||
queryString = "Name = '" + module.Name + "' AND course = '" + module.Course + "'"
|
||||
} else {
|
||||
queryString = queryString + " OR Name = '" + module.Name + "' AND course = '" + module.Course + "'"
|
||||
}
|
||||
}
|
||||
var selectedModulesQuery = buildIcalQueryForModules(modules)
|
||||
|
||||
var events model.Events
|
||||
// get all events from event records in the events collection
|
||||
err := app.Dao().DB().Select("*").From("events").Where(dbx.NewExp(queryString)).All(&events)
|
||||
err := app.Dao().DB().Select("*").From("events").Where(selectedModulesQuery).All(&events)
|
||||
if err != nil {
|
||||
print("Error while getting events from database: ", err)
|
||||
return nil
|
||||
|
42
backend/service/db/dbEvents_test.go
Normal file
42
backend/service/db/dbEvents_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"github.com/pocketbase/dbx"
|
||||
"htwkalender/model"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_buildIcalQueryForModules(t *testing.T) {
|
||||
type args struct {
|
||||
modules []model.FeedCollection
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want dbx.Expression
|
||||
}{
|
||||
{
|
||||
name: "empty modules",
|
||||
args: args{modules: []model.FeedCollection{}},
|
||||
want: dbx.HashExp{},
|
||||
},
|
||||
{
|
||||
name: "one module",
|
||||
args: args{modules: []model.FeedCollection{{Name: "test", Course: "test"}}},
|
||||
want: dbx.And(dbx.HashExp{"Name": "test"}, dbx.HashExp{"course": "test"}),
|
||||
},
|
||||
{
|
||||
name: "two modules",
|
||||
args: args{modules: []model.FeedCollection{{Name: "test", Course: "test"}, {Name: "test2", Course: "test2"}}},
|
||||
want: dbx.Or(dbx.And(dbx.HashExp{"Name": "test"}, dbx.HashExp{"course": "test"}), dbx.And(dbx.HashExp{"Name": "test2"}, dbx.HashExp{"course": "test2"})),
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := buildIcalQueryForModules(tt.args.modules); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("buildIcalQueryForModules() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user