added function chaining for db query

This commit is contained in:
masterelmar
2023-10-18 11:06:35 +02:00
parent fbba80fc67
commit b3d9f32fa1
2 changed files with 79 additions and 11 deletions

View File

@@ -77,24 +77,50 @@ func findEventByDayWeekStartEndNameCourse(event model.Event, course string, app
return &event, err 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 // GetPlanForModules returns all events for the given modules with the given course
// used for the ical feed // used for the ical feed
func GetPlanForModules(app *pocketbase.PocketBase, modules []model.FeedCollection) model.Events { func GetPlanForModules(app *pocketbase.PocketBase, modules []model.FeedCollection) model.Events {
// build query functions with name equals elements in modules for dbx query var selectedModulesQuery = buildIcalQueryForModules(modules)
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 events model.Events var events model.Events
// get all events from event records in the events collection // 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 { if err != nil {
print("Error while getting events from database: ", err) print("Error while getting events from database: ", err)
return nil return nil

View 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)
}
})
}
}