Skip to content
This repository has been archived by the owner on Oct 5, 2023. It is now read-only.

Commit

Permalink
Refactor (#18)
Browse files Browse the repository at this point in the history
* Refactor query

* Lint yaml
  • Loading branch information
mgdigital authored Jun 25, 2023
1 parent 54c5d80 commit b4afe7e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ghcr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ name: Deploy Image to GHCR
on:
push:
tags:
- 'v*.*.*'
- "v*.*.*"

jobs:
build-push:
runs-on: ubuntu-latest
steps:
- name: 'Checkout GitHub Action'
- name: "Checkout GitHub Action"
uses: actions/checkout@main
- name: 'Login to GitHub Container Registry'
- name: "Login to GitHub Container Registry"
uses: docker/login-action@v1
with:
registry: ghcr.io
username: mgdigital
password: ${{secrets.GHCR_TOKEN}}
- name: 'Build and push'
- name: "Build and push"
run: |
docker build . --tag ghcr.io/mgdigital/rarbg-selfhosted:${GITHUB_REF#refs/tags/} --tag ghcr.io/mgdigital/rarbg-selfhosted:latest
docker push --all-tags ghcr.io/mgdigital/rarbg-selfhosted
41 changes: 23 additions & 18 deletions sqlitedb/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ type SearchQuery struct {
}

func Query(db *sql.DB, query *SearchQuery) ([]Record, error) {
sqlQuery, err := createQuery(query)
log.WithField("query", sqlQuery).Debug("SQL query")
if err != nil {
return nil, err
}
rows, err := db.Query(sqlQuery)
if err != nil {
return nil, err
}
var records []Record
for rows.Next() {
record := Record{}
err = rows.Scan(&record.Id, &record.Hash, &record.Title, &record.Dt, &record.Cat, &record.Size, &record.ExtId, &record.Imdb)
if err != nil {
return nil, err
}
records = append(records, record)
}
return records, nil
}

func createQuery(query *SearchQuery) (string, error) {
dialect := goqu.Dialect("sqlite3")
var expressions []goqu.Expression
if len(query.Query) > 0 {
Expand All @@ -56,22 +78,5 @@ func Query(db *sql.DB, query *SearchQuery) ([]Record, error) {
}
ds := dialect.From("items").Where(expressions...).Order(goqu.C("dt").Desc()).Limit(query.Limit).Offset(query.Offset)
sqlQuery, _, err := ds.ToSQL()
log.WithField("query", sqlQuery).Debug("SQL query")
if err != nil {
return nil, err
}
rows, err := db.Query(sqlQuery)
if err != nil {
return nil, err
}
var records []Record
for rows.Next() {
record := Record{}
err = rows.Scan(&record.Id, &record.Hash, &record.Title, &record.Dt, &record.Cat, &record.Size, &record.ExtId, &record.Imdb)
if err != nil {
return nil, err
}
records = append(records, record)
}
return records, nil
return sqlQuery, err
}

0 comments on commit b4afe7e

Please sign in to comment.