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 |
package database import ( "fmt" "time" "github.com/gocql/gocql" ) func (s *service) CreateApplication(userID gocql.UUID, name, description, keyHash string) (*Application, error) { app := &Application{ ID: gocql.TimeUUID(), UserID: userID, Name: name, Description: description, KeyHash: keyHash, CreatedAt: time.Now(), UpdatedAt: time.Now(), } if err := s.session.Query(` INSERT INTO applications (id, user_id, name, description, key_hash, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)`, app.ID, app.UserID, app.Name, app.Description, app.KeyHash, app.CreatedAt, app.UpdatedAt, ).Exec(); err != nil { return nil, fmt.Errorf("error creating application: %w", err) } return app, nil } func (s *service) GetApplication(id gocql.UUID) (*Application, error) { var app Application if err := s.session.Query(` SELECT id, user_id, name, description, key_hash, created_at, updated_at FROM applications WHERE id = ?`, id, ).Scan( &app.ID, &app.UserID, &app.Name, &app.Description, &app.KeyHash, &app.CreatedAt, &app.UpdatedAt, ); err != nil { return nil, fmt.Errorf("application not found: %w", err) } return &app, nil } func (s *service) ListApplications(userID gocql.UUID) ([]Application, error) { iter := s.session.Query(` SELECT id, user_id, name, description, key_hash, created_at, updated_at FROM applications WHERE user_id = ? ALLOW FILTERING`, userID, ).Iter() var apps []Application var app Application for iter.Scan( &app.ID, &app.UserID, &app.Name, &app.Description, &app.KeyHash, &app.CreatedAt, &app.UpdatedAt, ) { apps = append(apps, app) } if err := iter.Close(); err != nil { return nil, fmt.Errorf("error listing applications: %w", err) } return apps, nil } func (s *service) UpdateApplication(id gocql.UUID, name, description string) (*Application, error) { now := time.Now() if err := s.session.Query(` UPDATE applications SET name = ?, description = ?, updated_at = ? WHERE id = ?`, name, description, now, id, ).Exec(); err != nil { return nil, fmt.Errorf("error updating application: %w", err) } return s.GetApplication(id) } func (s *service) DeleteApplication(id gocql.UUID) error { // First, delete all logs associated with the application if err := s.session.Query(` DELETE FROM logs WHERE application_id = ?`, id, ).Exec(); err != nil { return fmt.Errorf("error deleting application logs: %w", err) } // Then delete the application itself if err := s.session.Query(` DELETE FROM applications WHERE id = ?`, id, ).Exec(); err != nil { return fmt.Errorf("error deleting application: %w", err) } return nil } func (s *service) RegenerateApplicationKey(appID gocql.UUID, newKeyHash string) error { now := time.Now() if err := s.session.Query(` UPDATE applications SET key_hash = ?, updated_at = ? WHERE id = ?`, newKeyHash, now, appID, ).Exec(); err != nil { return fmt.Errorf("error regenerating application key: %w", err) } return nil } |