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) {
return [];
}
const request = new Request("/api/collections/feeds/records/" + token, {
const request = new Request("/api/feeds/records/" + token, {
method: "GET",
});

View File

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

View File

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

View File

@ -84,3 +84,22 @@ func GetAllFeeds(db *daos.Dao) ([]model.Feed, error) {
}
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
import (
"database/sql"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/daos"
"htwkalender/data-manager/model"
database "htwkalender/data-manager/service/db"
@ -41,13 +39,9 @@ func ClearFeeds(db *daos.Dao, months int, clock localTime.Clock) {
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", "error", err)
slog.Error("SQL Result: ", "error", sqlResult)
} else {
slog.Info("CleanFeeds: delete feed " + feed.GetId() + " successful")
feedErr := database.DeleteFeed(db, feed.GetId())
if feedErr != nil {
slog.Error("CleanFeeds: failed to delete feed: "+feed.GetId(), "error", feedErr)
}
}
}
@ -124,3 +118,7 @@ func combineRooms(events model.Events, index1 int, combinedEvents model.Events,
}
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
import (

View File

@ -61,6 +61,7 @@ type FeedModule struct {
type FeedRecord struct {
Modules []FeedModule `db:"modules" json:"modules"`
Retrieved JSONTime `db:"retrieved" json:"retrieved"`
Deleted bool `db:"deleted" json:"deleted"`
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
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
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
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
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
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
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
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
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
import (
"bytes"
"fmt"
"github.com/jordic/goics"
"htwkalender/ical/model"
"htwkalender/ical/service/connector"
@ -12,6 +29,8 @@ import (
const expirationTime = 5 * time.Minute
var FeedDeletedError = fmt.Errorf("feed deleted")
func Feed(app model.AppType, token string) (string, error) {
// get feed by token
feed, err := htwkalenderGrpc.GetFeed(token, app.GrpcClient)
@ -19,6 +38,10 @@ func Feed(app model.AppType, token string) (string, error) {
return "", err
}
if feed.Deleted {
return "", FeedDeletedError
}
var events model.Events
// 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) {
feedRecord, err := connector.GetFeedByToken(app.DataManagerURL, token)
if err != nil {
if err != nil || feedRecord.Deleted {
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
import (
"encoding/json"
"errors"
"github.com/gofiber/fiber/v3"
"htwkalender/ical/model"
"htwkalender/ical/service/ical"
@ -19,9 +36,13 @@ func AddFeedRoutes(app model.AppType) {
token := c.Query("token")
results, err := ical.Feed(app, token)
if errors.Is(err, ical.FeedDeletedError) {
return c.SendStatus(fiber.StatusGone)
}
if err != nil {
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("charset", "utf-8")
@ -53,7 +74,7 @@ func AddFeedRoutes(app model.AppType) {
})
// 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")
results, err := ical.FeedRecord(app, token)