Skip to content

Commit

Permalink
Merge pull request #250 from systemli/Delete-Attachments-when-deletin…
Browse files Browse the repository at this point in the history
…g-Messages-or-Tickers

🗃️ Delete Attachments when deleting Messages or Tickers
  • Loading branch information
0x46616c6b authored Oct 21, 2023
2 parents ebfcee9 + a48473c commit 1c42a42
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 5 deletions.
4 changes: 4 additions & 0 deletions internal/api/tickers.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ func (h *handler) DeleteTicker(c *gin.Context) {
if err != nil {
log.WithError(err).Error("failed to delete message for ticker")
}
err = h.storage.DeleteUploadsByTicker(ticker)
if err != nil {
log.WithError(err).Error("failed to delete uploads for ticker")
}
err = h.storage.DeleteTicker(ticker)
if err != nil {
c.JSON(http.StatusNotFound, response.ErrorResponse(response.CodeNotFound, response.StorageError))
Expand Down
2 changes: 2 additions & 0 deletions internal/api/tickers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ func TestDeleteTickerStorageError(t *testing.T) {
c.Set("ticker", storage.Ticker{})
s := &storage.MockStorage{}
s.On("DeleteMessages", mock.Anything).Return(errors.New("storage error"))
s.On("DeleteUploadsByTicker", mock.Anything).Return(errors.New("storage error"))
s.On("DeleteTicker", mock.Anything).Return(errors.New("storage error"))
h := handler{
storage: s,
Expand All @@ -639,6 +640,7 @@ func TestDeleteTicker(t *testing.T) {
c.Set("ticker", storage.Ticker{})
s := &storage.MockStorage{}
s.On("DeleteMessages", mock.Anything).Return(nil)
s.On("DeleteUploadsByTicker", mock.Anything).Return(nil)
s.On("DeleteTicker", mock.Anything).Return(nil)
h := handler{
storage: s,
Expand Down
14 changes: 14 additions & 0 deletions internal/storage/mock_Storage.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 26 additions & 3 deletions internal/storage/sql_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,36 @@ func (s *SqlStorage) SaveMessage(message *Message) error {
}

func (s *SqlStorage) DeleteMessage(message Message) error {
return s.db.Delete(&message).Error
var err error
err = s.db.Delete(&message).Error
if err != nil {
return err
}

if len(message.Attachments) > 0 {
err = s.DeleteAttachmentsByMessage(message)
}

return err
}

func (s *SqlStorage) DeleteMessages(ticker Ticker) error {
err := s.db.Where("ticker_id = ?", ticker.ID).Delete(&Message{}).Error
var msgIds []int
err := s.db.Model(&Message{}).Where("ticker_id = ?", ticker.ID).Pluck("id", &msgIds).Error
if err != nil {
return err
}

return err
err = s.db.Where("message_id IN ?", msgIds).Delete(&Attachment{}).Error
if err != nil {
return err
}

return s.db.Where("ticker_id = ?", ticker.ID).Delete(&Message{}).Error
}

func (s *SqlStorage) DeleteAttachmentsByMessage(message Message) error {
return s.db.Where("message_id = ?", message.ID).Delete(&Attachment{}).Error
}

func (s *SqlStorage) GetInactiveSettings() InactiveSettings {
Expand Down
33 changes: 33 additions & 0 deletions internal/storage/sql_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,32 @@ var _ = Describe("SqlStorage", func() {
Expect(err).ToNot(HaveOccurred())
Expect(count).To(Equal(int64(0)))
})

It("deletes the message with attachments", func() {
message := NewMessage()
message.Attachments = []Attachment{
{ID: 1, MessageID: 1, UUID: "uuid", ContentType: "image/jpg", Extension: "jpg"},
}

err = store.SaveMessage(&message)
Expect(err).ToNot(HaveOccurred())

var count int64
err = db.Model(&Message{}).Count(&count).Error
Expect(err).ToNot(HaveOccurred())
Expect(count).To(Equal(int64(1)))

err = store.DeleteMessage(message)
Expect(err).ToNot(HaveOccurred())

err = db.Model(&Message{}).Count(&count).Error
Expect(err).ToNot(HaveOccurred())
Expect(count).To(Equal(int64(0)))

err = db.Model(&Attachment{}).Count(&count).Error
Expect(err).ToNot(HaveOccurred())
Expect(count).To(Equal(int64(0)))
})
})

Describe("DeleteMessages", func() {
Expand All @@ -718,6 +744,9 @@ var _ = Describe("SqlStorage", func() {

message := NewMessage()
message.TickerID = ticker.ID
message.Attachments = []Attachment{
{ID: 1, MessageID: 1, UUID: "uuid", ContentType: "image/jpg", Extension: "jpg"},
}
err = store.SaveMessage(&message)
Expect(err).ToNot(HaveOccurred())

Expand All @@ -732,6 +761,10 @@ var _ = Describe("SqlStorage", func() {
err = db.Model(&Message{}).Count(&count).Error
Expect(err).ToNot(HaveOccurred())
Expect(count).To(Equal(int64(0)))

err = db.Model(&Attachment{}).Count(&count).Error
Expect(err).ToNot(HaveOccurred())
Expect(count).To(Equal(int64(0)))
})
})

Expand Down
5 changes: 3 additions & 2 deletions internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type Storage interface {
SaveTicker(ticker *Ticker) error
DeleteTicker(ticker Ticker) error
SaveUpload(upload *Upload) error
FindUploadByUUID(uuid string) (Upload, error)
FindUploadsByIDs(ids []int) ([]Upload, error)
DeleteUpload(upload Upload) error
DeleteUploads(uploads []Upload)
DeleteUploadsByTicker(ticker Ticker) error
Expand All @@ -36,11 +38,10 @@ type Storage interface {
SaveMessage(message *Message) error
DeleteMessage(message Message) error
DeleteMessages(ticker Ticker) error
DeleteAttachmentsByMessage(message Message) error
GetInactiveSettings() InactiveSettings
GetRefreshIntervalSettings() RefreshIntervalSettings
SaveInactiveSettings(inactiveSettings InactiveSettings) error
SaveRefreshIntervalSettings(refreshInterval RefreshIntervalSettings) error
FindUploadByUUID(uuid string) (Upload, error)
FindUploadsByIDs(ids []int) ([]Upload, error)
UploadPath() string
}

0 comments on commit 1c42a42

Please sign in to comment.