From 3df923318ec83b942723c34b2d209b567f090913 Mon Sep 17 00:00:00 2001 From: Liam Burnand Date: Sun, 18 Feb 2024 16:42:04 +0000 Subject: [PATCH] It would help if I could actually delete the stored stream... --- server/store/store.go | 32 ++++++++++++++++++++++++++++++++ server/views/delete.go | 2 +- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/server/store/store.go b/server/store/store.go index 715a672..ab1746f 100644 --- a/server/store/store.go +++ b/server/store/store.go @@ -134,6 +134,38 @@ func (store *Store) AddStored(stored *storage.Stored) (*storage.Stored, error) { return stored, nil } +func (store *Store) DeleteStored(unique string) error { + streamer, err := store.backend.Read() + if err != nil { + return err + } + + s := streamer.Stored + found := false + var index int + var v *storage.Stored + for index, v = range s { + if v.Stream == unique { + found = true + break + } + } + + if found { + copy(s[index:], s[index+1:]) // Shift a[i+1:] left one index + s[len(s)-1] = nil // Erase last element (write zero value) + streamer.Stored = s[:len(s)-1] // Truncate slice + } else { + return fmt.Errorf("stream not found for DeleteStored") + } + + if err = store.backend.Write(streamer); err != nil { + return err + } + + return nil +} + func (store *Store) Get() (*storage.Streamer, error) { return store.backend.Read() } diff --git a/server/views/delete.go b/server/views/delete.go index 7b508dc..b76e95f 100644 --- a/server/views/delete.go +++ b/server/views/delete.go @@ -39,7 +39,7 @@ func (v *Views) DeleteFunc(c echo.Context) error { return c.JSON(http.StatusOK, response) } - err = v.store.DeleteStream(unique) + err = v.store.DeleteStored(unique) if err != nil { log.Printf("failed to delete stored: %+v, unique: %s", err, unique) response.Error = fmt.Sprintf("failed to delete stored: %+v, unique: %s", err, unique)