core/internal/database/ops_application.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
package database import ( "context" "errors" "fmt" "time" "github.com/wbrijesh/identity/buid" "github.com/wbrijesh/identity/internal/auth" "github.com/wbrijesh/identity/internal/models" "gorm.io/gorm" ) // Assuming this is defined somewhere in your package var secretKey = []byte("your-secret-key-here") func (s *service) CreateApplication(ctx context.Context, app *models.Application) (*models.Application, error) { tx := s.db.WithContext(ctx).Begin() if tx.Error != nil { return nil, fmt.Errorf("failed to begin transaction: %w", tx.Error) } defer func() { if r := recover(); r != nil { tx.Rollback() } }() app.ID = buid.GenerateBUID() // Set CreatedAt and UpdatedAt now := time.Now() app.CreatedAt = now app.UpdatedAt = now if err := tx.Create(app).Error; err != nil { tx.Rollback() return nil, fmt.Errorf("failed to create application: %w", err) } if err := tx.Commit().Error; err != nil { return nil, fmt.Errorf("failed to commit transaction: %w", err) } return app, nil } func (s *service) GetApplicationByID(ctx context.Context, id string) (*models.Application, error) { var app models.Application if err := s.db.WithContext(ctx).First(&app, "id = ?", id).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil, fmt.Errorf("application with ID %s not found", id) } return nil, fmt.Errorf("error fetching application: %w", err) } return &app, nil } func (s *service) GetApplicationByAPIKey(ctx context.Context, apiKey string) (*models.Application, error) { var app models.Application if err := s.db.WithContext(ctx).Where("api_key = ?", apiKey).First(&app).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil, fmt.Errorf("application with API key %s not found", apiKey) } return nil, fmt.Errorf("error fetching application: %w", err) } return &app, nil } func (s *service) UpdateApplication(ctx context.Context, app *models.Application) (*models.Application, error) { tx := s.db.WithContext(ctx).Begin() if tx.Error != nil { return nil, fmt.Errorf("failed to begin transaction: %w", tx.Error) } defer func() { if r := recover(); r != nil { tx.Rollback() } }() var existingApp models.Application if err := tx.First(&existingApp, "id = ?", app.ID).Error; err != nil { tx.Rollback() if errors.Is(err, gorm.ErrRecordNotFound) { return nil, fmt.Errorf("application with ID %s not found", app.ID) } return nil, fmt.Errorf("error fetching application: %w", err) } app.UpdatedAt = time.Now() if err := tx.Model(&existingApp).Updates(app).Error; err != nil { tx.Rollback() return nil, fmt.Errorf("failed to update application: %w", err) } if err := tx.Commit().Error; err != nil { return nil, fmt.Errorf("failed to commit transaction: %w", err) } return app, nil } func (s *service) DeleteApplication(ctx context.Context, id string) error { tx := s.db.WithContext(ctx).Begin() if tx.Error != nil { return fmt.Errorf("failed to begin transaction: %w", tx.Error) } defer func() { if r := recover(); r != nil { tx.Rollback() } }() if err := tx.Delete(&models.Application{}, "id = ?", id).Error; err != nil { tx.Rollback() return fmt.Errorf("failed to delete application: %w", err) } if err := tx.Commit().Error; err != nil { return fmt.Errorf("failed to commit transaction: %w", err) } return nil } func (s *service) ListApplications(ctx context.Context, offset, limit int, adminID string) ([]*models.Application, int64, error) { var apps []*models.Application var total int64 query := s.db.WithContext(ctx).Model(&models.Application{}).Where("admin_id = ?", adminID) if err := query.Count(&total).Error; err != nil { return nil, 0, fmt.Errorf("error counting applications: %w", err) } if limit == 0 { offset = 0 limit = 20 } if err := query.Offset(offset).Limit(limit).Find(&apps).Error; err != nil { return nil, 0, fmt.Errorf("error fetching applications: %w", err) } return apps, total, nil } func (s *service) GenerateRefreshToken(ctx context.Context, id string) (string, error) { tx := s.db.WithContext(ctx).Begin() if tx.Error != nil { return "", fmt.Errorf("failed to begin transaction: %w", tx.Error) } defer func() { if r := recover(); r != nil { tx.Rollback() } }() // Fetch the application var app models.Application if err := tx.First(&app, "id = ?", id).Error; err != nil { tx.Rollback() return "", fmt.Errorf("failed to find application: %w", err) } // if app.RefreshToken != "" { // tx.Rollback() // return "", fmt.Errorf("application already has a refresh token") // } // Generate a new refresh token refreshToken, err := auth.GenerateRefreshToken(app.ID) if err != nil { tx.Rollback() return "", fmt.Errorf("failed to generate refresh token: %w", err) } // Update the application with the new refresh token app.RefreshToken = refreshToken app.UpdatedAt = time.Now() if err := tx.Save(&app).Error; err != nil { tx.Rollback() return "", fmt.Errorf("failed to update application with refresh token: %w", err) } if err := tx.Commit().Error; err != nil { return "", fmt.Errorf("failed to commit transaction: %w", err) } return refreshToken, nil } func (s *service) DeleteRefreshToken(ctx context.Context, id string) error { tx := s.db.WithContext(ctx).Begin() if tx.Error != nil { return fmt.Errorf("failed to begin transaction: %w", tx.Error) } defer func() { if r := recover(); r != nil { tx.Rollback() } }() // Fetch the application var app models.Application if err := tx.First(&app, "id = ?", id).Error; err != nil { tx.Rollback() return fmt.Errorf("failed to find application: %w", err) } // Clear the refresh token app.RefreshToken = "" app.UpdatedAt = time.Now() if err := tx.Save(&app).Error; err != nil { tx.Rollback() return fmt.Errorf("failed to update application and remove refresh token: %w", err) } if err := tx.Commit().Error; err != nil { return fmt.Errorf("failed to commit transaction: %w", err) } return nil } |