1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
2//
3// SPDX-License-Identifier: Apache-2.0
4
5package db
6
7import (
8 "database/sql"
9 "sync"
10)
11
12// UpsertRelease adds or updates a release for a project with a given URL in the
13// database
14func UpsertRelease(db *sql.DB, mu *sync.Mutex, id, projectID, url, tag, content, date string) error {
15 mu.Lock()
16 defer mu.Unlock()
17 _, err := db.Exec(`INSERT INTO releases (id, project_id, url, tag, content, date)
18 VALUES (?, ?, ?, ?, ?, ?)
19 ON CONFLICT(id) DO
20 UPDATE SET
21 url = excluded.url,
22 content = excluded.content,
23 tag = excluded.tag,
24 content = excluded.content,
25 date = excluded.date;`, id, projectID, url, tag, content, date)
26 return err
27}
28
29// GetReleases returns all releases for a project with a given id from the database
30func GetReleases(db *sql.DB, projectID string) ([]map[string]string, error) {
31 rows, err := db.Query(`SELECT id, url, tag, content, date FROM releases WHERE project_id = ?`, projectID)
32 if err != nil {
33 return nil, err
34 }
35 defer rows.Close()
36
37 releases := make([]map[string]string, 0)
38 for rows.Next() {
39 var (
40 id string
41 url string
42 tag string
43 content string
44 date string
45 )
46 err := rows.Scan(&id, &url, &tag, &content, &date)
47 if err != nil {
48 return nil, err
49 }
50 releases = append(releases, map[string]string{
51 "id": id,
52 "url": url,
53 "tag": tag,
54 "content": content,
55 "date": date,
56 })
57 }
58 return releases, nil
59}