You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.5 KiB
Go
89 lines
2.5 KiB
Go
package dbi
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
)
|
|
|
|
type Scholarship struct {
|
|
UID string `json:"uid"`
|
|
Citation string `json:"citation"`
|
|
Hyperlink string `json:"hyperlink"`
|
|
DocumentLink string `json:"document_link"`
|
|
Status string `json:"status"`
|
|
Created string `json:"created"`
|
|
Modified string `json:"modified"`
|
|
Username string `json:"username"`
|
|
}
|
|
|
|
func CreateScholarship(conn *sql.Conn, s *Scholarship) error {
|
|
if s.UID == "" {
|
|
s.UID = GenUUID()
|
|
}
|
|
now := GetNow()
|
|
s.Created = now
|
|
s.Modified = now
|
|
_, err := conn.ExecContext(context.Background(),
|
|
`INSERT INTO scholarship (UID, Citation, Hyperlink, DocumentLink, Status, Created, Modified, Username)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
s.UID, s.Citation, s.Hyperlink, s.DocumentLink, s.Status, s.Created, s.Modified, s.Username,
|
|
)
|
|
return err
|
|
}
|
|
|
|
func GetScholarship(conn *sql.Conn, uid string) (Scholarship, error) {
|
|
row := conn.QueryRowContext(context.Background(),
|
|
`SELECT UID, Citation, Hyperlink, DocumentLink, Status, Created, Modified, Username
|
|
FROM scholarship WHERE UID = ?`,
|
|
uid,
|
|
)
|
|
var s Scholarship
|
|
err := row.Scan(&s.UID, &s.Citation, &s.Hyperlink, &s.DocumentLink, &s.Status, &s.Created, &s.Modified, &s.Username)
|
|
if err == sql.ErrNoRows {
|
|
return Scholarship{}, fmt.Errorf("scholarship %q not found", uid)
|
|
}
|
|
return s, err
|
|
}
|
|
|
|
func UpdateScholarship(conn *sql.Conn, s *Scholarship) error {
|
|
s.Modified = GetNow()
|
|
_, err := conn.ExecContext(context.Background(),
|
|
`UPDATE scholarship SET Citation = ?, Hyperlink = ?, DocumentLink = ?, Status = ?, Modified = ?, Username = ?
|
|
WHERE UID = ?`,
|
|
s.Citation, s.Hyperlink, s.DocumentLink, s.Status, s.Modified, s.Username, s.UID,
|
|
)
|
|
return err
|
|
}
|
|
|
|
func DeleteScholarship(conn *sql.Conn, uid string) error {
|
|
_, err := conn.ExecContext(context.Background(),
|
|
`DELETE FROM scholarship WHERE UID = ?`,
|
|
uid,
|
|
)
|
|
return err
|
|
}
|
|
|
|
func GetScholarshipsForUsername(conn *sql.Conn, username string) ([]Scholarship, error) {
|
|
rows, err := conn.QueryContext(context.Background(),
|
|
`SELECT UID, Citation, Hyperlink, DocumentLink, Status, Created, Modified, Username
|
|
FROM scholarship WHERE Username = ?`,
|
|
username,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var results []Scholarship
|
|
for rows.Next() {
|
|
var s Scholarship
|
|
if err := rows.Scan(&s.UID, &s.Citation, &s.Hyperlink, &s.DocumentLink, &s.Status, &s.Created, &s.Modified, &s.Username); err != nil {
|
|
return nil, err
|
|
}
|
|
results = append(results, s)
|
|
}
|
|
return results, rows.Err()
|
|
}
|