Files
htwkalender/backend/service/feed/feedFunctions.go
2024-04-02 11:34:47 +02:00

53 lines
1.8 KiB
Go

//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 feed
import (
"database/sql"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/daos"
database "htwkalender/service/db"
localTime "htwkalender/service/functions/time"
"log/slog"
)
func ClearFeeds(db *daos.Dao, months int, clock localTime.Clock) {
feeds, err := database.GetAllFeeds(db)
if err != nil {
slog.Error("CleanFeeds: failed to get all feeds", err)
return
}
for _, feed := range feeds {
// if retrieved time is older than a half year delete feed
now := clock.Now()
feedRetrievedTime := feed.Retrieved.Time()
timeShift := now.AddDate(0, -months, 0)
if feedRetrievedTime.Before(timeShift) {
// delete feed
var sqlResult sql.Result
sqlResult, err = db.DB().Delete("feeds", dbx.NewExp("id = {:id}", dbx.Params{"id": feed.GetId()})).Execute()
if err != nil {
slog.Error("CleanFeeds: delete feed "+feed.GetId()+" failed", err)
slog.Error("SQL Result: ", sqlResult)
} else {
slog.Info("CleanFeeds: delete feed " + feed.GetId() + " successful")
}
}
}
}