feat:#44 updated delete process and api response

This commit is contained in:
Elmar Kresse
2024-07-13 16:46:28 +02:00
parent 0addcd72a6
commit 1f9e9614aa
17 changed files with 226 additions and 15 deletions

View File

@ -21,7 +21,7 @@ export async function getCalender(token: string): Promise<Module[]> {
if (import.meta.env.SSR) { if (import.meta.env.SSR) {
return []; return [];
} }
const request = new Request("/api/collections/feeds/records/" + token, { const request = new Request("/api/feeds/records/" + token, {
method: "GET", method: "GET",
}); });

View File

@ -24,9 +24,14 @@ import (
type Feed struct { type Feed struct {
Modules string `db:"modules" json:"modules"` Modules string `db:"modules" json:"modules"`
Retrieved types.DateTime `db:"retrieved" json:"retrieved"` Retrieved types.DateTime `db:"retrieved" json:"retrieved"`
Deleted bool `db:"deleted" json:"deleted"`
models.BaseModel models.BaseModel
} }
func (f *Feed) TableName() string {
return "feeds"
}
// SetModules set modules field // SetModules set modules field
func (f *Feed) SetModules(modules string) { func (f *Feed) SetModules(modules string) {
f.Modules = modules f.Modules = modules

View File

@ -21,7 +21,7 @@ import (
"github.com/pocketbase/pocketbase" "github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis" "github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core" "github.com/pocketbase/pocketbase/core"
"htwkalender/data-manager/service/db" "htwkalender/data-manager/service/feed"
"htwkalender/data-manager/service/ical" "htwkalender/data-manager/service/ical"
"io" "io"
"log/slog" "log/slog"
@ -58,7 +58,7 @@ func addFeedRoutes(app *pocketbase.PocketBase) {
Path: "/api/feed", Path: "/api/feed",
Handler: func(c echo.Context) error { Handler: func(c echo.Context) error {
token := c.QueryParam("token") token := c.QueryParam("token")
err := db.DeleteFeed(app.Dao(), token) err := feed.MarkFeedForDeletion(app.Dao(), token)
if err != nil { if err != nil {
return c.JSON(http.StatusNotFound, err) return c.JSON(http.StatusNotFound, err)
} else { } else {

View File

@ -84,3 +84,22 @@ func GetAllFeeds(db *daos.Dao) ([]model.Feed, error) {
} }
return feeds, nil return feeds, nil
} }
func MarkForDelete(db *daos.Dao, token string) error {
// get record from db
feed := model.Feed{}
err := db.DB().Select("*").From("feeds").Where(dbx.NewExp("id = {:id}", dbx.Params{"id": token})).One(&feed)
if err != nil {
return err
}
// set delete flag
feed.Deleted = true
feed.Modules = "[\n {\n \"uuid\": \"\",\n \"name\": \"Deleted\",\n \"course\": \"\",\n \"userDefinedName\": \"Deleted\",\n \"reminder\": false\n }\n]"
// save record
err = db.Save(&feed)
if err != nil {
return err
}
return nil
}

View File

@ -17,8 +17,6 @@
package feed package feed
import ( import (
"database/sql"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/daos" "github.com/pocketbase/pocketbase/daos"
"htwkalender/data-manager/model" "htwkalender/data-manager/model"
database "htwkalender/data-manager/service/db" database "htwkalender/data-manager/service/db"
@ -41,13 +39,9 @@ func ClearFeeds(db *daos.Dao, months int, clock localTime.Clock) {
if feedRetrievedTime.Before(timeShift) { if feedRetrievedTime.Before(timeShift) {
// delete feed // delete feed
var sqlResult sql.Result feedErr := database.DeleteFeed(db, feed.GetId())
sqlResult, err = db.DB().Delete("feeds", dbx.NewExp("id = {:id}", dbx.Params{"id": feed.GetId()})).Execute() if feedErr != nil {
if err != nil { slog.Error("CleanFeeds: failed to delete feed: "+feed.GetId(), "error", feedErr)
slog.Error("CleanFeeds: delete feed "+feed.GetId()+" failed", "error", err)
slog.Error("SQL Result: ", "error", sqlResult)
} else {
slog.Info("CleanFeeds: delete feed " + feed.GetId() + " successful")
} }
} }
} }
@ -124,3 +118,7 @@ func combineRooms(events model.Events, index1 int, combinedEvents model.Events,
} }
return combinedEvents[index2].Rooms return combinedEvents[index2].Rooms
} }
func MarkFeedForDeletion(db *daos.Dao, feedId string) error {
return database.MarkForDelete(db, feedId)
}

View File

@ -1,3 +1,19 @@
//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 model package model
import ( import (

View File

@ -61,6 +61,7 @@ type FeedModule struct {
type FeedRecord struct { type FeedRecord struct {
Modules []FeedModule `db:"modules" json:"modules"` Modules []FeedModule `db:"modules" json:"modules"`
Retrieved JSONTime `db:"retrieved" json:"retrieved"` Retrieved JSONTime `db:"retrieved" json:"retrieved"`
Deleted bool `db:"deleted" json:"deleted"`
BaseModel BaseModel
} }

View File

@ -1,3 +1,19 @@
//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 model package model
type Module struct { type Module struct {

View File

@ -1,3 +1,19 @@
//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 connector package connector
import ( import (

View File

@ -1,3 +1,19 @@
//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 grpc package grpc
import ( import (

View File

@ -1,3 +1,19 @@
//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 grpc package grpc
import ( import (

View File

@ -1,3 +1,19 @@
//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 grpc package grpc
import ( import (

View File

@ -1,3 +1,19 @@
//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 grpc package grpc
import ( import (

View File

@ -1,3 +1,19 @@
//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 connector package connector
import ( import (

View File

@ -1,3 +1,19 @@
//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 functions package functions
import ( import (

View File

@ -1,7 +1,24 @@
//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 ical package ical
import ( import (
"bytes" "bytes"
"fmt"
"github.com/jordic/goics" "github.com/jordic/goics"
"htwkalender/ical/model" "htwkalender/ical/model"
"htwkalender/ical/service/connector" "htwkalender/ical/service/connector"
@ -12,6 +29,8 @@ import (
const expirationTime = 5 * time.Minute const expirationTime = 5 * time.Minute
var FeedDeletedError = fmt.Errorf("feed deleted")
func Feed(app model.AppType, token string) (string, error) { func Feed(app model.AppType, token string) (string, error) {
// get feed by token // get feed by token
feed, err := htwkalenderGrpc.GetFeed(token, app.GrpcClient) feed, err := htwkalenderGrpc.GetFeed(token, app.GrpcClient)
@ -19,6 +38,10 @@ func Feed(app model.AppType, token string) (string, error) {
return "", err return "", err
} }
if feed.Deleted {
return "", FeedDeletedError
}
var events model.Events var events model.Events
// Get all events for modules // Get all events for modules
@ -45,7 +68,7 @@ func Feed(app model.AppType, token string) (string, error) {
func FeedRecord(app model.AppType, token string) (model.FeedRecord, error) { func FeedRecord(app model.AppType, token string) (model.FeedRecord, error) {
feedRecord, err := connector.GetFeedByToken(app.DataManagerURL, token) feedRecord, err := connector.GetFeedByToken(app.DataManagerURL, token)
if err != nil { if err != nil || feedRecord.Deleted {
return model.FeedRecord{}, err return model.FeedRecord{}, err
} }

View File

@ -1,7 +1,24 @@
//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 service package service
import ( import (
"encoding/json" "encoding/json"
"errors"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
"htwkalender/ical/model" "htwkalender/ical/model"
"htwkalender/ical/service/ical" "htwkalender/ical/service/ical"
@ -19,9 +36,13 @@ func AddFeedRoutes(app model.AppType) {
token := c.Query("token") token := c.Query("token")
results, err := ical.Feed(app, token) results, err := ical.Feed(app, token)
if errors.Is(err, ical.FeedDeletedError) {
return c.SendStatus(fiber.StatusGone)
}
if err != nil { if err != nil {
slog.Error("Failed to get feed", "error", err, "token", token) slog.Error("Failed to get feed", "error", err, "token", token)
return c.SendStatus(fiber.StatusGone) return c.SendStatus(fiber.StatusNotFound)
} }
c.Response().Header.Set("Content-type", "text/calendar") c.Response().Header.Set("Content-type", "text/calendar")
c.Response().Header.Set("charset", "utf-8") c.Response().Header.Set("charset", "utf-8")
@ -53,7 +74,7 @@ func AddFeedRoutes(app model.AppType) {
}) })
// Define a route for the GET method on the root path '/' // Define a route for the GET method on the root path '/'
app.Fiber.Get("/api/collections/feeds/records/:token", func(c fiber.Ctx) error { app.Fiber.Get("/api/feeds/records/:token", func(c fiber.Ctx) error {
token := c.Params("token") token := c.Params("token")
results, err := ical.FeedRecord(app, token) results, err := ical.FeedRecord(app, token)