2017-05-15 01:31:17 +02:00
package postgres
import (
"database/sql"
"fmt"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/common"
"github.com/NyaaPantsu/nyaa/model"
2017-05-15 01:31:17 +02:00
)
func ( db * Database ) InsertTorrentReport ( report * model . TorrentReport ) ( err error ) {
2017-05-15 01:43:07 +02:00
_ , err = db . getPrepared ( queryInsertTorrentReport ) . Exec ( report . Description , report . TorrentID , report . UserID , report . CreatedAt )
2017-05-15 01:31:17 +02:00
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
}