2023-10-25 04:14:32 +00:00
|
|
|
// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package db
|
|
|
|
|
2023-10-29 14:41:00 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
2024-02-23 23:06:16 +00:00
|
|
|
"sync"
|
2023-10-29 14:41:00 +00:00
|
|
|
)
|
2023-10-25 04:14:32 +00:00
|
|
|
|
|
|
|
// UpsertRelease adds or updates a release for a project with a given URL in the
|
|
|
|
// database
|
2024-02-23 23:06:16 +00:00
|
|
|
func UpsertRelease(db *sql.DB, mu *sync.Mutex, id, projectID, url, tag, content, date string) error {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
_, err := db.Exec(`INSERT INTO releases (id, project_id, url, tag, content, date)
|
2023-10-29 14:41:00 +00:00
|
|
|
VALUES (?, ?, ?, ?, ?, ?)
|
|
|
|
ON CONFLICT(id) DO
|
2023-10-25 04:14:32 +00:00
|
|
|
UPDATE SET
|
2024-02-23 23:06:16 +00:00
|
|
|
url = excluded.url,
|
2023-10-25 04:14:32 +00:00
|
|
|
content = excluded.content,
|
|
|
|
tag = excluded.tag,
|
|
|
|
content = excluded.content,
|
2024-02-23 23:06:16 +00:00
|
|
|
date = excluded.date;`, id, projectID, url, tag, content, date)
|
2023-10-25 04:14:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-23 23:06:16 +00:00
|
|
|
// GetReleases returns all releases for a project with a given id from the database
|
|
|
|
func GetReleases(db *sql.DB, projectID string) ([]map[string]string, error) {
|
|
|
|
rows, err := db.Query(`SELECT id, url, tag, content, date FROM releases WHERE project_id = ?`, projectID)
|
2023-10-25 04:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
releases := make([]map[string]string, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
var (
|
2024-02-23 23:06:16 +00:00
|
|
|
id string
|
|
|
|
url string
|
|
|
|
tag string
|
|
|
|
content string
|
|
|
|
date string
|
2023-10-25 04:14:32 +00:00
|
|
|
)
|
2024-02-23 23:06:16 +00:00
|
|
|
err := rows.Scan(&id, &url, &tag, &content, &date)
|
2023-10-25 04:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
releases = append(releases, map[string]string{
|
2024-02-23 23:06:16 +00:00
|
|
|
"id": id,
|
|
|
|
"url": url,
|
|
|
|
"tag": tag,
|
|
|
|
"content": content,
|
|
|
|
"date": date,
|
2023-10-25 04:14:32 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return releases, nil
|
|
|
|
}
|