Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion db/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,21 @@ import (
_ "github.com/go-sql-driver/mysql"
)

var conn *sql.DB
var conn *sql.DB = nil
var protocol_type = ""

func GetProtocol() string {
return protocol_type
}

func Init(username string, password string, protocol string, address string, database string) error {
var err error

protocol_type = protocol
if protocol == "none" {
return nil
}

conn, err = sql.Open("mysql", fmt.Sprintf("%s:%s@%s(%s)/%s?parseTime=true", username, password, protocol, address, database))
if err != nil {
return err
Expand Down
16 changes: 16 additions & 0 deletions db/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ type Item struct {

var ErrInvalidID = errors.New("download id not found")

var TEST_ITEM = Item{
Name: "missingname",
Filename: "missingname.zip",
Size: 1337 * (1024 * 1024),
}

func (i Item) PrettySize() string {
return fmt.Sprintf("%.02f MB", float64(i.Size)/1024/1024)
}
Expand All @@ -53,6 +59,13 @@ func (i Item) PrettyName() string {
}

func GetItemList(ctx context.Context, tag string, query string) ([]Item, error) {
if (GetProtocol() == "test") { return []Item{
TEST_ITEM,
TEST_ITEM,
TEST_ITEM,
TEST_ITEM,
}, nil }

q := `SELECT p.id, p.name, p.filename, p.description, p.size, p.uploader, COALESCE(p.uploaded, f.modified), s.downloads, s.views
FROM packages p
JOIN stats s ON p.id = s.pid
Expand All @@ -63,6 +76,7 @@ func GetItemList(ctx context.Context, tag string, query string) ([]Item, error)
AND modified < TIMESTAMP'2015-01-01 00:00:00'
GROUP BY pid) f
ON p.id = f.pid`

var args []any

if tag != "" {
Expand Down Expand Up @@ -116,6 +130,8 @@ func GetItemList(ctx context.Context, tag string, query string) ([]Item, error)
}

func GetItem(ctx context.Context, id int) (Item, error) {
if (GetProtocol() == "test") { return TEST_ITEM, nil }

item := Item{ID: id, Images: make(map[int]string)}

q := `SELECT p.name, p.filename, p.description, p.size, p.uploader, COALESCE(p.uploaded, f.modified)
Expand Down
10 changes: 10 additions & 0 deletions db/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ package db
import "context"

func GetPopularTags(ctx context.Context) ([]string, error) {
if (GetProtocol() == "test") { return []string{
"garrysmod",
"1337",
"hax",
"lol",
"lua",
"is",
"awesome",
}, nil }

var tags []string
rows, err := conn.QueryContext(ctx, "SELECT tag FROM tags GROUP BY tag ORDER BY COUNT(*) DESC, tag ASC LIMIT 100")
if err != nil {
Expand Down