diff options
| author | simponic <simponic@hatecomputers.club> | 2024-03-28 12:57:35 -0400 |
|---|---|---|
| committer | simponic <simponic@hatecomputers.club> | 2024-03-28 12:57:35 -0400 |
| commit | b2fc689bdcff28bf75c0128db19ba4730d726b4f (patch) | |
| tree | 37c16d95183242516ba667aa5f441539d152c279 /database | |
| parent | 75ba836d6072235fc7a71659f8630ab3c1b210ad (diff) | |
| download | hatecomputers.club-b2fc689bdcff28bf75c0128db19ba4730d726b4f.tar.gz hatecomputers.club-b2fc689bdcff28bf75c0128db19ba4730d726b4f.zip | |
dns api (#1)
Co-authored-by: Elizabeth Hunt <elizabeth.hunt@simponic.xyz>
Reviewed-on: https://git.hatecomputers.club/hatecomputers/hatecomputers.club/pulls/1
Diffstat (limited to 'database')
| -rw-r--r-- | database/dns.go | 48 | ||||
| -rw-r--r-- | database/users.go | 63 |
2 files changed, 104 insertions, 7 deletions
diff --git a/database/dns.go b/database/dns.go index 17487b7..bb5c1ef 100644 --- a/database/dns.go +++ b/database/dns.go @@ -8,13 +8,13 @@ import ( ) type DNSRecord struct { - ID string - UserID string - Name string - Type string - Content string - TTL int - CreatedAt time.Time + ID string `json:"id"` + UserID string `json:"user_id"` + Name string `json:"name"` + Type string `json:"type"` + Content string `json:"content"` + TTL int `json:"ttl"` + CreatedAt time.Time `json:"created_at"` } func GetUserDNSRecords(db *sql.DB, userID string) ([]DNSRecord, error) { @@ -38,3 +38,37 @@ func GetUserDNSRecords(db *sql.DB, userID string) ([]DNSRecord, error) { return records, nil } + +func SaveDNSRecord(db *sql.DB, record *DNSRecord) (*DNSRecord, error) { + log.Println("saving dns record", record) + + record.CreatedAt = time.Now() + _, err := db.Exec("INSERT OR REPLACE INTO dns_records (id, user_id, name, type, content, ttl, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)", record.ID, record.UserID, record.Name, record.Type, record.Content, record.TTL, record.CreatedAt) + + if err != nil { + return nil, err + } + return record, nil +} + +func GetDNSRecord(db *sql.DB, recordID string) (*DNSRecord, error) { + log.Println("getting dns record", recordID) + + row := db.QueryRow("SELECT * FROM dns_records WHERE id = ?", recordID) + var record DNSRecord + err := row.Scan(&record.ID, &record.UserID, &record.Name, &record.Type, &record.Content, &record.TTL, &record.CreatedAt) + if err != nil { + return nil, err + } + return &record, nil +} + +func DeleteDNSRecord(db *sql.DB, recordID string) error { + log.Println("deleting dns record", recordID) + + _, err := db.Exec("DELETE FROM dns_records WHERE id = ?", recordID) + if err != nil { + return err + } + return nil +} diff --git a/database/users.go b/database/users.go index d2b4f20..f9e4436 100644 --- a/database/users.go +++ b/database/users.go @@ -12,6 +12,12 @@ const ( ExpiryDuration = time.Hour * 24 ) +type UserApiKey struct { + Key string `json:"key"` + UserID string `json:"user_id"` + CreatedAt time.Time `json:"created_at"` +} + type User struct { ID string `json:"sub"` Mail string `json:"email"` @@ -119,3 +125,60 @@ func DeleteExpiredSessions(dbConn *sql.DB) error { } return nil } + +func ListUserAPIKeys(dbConn *sql.DB, userId string) ([]*UserApiKey, error) { + rows, err := dbConn.Query(`SELECT key, user_id, created_at FROM api_keys WHERE user_id = ?;`, userId) + if err != nil { + log.Println(err) + return nil, err + } + defer rows.Close() + + var apiKeys []*UserApiKey + for rows.Next() { + var apiKey UserApiKey + err := rows.Scan(&apiKey.Key, &apiKey.UserID, &apiKey.CreatedAt) + if err != nil { + log.Println(err) + return nil, err + } + + apiKeys = append(apiKeys, &apiKey) + } + + return apiKeys, nil +} + +func SaveAPIKey(dbConn *sql.DB, apiKey *UserApiKey) (*UserApiKey, error) { + _, err := dbConn.Exec(`INSERT OR REPLACE INTO api_keys (key, user_id) VALUES (?, ?);`, apiKey.Key, apiKey.UserID) + if err != nil { + log.Println(err) + return nil, err + } + + apiKey.CreatedAt = time.Now() + return apiKey, nil +} + +func GetAPIKey(dbConn *sql.DB, key string) (*UserApiKey, error) { + row := dbConn.QueryRow(`SELECT key, user_id, created_at FROM api_keys WHERE key = ?;`, key) + + var apiKey UserApiKey + err := row.Scan(&apiKey.Key, &apiKey.UserID, &apiKey.CreatedAt) + if err != nil { + log.Println(err) + return nil, err + } + + return &apiKey, nil +} + +func DeleteAPIKey(dbConn *sql.DB, key string) error { + _, err := dbConn.Exec(`DELETE FROM api_keys WHERE key = ?;`, key) + if err != nil { + log.Println(err) + return err + } + + return nil +} |
