diff options
author | FChannel <> | 2022-05-08 14:57:40 -0700 |
---|---|---|
committer | FChannel <> | 2022-06-19 12:53:29 -0700 |
commit | 580dec5b89215310ce34341e11ff17fe38bdb63a (patch) | |
tree | 894424df66a9d9f7e41805822f29adac8fb490fe /db/report.go | |
parent | f7bf818d29393ceaccf4d2906557351fa6a4f49f (diff) |
more cleanup, logging and error logging everywhere
things are mostly in place can work on "features" and polish
Diffstat (limited to 'db/report.go')
-rw-r--r-- | db/report.go | 112 |
1 files changed, 34 insertions, 78 deletions
diff --git a/db/report.go b/db/report.go index 4120d9a..958b619 100644 --- a/db/report.go +++ b/db/report.go @@ -1,6 +1,9 @@ package db -import "github.com/FChannel0/FChannel-Server/config" +import ( + "github.com/FChannel0/FChannel-Server/config" + "github.com/FChannel0/FChannel-Server/util" +) type Report struct { ID string @@ -14,57 +17,53 @@ type Removed struct { Board string } -func CreateLocalDeleteDB(id string, _type string) error { - query := `select id from removed where id=$1` - - rows, err := config.DB.Query(query, id) - if err != nil { - return err - } - defer rows.Close() - - if rows.Next() { - var i string +func CloseLocalReport(id string, board string) error { + query := `delete from reported where id=$1 and board=$2` + _, err := config.DB.Exec(query, id, board) - if err := rows.Scan(&i); err != nil { - return err - } + return util.MakeError(err, "CloseLocalReportDB") +} - if i != "" { - query := `update removed set type=$1 where id=$2` +func CreateLocalDelete(id string, _type string) error { + var i string - if _, err := config.DB.Exec(query, _type, id); err != nil { - return err - } - } - } else { + query := `select id from removed where id=$1` + if err := config.DB.QueryRow(query, id).Scan(&i); err != nil { query := `insert into removed (id, type) values ($1, $2)` - if _, err := config.DB.Exec(query, id, _type); err != nil { - return err + return util.MakeError(err, "CreateLocalDeleteDB") } } - return nil + query = `update removed set type=$1 where id=$2` + _, err := config.DB.Exec(query, _type, id) + + return util.MakeError(err, "CreateLocalDeleteDB") +} + +func CreateLocalReport(id string, board string, reason string) error { + query := `insert into reported (id, count, board, reason) values ($1, $2, $3, $4)` + _, err := config.DB.Exec(query, id, 1, board, reason) + + return util.MakeError(err, "CreateLocalReportDB") } -func GetLocalDeleteDB() ([]Removed, error) { +func GetLocalDelete() ([]Removed, error) { var deleted []Removed query := `select id, type from removed` - rows, err := config.DB.Query(query) + if err != nil { - return deleted, err + return deleted, util.MakeError(err, "GetLocalDeleteDB") } defer rows.Close() - for rows.Next() { var r Removed if err := rows.Scan(&r.ID, &r.Type); err != nil { - return deleted, err + return deleted, util.MakeError(err, "GetLocalDeleteDB") } deleted = append(deleted, r) @@ -73,58 +72,22 @@ func GetLocalDeleteDB() ([]Removed, error) { return deleted, nil } -func CreateLocalReportDB(id string, board string, reason string) error { - query := `select id, count from reported where id=$1 and board=$2` - - rows, err := config.DB.Query(query, id, board) - if err != nil { - return err - } - defer rows.Close() - - if rows.Next() { - var i string - var count int - - if err := rows.Scan(&i, &count); err != nil { - return err - } - - if i != "" { - count = count + 1 - query := `update reported set count=$1 where id=$2` - - if _, err := config.DB.Exec(query, count, id); err != nil { - return err - } - } - } else { - query := `insert into reported (id, count, board, reason) values ($1, $2, $3, $4)` - - if _, err := config.DB.Exec(query, id, 1, board, reason); err != nil { - return err - } - } - - return nil -} - -func GetLocalReportDB(board string) ([]Report, error) { +func GetLocalReport(board string) ([]Report, error) { var reported []Report query := `select id, count, reason from reported where board=$1` - rows, err := config.DB.Query(query, board) + if err != nil { - return reported, err + return reported, util.MakeError(err, "GetLocalReportDB") } - defer rows.Close() + defer rows.Close() for rows.Next() { var r Report if err := rows.Scan(&r.ID, &r.Count, &r.Reason); err != nil { - return reported, err + return reported, util.MakeError(err, "GetLocalReportDB") } reported = append(reported, r) @@ -132,10 +95,3 @@ func GetLocalReportDB(board string) ([]Report, error) { return reported, nil } - -func CloseLocalReportDB(id string, board string) error { - query := `delete from reported where id=$1 and board=$2` - - _, err := config.DB.Exec(query, id, board) - return err -} |