2023-03-19 16:14:09 +01:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"emperror.dev/errors"
|
|
|
|
"github.com/georgysavva/scany/pgxscan"
|
|
|
|
"github.com/rs/xid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Report struct {
|
2023-03-21 17:16:16 +01:00
|
|
|
ID int64 `json:"id"`
|
|
|
|
UserID xid.ID `json:"user_id"`
|
2023-03-23 11:30:47 +01:00
|
|
|
UserName string `json:"user_name"`
|
2023-03-21 17:16:16 +01:00
|
|
|
MemberID *xid.ID `json:"member_id"`
|
2023-03-23 11:30:47 +01:00
|
|
|
MemberName *string `json:"member_name"`
|
2023-03-21 17:16:16 +01:00
|
|
|
Reason string `json:"reason"`
|
|
|
|
ReporterID xid.ID `json:"reporter_id"`
|
2023-03-19 16:14:09 +01:00
|
|
|
|
2023-03-21 17:16:16 +01:00
|
|
|
CreatedAt time.Time `json:"created_at"`
|
|
|
|
ResolvedAt *time.Time `json:"resolved_at"`
|
|
|
|
AdminID *xid.ID `json:"admin_id"`
|
|
|
|
AdminComment *string `json:"admin_comment"`
|
2023-03-19 16:14:09 +01:00
|
|
|
}
|
|
|
|
|
2023-03-21 14:27:39 +01:00
|
|
|
const ReportPageSize = 100
|
2023-03-19 16:14:09 +01:00
|
|
|
|
2023-03-21 14:27:39 +01:00
|
|
|
func (db *DB) Reports(ctx context.Context, closed bool, before int) (rs []Report, err error) {
|
2023-03-23 11:30:47 +01:00
|
|
|
builder := sq.Select("*",
|
|
|
|
"(SELECT username FROM users WHERE id = reports.user_id) AS user_name",
|
|
|
|
"(SELECT name FROM members WHERE id = reports.member_id) AS member_name").
|
|
|
|
From("reports").
|
|
|
|
Limit(ReportPageSize).
|
|
|
|
OrderBy("id DESC")
|
2023-03-21 14:27:39 +01:00
|
|
|
if before != 0 {
|
|
|
|
builder = builder.Where("id < ?", before)
|
|
|
|
}
|
2023-03-19 16:14:09 +01:00
|
|
|
if closed {
|
|
|
|
builder = builder.Where("resolved_at IS NOT NULL")
|
|
|
|
} else {
|
|
|
|
builder = builder.Where("resolved_at IS NULL")
|
|
|
|
}
|
|
|
|
sql, args, err := builder.ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pgxscan.Select(ctx, db, &rs, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "executing query")
|
|
|
|
}
|
|
|
|
if len(rs) == 0 {
|
|
|
|
return []Report{}, nil
|
|
|
|
}
|
|
|
|
return rs, nil
|
|
|
|
}
|
2023-03-21 14:27:39 +01:00
|
|
|
|
|
|
|
func (db *DB) ReportsByUser(ctx context.Context, userID xid.ID, before int) (rs []Report, err error) {
|
|
|
|
builder := sq.Select("*").From("reports").Where("user_id = ?", userID).Limit(ReportPageSize).OrderBy("id DESC")
|
|
|
|
if before != 0 {
|
|
|
|
builder = builder.Where("id < ?", before)
|
|
|
|
}
|
|
|
|
sql, args, err := builder.ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pgxscan.Select(ctx, db, &rs, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "executing query")
|
|
|
|
}
|
|
|
|
if len(rs) == 0 {
|
|
|
|
return []Report{}, nil
|
|
|
|
}
|
|
|
|
return rs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) ReportsByReporter(ctx context.Context, reporterID xid.ID, before int) (rs []Report, err error) {
|
|
|
|
builder := sq.Select("*").From("reports").Where("reporter_id = ?", reporterID).Limit(ReportPageSize).OrderBy("id DESC")
|
|
|
|
if before != 0 {
|
|
|
|
builder = builder.Where("id < ?", before)
|
|
|
|
}
|
|
|
|
sql, args, err := builder.ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pgxscan.Select(ctx, db, &rs, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "executing query")
|
|
|
|
}
|
|
|
|
if len(rs) == 0 {
|
|
|
|
return []Report{}, nil
|
|
|
|
}
|
|
|
|
return rs, nil
|
|
|
|
}
|
2023-03-21 17:16:16 +01:00
|
|
|
|
|
|
|
func (db *DB) CreateReport(ctx context.Context, reporterID, userID xid.ID, memberID *xid.ID, reason string) (r Report, err error) {
|
|
|
|
sql, args, err := sq.Insert("reports").SetMap(map[string]any{
|
|
|
|
"user_id": userID,
|
|
|
|
"reporter_id": reporterID,
|
|
|
|
"member_id": memberID,
|
|
|
|
"reason": reason,
|
|
|
|
}).Suffix("RETURNING *").ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return r, errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = pgxscan.Get(ctx, db, &r, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
return r, errors.Wrap(err, "executing query")
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|