2022-05-10 07:33:29 -07:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-05-17 13:35:26 -07:00
|
|
|
"fmt"
|
2022-05-10 07:33:29 -07:00
|
|
|
|
|
|
|
"emperror.dev/errors"
|
2023-04-03 19:11:03 -07:00
|
|
|
"github.com/georgysavva/scany/v2/pgxscan"
|
|
|
|
"github.com/jackc/pgx/v5"
|
2022-05-10 07:33:29 -07:00
|
|
|
"github.com/rs/xid"
|
|
|
|
)
|
|
|
|
|
2022-05-17 13:35:26 -07:00
|
|
|
const (
|
|
|
|
MaxFields = 25
|
|
|
|
FieldNameMaxLength = 100
|
|
|
|
FieldEntriesLimit = 100
|
2023-03-29 15:15:21 -07:00
|
|
|
FieldEntryMaxLength = 100
|
2022-05-17 13:35:26 -07:00
|
|
|
)
|
|
|
|
|
2022-05-10 07:33:29 -07:00
|
|
|
type Field struct {
|
2023-01-04 13:41:29 -08:00
|
|
|
ID int64 `json:"-"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Entries []FieldEntry `json:"entries"`
|
2022-05-10 07:33:29 -07:00
|
|
|
}
|
|
|
|
|
2022-05-17 13:35:26 -07:00
|
|
|
// Validate validates this field. If it is invalid, a non-empty string is returned as error message.
|
2023-04-19 03:00:21 -07:00
|
|
|
func (f Field) Validate(custom CustomPreferences) string {
|
2022-05-17 13:35:26 -07:00
|
|
|
if f.Name == "" {
|
|
|
|
return "name cannot be empty"
|
|
|
|
}
|
|
|
|
|
|
|
|
if length := len([]rune(f.Name)); length > FieldNameMaxLength {
|
|
|
|
return fmt.Sprintf("name max length is %d characters, length is %d", FieldNameMaxLength, length)
|
|
|
|
}
|
|
|
|
|
2023-01-04 13:41:29 -08:00
|
|
|
if length := len(f.Entries); length > FieldEntriesLimit {
|
2022-05-17 13:35:26 -07:00
|
|
|
return fmt.Sprintf("max number of entries is %d, current number is %d", FieldEntriesLimit, length)
|
|
|
|
}
|
|
|
|
|
2023-01-04 13:41:29 -08:00
|
|
|
for i, entry := range f.Entries {
|
|
|
|
if length := len([]rune(entry.Value)); length > FieldEntryMaxLength {
|
|
|
|
return fmt.Sprintf("entries.%d: max length is %d characters, length is %d", i, FieldEntryMaxLength, length)
|
2022-05-17 13:35:26 -07:00
|
|
|
}
|
|
|
|
|
2023-04-19 03:00:21 -07:00
|
|
|
if !entry.Status.Valid(custom) {
|
2023-03-29 14:59:51 -07:00
|
|
|
return fmt.Sprintf("entries.%d: status is invalid", i)
|
2022-05-17 13:35:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-05-10 07:33:29 -07:00
|
|
|
// UserFields returns the fields associated with the given user ID.
|
|
|
|
func (db *DB) UserFields(ctx context.Context, id xid.ID) (fs []Field, err error) {
|
2023-03-11 19:25:53 -08:00
|
|
|
sql, args, err := sq.Select("id", "name", "entries").From("user_fields").Where("user_id = ?", id).OrderBy("id").ToSql()
|
2022-05-10 07:33:29 -07:00
|
|
|
if err != nil {
|
2023-03-11 16:31:10 -08:00
|
|
|
return fs, errors.Wrap(err, "building sql")
|
2022-05-10 07:33:29 -07:00
|
|
|
}
|
|
|
|
|
2023-03-11 16:31:10 -08:00
|
|
|
err = pgxscan.Select(ctx, db, &fs, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
return fs, errors.Wrap(err, "executing query")
|
2022-05-10 07:33:29 -07:00
|
|
|
}
|
2023-01-04 13:41:29 -08:00
|
|
|
|
2022-05-10 07:33:29 -07:00
|
|
|
return fs, nil
|
|
|
|
}
|
2022-05-17 13:35:26 -07:00
|
|
|
|
|
|
|
// SetUserFields updates the fields for the given user.
|
2022-06-16 05:54:15 -07:00
|
|
|
func (db *DB) SetUserFields(ctx context.Context, tx pgx.Tx, userID xid.ID, fields []Field) (err error) {
|
2022-05-17 13:35:26 -07:00
|
|
|
sql, args, err := sq.Delete("user_fields").Where("user_id = ?", userID).ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tx.Exec(ctx, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "deleting existing fields")
|
|
|
|
}
|
|
|
|
|
2023-01-04 13:41:29 -08:00
|
|
|
for _, field := range fields {
|
2023-03-11 16:31:10 -08:00
|
|
|
_, err := tx.Exec(ctx, "INSERT INTO user_fields (user_id, name, entries) VALUES ($1, $2, $3)", userID, field.Name, field.Entries)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "inserting new fields")
|
|
|
|
}
|
2022-05-17 13:35:26 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-20 03:55:00 -07:00
|
|
|
|
|
|
|
// MemberFields returns the fields associated with the given member ID.
|
|
|
|
func (db *DB) MemberFields(ctx context.Context, id xid.ID) (fs []Field, err error) {
|
2023-03-11 19:25:53 -08:00
|
|
|
sql, args, err := sq.Select("id", "name", "entries").From("member_fields").Where("member_id = ?", id).OrderBy("id").ToSql()
|
2022-09-20 03:55:00 -07:00
|
|
|
if err != nil {
|
2023-03-11 16:31:10 -08:00
|
|
|
return fs, errors.Wrap(err, "building sql")
|
2022-09-20 03:55:00 -07:00
|
|
|
}
|
|
|
|
|
2023-03-11 16:31:10 -08:00
|
|
|
err = pgxscan.Select(ctx, db, &fs, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
return fs, errors.Wrap(err, "executing query")
|
2022-09-20 03:55:00 -07:00
|
|
|
}
|
2023-01-04 13:41:29 -08:00
|
|
|
|
2022-09-20 03:55:00 -07:00
|
|
|
return fs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMemberFields updates the fields for the given member.
|
|
|
|
func (db *DB) SetMemberFields(ctx context.Context, tx pgx.Tx, memberID xid.ID, fields []Field) (err error) {
|
|
|
|
sql, args, err := sq.Delete("member_fields").Where("member_id = ?", memberID).ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "building sql")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tx.Exec(ctx, sql, args...)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "deleting existing fields")
|
|
|
|
}
|
|
|
|
|
2023-01-04 13:41:29 -08:00
|
|
|
for _, field := range fields {
|
2023-03-11 16:31:10 -08:00
|
|
|
_, err := tx.Exec(ctx, "INSERT INTO member_fields (member_id, name, entries) VALUES ($1, $2, $3)", memberID, field.Name, field.Entries)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "inserting new fields")
|
|
|
|
}
|
2022-09-20 03:55:00 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|