refactor: have PinByID check for a pin before adding one

This commit is contained in:
Derrick Hammer 2024-01-17 17:18:58 -05:00
parent 7fde67aea5
commit 310c23b95e
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 11 additions and 1 deletions

View File

@ -186,9 +186,19 @@ func (s AccountServiceImpl) PinByHash(hash string, accountID uint) error {
}
func (s AccountServiceImpl) PinByID(uploadId uint, accountID uint) error {
result := s.portal.Database().Model(&models.Pin{}).Where(&models.Pin{UploadID: uploadId, UserID: accountID}).First(&models.Pin{})
if result.Error != nil && result.Error != gorm.ErrRecordNotFound {
return result.Error
}
if result.RowsAffected > 0 {
return nil
}
// Create a pin with the retrieved upload ID and matching account ID
pinQuery := models.Pin{UploadID: uploadId, UserID: accountID}
result := s.portal.Database().Create(&pinQuery)
result = s.portal.Database().Create(&pinQuery)
if result.Error != nil {
return result.Error