diff --git a/account/account.go b/account/account.go index 12ee393..22df65d 100644 --- a/account/account.go +++ b/account/account.go @@ -4,6 +4,7 @@ import ( "git.lumeweb.com/LumeWeb/portal/db/models" "git.lumeweb.com/LumeWeb/portal/interfaces" "golang.org/x/crypto/bcrypt" + "gorm.io/gorm" ) var ( @@ -123,3 +124,36 @@ func (s AccountServiceImpl) AccountPins(id uint64, createdAfter uint64) ([]model return pins, nil } + +func (s AccountServiceImpl) DeletePinByHash(hash string, accountID uint) error { + // Define a struct for the query condition + uploadQuery := models.Upload{Hash: hash} + + // Retrieve the upload ID for the given hash + var uploadID uint + result := s.portal.Database(). + Model(&models.Upload{}). + Where(&uploadQuery). + Select("id"). + First(&uploadID) + + if result.Error != nil { + if result.Error == gorm.ErrRecordNotFound { + // No record found, nothing to delete + return nil + } + return result.Error + } + + // Delete pins with the retrieved upload ID and matching account ID + pinQuery := models.Pin{UploadID: uploadID, UserID: accountID} + result = s.portal.Database(). + Where(&pinQuery). + Delete(&models.Pin{}) + + if result.Error != nil { + return result.Error + } + + return nil +} diff --git a/interfaces/account.go b/interfaces/account.go index 7e9ff5b..2d18d16 100644 --- a/interfaces/account.go +++ b/interfaces/account.go @@ -11,4 +11,5 @@ type AccountService interface { LoginPassword(email string, password string) (string, error) LoginPubkey(pubkey string) (string, error) AccountPins(id uint64, createdAfter uint64) ([]models.Pin, error) + DeletePinByHash(hash string, accountID uint) error }