summaryrefslogtreecommitdiffstats
path: root/database/postgres/report.go
blob: a8665354f58366fb7571634761e5e5058d8ab696 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package postgres

import (
	"database/sql"
	"fmt"

	"github.com/NyaaPantsu/nyaa/common"
	"github.com/NyaaPantsu/nyaa/model"
)

func (db *Database) InsertTorrentReport(report *model.TorrentReport) (err error) {
	_, err = db.getPrepared(queryInsertTorrentReport).Exec(report.Description, report.TorrentID, report.UserID, report.CreatedAt)
	return
}

func reportParamToQuery(param *common.ReportParam) (q sqlQuery) {
	q.query += fmt.Sprintf("SELECT %s FROM %s WHERE created_at IS NOT NULL ", torrentReportSelectColumnsFull, tableTorrentReports)

	counter := 1

	if !param.AllTime {
		q.query += fmt.Sprintf("AND created_at < $%d AND created_at > $%d", counter, counter+1)
		q.params = append(q.params, param.Before, param.After)
		counter += 2
	}

	if param.Limit > 0 {
		q.query += fmt.Sprintf("LIMIT $%d ", counter)
		q.params = append(q.params, param.Limit)
		counter++
	}
	if param.Offset > 0 {
		q.query += fmt.Sprintf("OFFSET $%d ", counter)
		q.params = append(q.params, param.Offset)
		counter++
	}
	return
}

func (db *Database) GetTorrentReportsWhere(param *common.ReportParam) (reports []model.TorrentReport, err error) {
	q := reportParamToQuery(param)
	err = q.Query(db.conn, func(rows *sql.Rows) error {
		for rows.Next() {
			var r model.TorrentReport
			scanTorrentReportColumnsFull(rows, &r)
			reports = append(reports, r)
		}
		return nil
	})
	return
}

func (db *Database) DeleteTorrentReportByID(id uint32) (err error) {
	_, err = db.getPrepared(queryDeleteTorrentReportByID).Exec(id)
	if err == sql.ErrNoRows {
		err = nil
	}
	return
}

func (db *Database) DeleteTorrentReportsWhere(param *common.ReportParam) (deleted uint32, err error) {

	return
}