mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender.git
synced 2025-08-03 02:09:15 +02:00
feat:#34 refactored function to intended service, fixed docker files
This commit is contained in:
400
services/data-manager/service/db/dbEvents.go
Normal file
400
services/data-manager/service/db/dbEvents.go
Normal file
@@ -0,0 +1,400 @@
|
||||
//Calendar implementation for the HTWK Leipzig timetable. Evaluation and display of the individual dates in iCal format.
|
||||
//Copyright (C) 2024 HTWKalender support@htwkalender.de
|
||||
|
||||
//This program is free software: you can redistribute it and/or modify
|
||||
//it under the terms of the GNU Affero General Public License as published by
|
||||
//the Free Software Foundation, either version 3 of the License, or
|
||||
//(at your option) any later version.
|
||||
|
||||
//This program is distributed in the hope that it will be useful,
|
||||
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
//GNU Affero General Public License for more details.
|
||||
|
||||
//You should have received a copy of the GNU Affero General Public License
|
||||
//along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/pocketbase/pocketbase/tools/types"
|
||||
"htwkalender/model"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/pocketbase/dbx"
|
||||
"github.com/pocketbase/pocketbase"
|
||||
)
|
||||
|
||||
func SaveSeminarGroupEvents(seminarGroup model.SeminarGroup, app *pocketbase.PocketBase) ([]model.Event, error) {
|
||||
var toBeSavedEvents model.Events
|
||||
var savedRecords model.Events
|
||||
|
||||
// check if event is already in database and add to toBeSavedEvents if not
|
||||
for _, event := range seminarGroup.Events {
|
||||
event = event.SetCourse(seminarGroup.Course)
|
||||
existsInDatabase, err := findEventByDayWeekStartEndNameCourse(event, seminarGroup.Course, app)
|
||||
alreadyAddedToSave := toBeSavedEvents.Contains(event)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !existsInDatabase && !alreadyAddedToSave {
|
||||
toBeSavedEvents = append(toBeSavedEvents, event)
|
||||
}
|
||||
}
|
||||
|
||||
// create record for each event that's not already in the database
|
||||
for _, event := range toBeSavedEvents {
|
||||
event.MarkAsNew()
|
||||
// auto mapping for event fields to record fields
|
||||
err := app.Dao().Save(&event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
savedRecords = append(savedRecords, event)
|
||||
}
|
||||
}
|
||||
|
||||
return savedRecords, nil
|
||||
}
|
||||
|
||||
func SaveEvents(events []model.Event, app *pocketbase.PocketBase) ([]model.Event, error) {
|
||||
var toBeSavedEvents model.Events
|
||||
var savedRecords model.Events
|
||||
|
||||
// check if event is already in database and add to toBeSavedEvents if not
|
||||
for _, event := range events {
|
||||
existsInDatabase, err := findEventByDayWeekStartEndNameCourse(event, event.Course, app)
|
||||
alreadyAddedToSave := toBeSavedEvents.Contains(event)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !existsInDatabase && !alreadyAddedToSave {
|
||||
toBeSavedEvents = append(toBeSavedEvents, event)
|
||||
}
|
||||
}
|
||||
|
||||
// create record for each event that's not already in the database
|
||||
for _, event := range toBeSavedEvents {
|
||||
event.MarkAsNew()
|
||||
// auto mapping for event fields to record fields
|
||||
err := app.Dao().Save(&event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
savedRecords = append(savedRecords, event)
|
||||
}
|
||||
}
|
||||
return savedRecords, nil
|
||||
}
|
||||
|
||||
// check if event is already in database and return true if it is and false if it's not
|
||||
func findEventByDayWeekStartEndNameCourse(event model.Event, course string, app *pocketbase.PocketBase) (bool, error) {
|
||||
|
||||
var dbEvent model.Event
|
||||
|
||||
err := app.Dao().DB().Select("*").From("events").
|
||||
Where(dbx.NewExp(
|
||||
"Day = {:day} AND "+
|
||||
"Week = {:week} AND "+
|
||||
"Start = {:start} AND "+
|
||||
"End = {:end} AND "+
|
||||
"Name = {:name} AND "+
|
||||
"course = {:course} AND "+
|
||||
"Prof = {:prof} AND "+
|
||||
"Rooms = {:rooms} AND "+
|
||||
"EventType = {:eventType}",
|
||||
dbx.Params{
|
||||
"day": event.Day,
|
||||
"week": event.Week,
|
||||
"start": event.Start,
|
||||
"end": event.End,
|
||||
"name": event.Name,
|
||||
"course": course,
|
||||
"prof": event.Prof,
|
||||
"rooms": event.Rooms,
|
||||
"eventType": event.EventType}),
|
||||
).One(&dbEvent)
|
||||
|
||||
if err != nil {
|
||||
if err.Error() == "sql: no rows in result set" {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
} else {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
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.HashExp{"uuid": modules[0].UUID}
|
||||
}
|
||||
|
||||
//third check if modules has more than one element
|
||||
var wheres []dbx.Expression
|
||||
|
||||
for _, module := range modules {
|
||||
where := dbx.HashExp{"uuid": module.UUID}
|
||||
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 map[string]model.FeedCollection) (model.Events, error) {
|
||||
|
||||
var events model.Events
|
||||
|
||||
modulesArray := make([]model.FeedCollection, 0, len(modules))
|
||||
for _, value := range modules {
|
||||
modulesArray = append(modulesArray, value)
|
||||
}
|
||||
|
||||
// iterate over modules in 100 batch sizes
|
||||
for i := 0; i < len(modules); i += 100 {
|
||||
var moduleBatch []model.FeedCollection
|
||||
|
||||
if i+100 > len(modules) {
|
||||
moduleBatch = modulesArray[i:]
|
||||
} else {
|
||||
moduleBatch = modulesArray[i : i+100]
|
||||
}
|
||||
|
||||
var selectedModulesQuery = buildIcalQueryForModules(moduleBatch)
|
||||
// get all events from event records in the events collection
|
||||
err := app.Dao().DB().Select("*").From("events").Where(selectedModulesQuery).OrderBy("start").All(&events)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func GetAllEventsForCourse(app *pocketbase.PocketBase, course string) (model.Events, error) {
|
||||
var events model.Events
|
||||
|
||||
// get all events from event records in the events collection
|
||||
err := app.Dao().DB().Select("*").From("events").Where(dbx.NewExp("course = {:course}", dbx.Params{"course": course})).All(&events)
|
||||
if err != nil {
|
||||
slog.Error("Error while getting events from database: ", "error", err)
|
||||
return nil, fmt.Errorf("error while getting events from database for course %s", course)
|
||||
}
|
||||
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func GetAllModulesForCourse(app *pocketbase.PocketBase, course string, semester string) (model.Events, error) {
|
||||
var events model.Events
|
||||
|
||||
// get all events from event records in the events collection
|
||||
err := app.Dao().DB().Select("*").From("events").Where(dbx.NewExp("course = {:course} AND semester = {:semester}", dbx.Params{"course": course, "semester": semester})).GroupBy("Name").Distinct(true).All(&events)
|
||||
if err != nil {
|
||||
slog.Error("Error while getting events from database: ", "error", err)
|
||||
return nil, fmt.Errorf("error while getting events from database for course %s and semester %s", course, semester)
|
||||
}
|
||||
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func GetAllModulesDistinctByNameAndCourse(app *pocketbase.PocketBase) ([]model.ModuleDTO, error) {
|
||||
var modules []model.ModuleDTO
|
||||
|
||||
err := app.Dao().DB().Select("Name", "EventType", "Prof", "course", "semester", "uuid").From("events").GroupBy("Name", "Course").Distinct(true).All(&modules)
|
||||
if err != nil {
|
||||
slog.Error("Error while getting events from database: ", "error", err)
|
||||
return nil, fmt.Errorf("error while getting events distinct by name and course from data")
|
||||
}
|
||||
|
||||
return modules, nil
|
||||
}
|
||||
|
||||
func DeleteAllEventsByCourse(app *pocketbase.PocketBase, course string, semester string) error {
|
||||
_, err := app.Dao().DB().Delete("events", dbx.NewExp("course = {:course} AND semester = {:semester}", dbx.Params{"course": course, "semester": semester})).Execute()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeleteAllEventsBySemesterWithoutCourse(app *pocketbase.PocketBase, course string, semester string) error {
|
||||
_, err := app.Dao().DB().Delete("events", dbx.NewExp("course != {:course} AND semester = {:semester}", dbx.Params{"course": course, "semester": semester})).Execute()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeleteAllEvents(app *pocketbase.PocketBase) error {
|
||||
|
||||
_, err := app.Dao().DB().Delete("events", dbx.NewExp("1=1")).Execute()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func FindModuleByUUID(app *pocketbase.PocketBase, uuid string) (model.Module, error) {
|
||||
var module model.Module
|
||||
|
||||
err := app.Dao().DB().Select("*").From("events").Where(dbx.NewExp("uuid = {:uuid}", dbx.Params{"uuid": uuid})).One(&module)
|
||||
if err != nil {
|
||||
return model.Module{}, err
|
||||
}
|
||||
|
||||
return module, nil
|
||||
}
|
||||
|
||||
func FindAllEventsByModule(app *pocketbase.PocketBase, module model.Module) (model.Events, error) {
|
||||
var events model.Events
|
||||
|
||||
err := app.Dao().DB().Select("*").From("events").Where(dbx.NewExp("Name = {:moduleName} AND course = {:course}", dbx.Params{"moduleName": module.Name, "course": module.Course})).All(&events)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func GetAllModulesByNameAndDateRange(app *pocketbase.PocketBase, name string, startDate time.Time, endDate time.Time) (model.Events, error) {
|
||||
var events model.Events
|
||||
|
||||
err := app.Dao().DB().Select("*").From("events").Where(dbx.NewExp("Name = {:name} AND Start >= {:startDate} AND End <= {:endDate}", dbx.Params{"name": name, "startDate": startDate, "endDate": endDate})).All(&events)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return events, nil
|
||||
}
|
||||
|
||||
// GetEventsThatCollideWithTimeRange returns all events that collide with the given time range
|
||||
// we have events with start and end in the database, we want to get all events that collide with the given time range
|
||||
// we have 4 cases:
|
||||
// 1. event starts before the given time range and ends after the given time range
|
||||
// 2. event starts after the given time range and ends before the given time range
|
||||
// 3. event starts before the given time range and ends before the given time range
|
||||
// 4. event starts after the given time range and ends after the given time range
|
||||
func GetEventsThatCollideWithTimeRange(app *pocketbase.PocketBase, from time.Time, to time.Time) (model.Events, error) {
|
||||
var fromTypeTime, _ = types.ParseDateTime(from)
|
||||
var toTypeTime, _ = types.ParseDateTime(to)
|
||||
|
||||
events1, err := GetEventsThatStartBeforeAndEndAfter(app, fromTypeTime, toTypeTime)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
events2, err := GetEventsThatStartAfterAndEndBefore(app, fromTypeTime, toTypeTime)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
events3, err := GetEventsThatStartBeforeAndEndBefore(app, fromTypeTime, toTypeTime)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
events4, err := GetEventsThatStartAfterAndEndAfter(app, fromTypeTime, toTypeTime)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var events model.Events
|
||||
events = append(events, events1...)
|
||||
events = append(events, events2...)
|
||||
events = append(events, events3...)
|
||||
events = append(events, events4...)
|
||||
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func GetEventsThatStartBeforeAndEndAfter(app *pocketbase.PocketBase, from types.DateTime, to types.DateTime) (model.Events, error) {
|
||||
var events model.Events
|
||||
|
||||
err := app.Dao().DB().Select("*").From("events").Where(dbx.NewExp("Start <= {:startDate} AND End >= {:endDate} AND Start <= {:endDate} AND End >= {:startDate}", dbx.Params{"startDate": from, "endDate": to})).Distinct(true).All(&events)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func GetEventsThatStartAfterAndEndBefore(app *pocketbase.PocketBase, from types.DateTime, to types.DateTime) (model.Events, error) {
|
||||
var events model.Events
|
||||
|
||||
err := app.Dao().DB().Select("*").From("events").Where(dbx.NewExp("Start >= {:startDate} AND End <= {:endDate} AND Start <= {:endDate} AND End >= {:startDate}", dbx.Params{"startDate": from, "endDate": to})).All(&events)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func GetEventsThatStartBeforeAndEndBefore(app *pocketbase.PocketBase, from types.DateTime, to types.DateTime) (model.Events, error) {
|
||||
var events model.Events
|
||||
|
||||
err := app.Dao().DB().Select("*").From("events").Where(dbx.NewExp("Start <= {:startDate} AND End <= {:endDate} AND Start <= {:endDate} AND End >= {:startDate}", dbx.Params{"startDate": from, "endDate": to})).All(&events)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func GetAllEventTypes(app *pocketbase.PocketBase) ([]model.EventType, error) {
|
||||
var eventTypes []model.EventType
|
||||
|
||||
err := app.Dao().DB().Select("EventType").From("events").GroupBy("EventType").Distinct(true).All(&eventTypes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return eventTypes, nil
|
||||
}
|
||||
|
||||
func GetEventsThatStartAfterAndEndAfter(app *pocketbase.PocketBase, from types.DateTime, to types.DateTime) (model.Events, error) {
|
||||
var events model.Events
|
||||
err := app.Dao().DB().Select("*").From("events").Where(dbx.NewExp("Start >= {:startDate} AND End >= {:endDate} AND Start <= {:endDate} AND End >= {:startDate}", dbx.Params{"startDate": from, "endDate": to})).All(&events)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func DeleteEvents(list model.Events, app *pocketbase.PocketBase) error {
|
||||
for _, event := range list {
|
||||
err := app.Dao().Delete(&event)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
58
services/data-manager/service/db/dbEvents_test.go
Normal file
58
services/data-manager/service/db/dbEvents_test.go
Normal file
@@ -0,0 +1,58 @@
|
||||
//Calendar implementation for the HTWK Leipzig timetable. Evaluation and display of the individual dates in iCal format.
|
||||
//Copyright (C) 2024 HTWKalender support@htwkalender.de
|
||||
|
||||
//This program is free software: you can redistribute it and/or modify
|
||||
//it under the terms of the GNU Affero General Public License as published by
|
||||
//the Free Software Foundation, either version 3 of the License, or
|
||||
//(at your option) any later version.
|
||||
|
||||
//This program is distributed in the hope that it will be useful,
|
||||
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
//GNU Affero General Public License for more details.
|
||||
|
||||
//You should have received a copy of the GNU Affero General Public License
|
||||
//along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
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", UUID: "test"}}},
|
||||
want: dbx.HashExp{"uuid": "test"},
|
||||
},
|
||||
{
|
||||
name: "two modules",
|
||||
args: args{modules: []model.FeedCollection{{Name: "test", Course: "test", UUID: "test"}, {Name: "test2", Course: "test2", UUID: "test2"}}},
|
||||
want: dbx.Or(dbx.HashExp{"uuid": "test"}, dbx.HashExp{"uuid": "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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
86
services/data-manager/service/db/dbFeeds.go
Normal file
86
services/data-manager/service/db/dbFeeds.go
Normal file
@@ -0,0 +1,86 @@
|
||||
//Calendar implementation for the HTWK Leipzig timetable. Evaluation and display of the individual dates in iCal format.
|
||||
//Copyright (C) 2024 HTWKalender support@htwkalender.de
|
||||
|
||||
//This program is free software: you can redistribute it and/or modify
|
||||
//it under the terms of the GNU Affero General Public License as published by
|
||||
//the Free Software Foundation, either version 3 of the License, or
|
||||
//(at your option) any later version.
|
||||
|
||||
//This program is distributed in the hope that it will be useful,
|
||||
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
//GNU Affero General Public License for more details.
|
||||
|
||||
//You should have received a copy of the GNU Affero General Public License
|
||||
//along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/pocketbase/dbx"
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/daos"
|
||||
"github.com/pocketbase/pocketbase/models"
|
||||
"htwkalender/model"
|
||||
"time"
|
||||
)
|
||||
|
||||
func SaveFeed(feed model.Feed, collection *models.Collection, app *pocketbase.PocketBase) (*models.Record, error) {
|
||||
record := models.NewRecord(collection)
|
||||
record.Set("modules", feed.Modules)
|
||||
err := app.Dao().SaveRecord(record)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return record, nil
|
||||
}
|
||||
|
||||
func FindFeedByToken(token string, app *pocketbase.PocketBase) (*model.Feed, error) {
|
||||
|
||||
record, err := app.Dao().FindRecordById("feeds", token)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var feed model.Feed
|
||||
feed.Modules = record.GetString("modules")
|
||||
feed.Retrieved = record.GetDateTime("retrieved")
|
||||
|
||||
//update retrieved time
|
||||
record.Set("retrieved", time.Now())
|
||||
|
||||
err = app.Dao().SaveRecord(record)
|
||||
|
||||
return &feed, err
|
||||
}
|
||||
|
||||
func DeleteFeed(db *daos.Dao, feedId string) error {
|
||||
|
||||
sqlResult, err := db.DB().Delete("feeds", dbx.NewExp("id = {:id}", dbx.Params{"id": feedId})).Execute()
|
||||
var deletedRows int64
|
||||
if sqlResult != nil {
|
||||
deletedRows, _ = sqlResult.RowsAffected()
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
if deletedRows == 0 {
|
||||
return errors.New("No feed with id " + feedId + " found")
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func GetAllFeeds(db *daos.Dao) ([]model.Feed, error) {
|
||||
var feeds []model.Feed
|
||||
err := db.DB().Select("*").From("feeds").All(&feeds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return feeds, nil
|
||||
}
|
71
services/data-manager/service/db/dbFeeds_test.go
Normal file
71
services/data-manager/service/db/dbFeeds_test.go
Normal file
@@ -0,0 +1,71 @@
|
||||
//Calendar implementation for the HTWK Leipzig timetable. Evaluation and display of the individual dates in iCal format.
|
||||
//Copyright (C) 2024 HTWKalender support@htwkalender.de
|
||||
|
||||
//This program is free software: you can redistribute it and/or modify
|
||||
//it under the terms of the GNU Affero General Public License as published by
|
||||
//the Free Software Foundation, either version 3 of the License, or
|
||||
//(at your option) any later version.
|
||||
|
||||
//This program is distributed in the hope that it will be useful,
|
||||
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
//GNU Affero General Public License for more details.
|
||||
|
||||
//You should have received a copy of the GNU Affero General Public License
|
||||
//along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"github.com/pocketbase/pocketbase/daos"
|
||||
"github.com/pocketbase/pocketbase/tests"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const testDataDir = "./mockData"
|
||||
|
||||
func TestDeleteFeed(t *testing.T) {
|
||||
|
||||
setupTestApp := func(t *testing.T) *daos.Dao {
|
||||
testApp, err := tests.NewTestApp(testDataDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dao := daos.New(testApp.Dao().DB())
|
||||
return dao
|
||||
}
|
||||
|
||||
type args struct {
|
||||
db *daos.Dao
|
||||
feedId string
|
||||
}
|
||||
testsCases := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "TestDeleteFeed",
|
||||
args: args{
|
||||
db: setupTestApp(t),
|
||||
feedId: "fkoqti06ohlnsb8",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "TestDeleteFeedNotExisting",
|
||||
args: args{
|
||||
db: setupTestApp(t),
|
||||
feedId: "test324",
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range testsCases {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := DeleteFeed(tt.args.db, tt.args.feedId); (err != nil) != tt.wantErr {
|
||||
t.Errorf("DeleteFeed() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
27
services/data-manager/service/db/dbFunctions.go
Normal file
27
services/data-manager/service/db/dbFunctions.go
Normal file
@@ -0,0 +1,27 @@
|
||||
//Calendar implementation for the HTWK Leipzig timetable. Evaluation and display of the individual dates in iCal format.
|
||||
//Copyright (C) 2024 HTWKalender support@htwkalender.de
|
||||
|
||||
//This program is free software: you can redistribute it and/or modify
|
||||
//it under the terms of the GNU Affero General Public License as published by
|
||||
//the Free Software Foundation, either version 3 of the License, or
|
||||
//(at your option) any later version.
|
||||
|
||||
//This program is distributed in the hope that it will be useful,
|
||||
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
//GNU Affero General Public License for more details.
|
||||
|
||||
//You should have received a copy of the GNU Affero General Public License
|
||||
//along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/models"
|
||||
)
|
||||
|
||||
func FindCollection(app *pocketbase.PocketBase, collectionName string) (*models.Collection, error) {
|
||||
collection, dbError := app.Dao().FindCollectionByNameOrId(collectionName)
|
||||
return collection, dbError
|
||||
}
|
144
services/data-manager/service/db/dbGroups.go
Normal file
144
services/data-manager/service/db/dbGroups.go
Normal file
@@ -0,0 +1,144 @@
|
||||
//Calendar implementation for the HTWK Leipzig timetable. Evaluation and display of the individual dates in iCal format.
|
||||
//Copyright (C) 2024 HTWKalender support@htwkalender.de
|
||||
|
||||
//This program is free software: you can redistribute it and/or modify
|
||||
//it under the terms of the GNU Affero General Public License as published by
|
||||
//the Free Software Foundation, either version 3 of the License, or
|
||||
//(at your option) any later version.
|
||||
|
||||
//This program is distributed in the hope that it will be useful,
|
||||
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
//GNU Affero General Public License for more details.
|
||||
|
||||
//You should have received a copy of the GNU Affero General Public License
|
||||
//along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"github.com/pocketbase/dbx"
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/models"
|
||||
"htwkalender/model"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
func SaveGroups(seminarGroup []model.SeminarGroup, collection *models.Collection, app *pocketbase.PocketBase) ([]*models.Record, error) {
|
||||
var savedRecords []*models.Record
|
||||
var tobeSavedGroups []model.SeminarGroup
|
||||
var insertRecords []*models.Record
|
||||
|
||||
for _, group := range seminarGroup {
|
||||
dbGroup, err := FindGroupByCourseAndSemester(group.Course, group.Semester, app)
|
||||
|
||||
if dbGroup == nil && err.Error() == "sql: no rows in result set" {
|
||||
tobeSavedGroups = append(tobeSavedGroups, group)
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// create record for each group that's not already in the database
|
||||
for _, group := range tobeSavedGroups {
|
||||
record := models.NewRecord(collection)
|
||||
record.Set("university", group.University)
|
||||
record.Set("shortcut", group.GroupShortcut)
|
||||
record.Set("groupId", group.GroupId)
|
||||
record.Set("course", group.Course)
|
||||
record.Set("faculty", group.Faculty)
|
||||
record.Set("facultyId", group.FacultyId)
|
||||
record.Set("semester", group.Semester)
|
||||
insertRecords = append(insertRecords, record)
|
||||
}
|
||||
|
||||
// save all records
|
||||
for _, record := range insertRecords {
|
||||
if record != nil {
|
||||
err := app.Dao().SaveRecord(record)
|
||||
if err == nil {
|
||||
savedRecords = append(savedRecords, record)
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return savedRecords, nil
|
||||
}
|
||||
|
||||
func FindGroupByCourseAndSemester(course string, semester string, app *pocketbase.PocketBase) (*model.SeminarGroup, error) {
|
||||
var group model.SeminarGroup
|
||||
err := app.Dao().DB().Select("*").From("groups").Where(dbx.NewExp("course = {:course} AND semester = {:semester}", dbx.Params{"course": course, "semester": semester})).One(&group)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &group, nil
|
||||
}
|
||||
|
||||
func GetAllCourses(app *pocketbase.PocketBase) []string {
|
||||
|
||||
var courses []struct {
|
||||
CourseShortcut string `db:"course" json:"course"`
|
||||
}
|
||||
|
||||
// get all rooms from event records in the events collection
|
||||
err := app.Dao().DB().Select("course").From("groups").All(&courses)
|
||||
if err != nil {
|
||||
slog.Error("Error while getting groups from database: ", "error", err)
|
||||
return []string{}
|
||||
}
|
||||
|
||||
var courseArray []string
|
||||
|
||||
for _, course := range courses {
|
||||
courseArray = append(courseArray, course.CourseShortcut)
|
||||
}
|
||||
|
||||
return courseArray
|
||||
}
|
||||
|
||||
func GetAllCoursesForSemester(app *pocketbase.PocketBase, semester string) []string {
|
||||
|
||||
var courses []struct {
|
||||
CourseShortcut string `db:"course" json:"course"`
|
||||
}
|
||||
|
||||
// get all courses for a specific semester
|
||||
err := app.Dao().DB().Select("course").From("groups").Where(dbx.NewExp("semester = {:semester}", dbx.Params{"semester": semester})).All(&courses)
|
||||
if err != nil {
|
||||
slog.Error("Error while getting groups from database: ", "error", err)
|
||||
return []string{}
|
||||
}
|
||||
|
||||
var courseArray []string
|
||||
|
||||
for _, course := range courses {
|
||||
courseArray = append(courseArray, course.CourseShortcut)
|
||||
}
|
||||
|
||||
return courseArray
|
||||
|
||||
}
|
||||
|
||||
func GetAllCoursesForSemesterWithEvents(app *pocketbase.PocketBase, semester string) ([]string, error) {
|
||||
|
||||
var courses []struct {
|
||||
CourseShortcut string `db:"course" json:"course"`
|
||||
}
|
||||
|
||||
// get all courses from events distinct for a specific semester
|
||||
err := app.Dao().DB().Select("course").From("events").Where(dbx.NewExp("semester = {:semester}", dbx.Params{"semester": semester})).Distinct(true).All(&courses)
|
||||
if err != nil {
|
||||
slog.Error("Error while getting groups from database: ", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var courseArray []string
|
||||
|
||||
for _, course := range courses {
|
||||
courseArray = append(courseArray, course.CourseShortcut)
|
||||
}
|
||||
|
||||
return courseArray, nil
|
||||
}
|
120
services/data-manager/service/db/dbRooms.go
Normal file
120
services/data-manager/service/db/dbRooms.go
Normal file
@@ -0,0 +1,120 @@
|
||||
//Calendar implementation for the HTWK Leipzig timetable. Evaluation and display of the individual dates in iCal format.
|
||||
//Copyright (C) 2024 HTWKalender support@htwkalender.de
|
||||
|
||||
//This program is free software: you can redistribute it and/or modify
|
||||
//it under the terms of the GNU Affero General Public License as published by
|
||||
//the Free Software Foundation, either version 3 of the License, or
|
||||
//(at your option) any later version.
|
||||
|
||||
//This program is distributed in the hope that it will be useful,
|
||||
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
//GNU Affero General Public License for more details.
|
||||
|
||||
//You should have received a copy of the GNU Affero General Public License
|
||||
//along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"htwkalender/model"
|
||||
"htwkalender/service/functions"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pocketbase/dbx"
|
||||
"github.com/pocketbase/pocketbase"
|
||||
)
|
||||
|
||||
func GetRooms(app *pocketbase.PocketBase) ([]string, error) {
|
||||
|
||||
var events []struct {
|
||||
Rooms string `db:"Rooms" json:"Rooms"`
|
||||
Course string `db:"course" json:"Course"`
|
||||
}
|
||||
|
||||
// get all rooms from event records in the events collection
|
||||
err := app.Dao().DB().Select("Rooms", "course").From("events").Distinct(true).All(&events)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
roomArray, err := clearAndSeparateRooms([]struct {
|
||||
Rooms string
|
||||
Course string
|
||||
}(events))
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return roomArray, nil
|
||||
}
|
||||
|
||||
func clearAndSeparateRooms(events []struct {
|
||||
Rooms string
|
||||
Course string
|
||||
}) ([]string, error) {
|
||||
var roomArray []string
|
||||
for _, event := range events {
|
||||
|
||||
var room []string
|
||||
|
||||
// sport rooms don't have to be separated
|
||||
if event.Course != "Sport" {
|
||||
//split rooms by comma, tab, newline, carriage return, semicolon, space and non-breaking space
|
||||
room = functions.SeperateRoomString(event.Rooms)
|
||||
} else {
|
||||
room = append(room, event.Rooms)
|
||||
}
|
||||
|
||||
//split functions room by space and add each room to array if it is not already in there
|
||||
for _, r := range room {
|
||||
var text = strings.TrimSpace(r)
|
||||
if !functions.Contains(roomArray, text) && len(text) >= 1 {
|
||||
roomArray = append(roomArray, text)
|
||||
}
|
||||
}
|
||||
}
|
||||
return roomArray, nil
|
||||
}
|
||||
|
||||
func GetRoomScheduleForDay(app *pocketbase.PocketBase, room string, date string) ([]model.Event, error) {
|
||||
var events []model.Event
|
||||
|
||||
// get all events from event records in the events collection
|
||||
err := app.Dao().DB().Select("*").From("events").
|
||||
Where(dbx.Like("Rooms", room).Escape("_", "_")).
|
||||
AndWhere(dbx.Like("Start", date)).
|
||||
GroupBy("Week", "Start", "End", "Rooms").
|
||||
All(&events)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func GetRoomSchedule(app *pocketbase.PocketBase, room string, from string, to string) ([]model.Event, error) {
|
||||
var events []model.Event
|
||||
|
||||
fromDate, err := time.Parse("2006-01-02", from)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
toDate, err := time.Parse("2006-01-02", to)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// get all events from event records in the events collection
|
||||
err = app.Dao().DB().Select("*").From("events").
|
||||
Where(dbx.Like("Rooms", room).Escape("_", "_")).
|
||||
AndWhere(dbx.Between("Start", fromDate, toDate)).
|
||||
GroupBy("Week", "Start", "End", "Rooms").
|
||||
All(&events)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return events, nil
|
||||
}
|
BIN
services/data-manager/service/db/mockData/data.db
Normal file
BIN
services/data-manager/service/db/mockData/data.db
Normal file
Binary file not shown.
BIN
services/data-manager/service/db/mockData/logs.db
Normal file
BIN
services/data-manager/service/db/mockData/logs.db
Normal file
Binary file not shown.
Reference in New Issue
Block a user