first commit
This commit is contained in:
54
app/account/services/social_account_service.go
Normal file
54
app/account/services/social_account_service.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gobeyhan/database"
|
||||
"gobeyhan/database/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type SocialAccountService struct{}
|
||||
|
||||
func NewSocialAccountService() *SocialAccountService {
|
||||
return &SocialAccountService{}
|
||||
}
|
||||
|
||||
// GetSocialAccountsByUser retrieves all social accounts for a user
|
||||
func (s *SocialAccountService) GetSocialAccountsByUser(userID uint64) ([]models.SocialAccount, error) {
|
||||
var accounts []models.SocialAccount
|
||||
err := database.DB.Where("user_id = ?", userID).Find(&accounts).Error
|
||||
return accounts, err
|
||||
}
|
||||
|
||||
// GetSocialAccountByID retrieves a social account by ID
|
||||
func (s *SocialAccountService) GetSocialAccountByID(id uint64) (*models.SocialAccount, error) {
|
||||
var account models.SocialAccount
|
||||
err := database.DB.First(&account, id).Error
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &account, nil
|
||||
}
|
||||
|
||||
// CreateSocialAccount creates a new social account
|
||||
func (s *SocialAccountService) CreateSocialAccount(account *models.SocialAccount) error {
|
||||
return database.DB.Create(account).Error
|
||||
}
|
||||
|
||||
// DeleteSocialAccount deletes a social account by ID
|
||||
func (s *SocialAccountService) DeleteSocialAccount(id uint64) error {
|
||||
result := database.DB.Delete(&models.SocialAccount{}, id)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user