pronounss/backend/db/field.go

142 lines
3.8 KiB
Go
Raw Normal View History

2022-05-10 16:33:29 +02:00
package db
import (
"context"
"fmt"
2022-05-10 16:33:29 +02:00
2023-01-04 22:41:29 +01:00
"codeberg.org/u1f320/pronouns.cc/backend/db/queries"
2022-05-10 16:33:29 +02:00
"emperror.dev/errors"
"github.com/jackc/pgx/v4"
2022-05-10 16:33:29 +02:00
"github.com/rs/xid"
)
const (
MaxFields = 25
FieldNameMaxLength = 100
FieldEntriesLimit = 100
FieldEntryMaxLength = 50
)
2022-05-10 16:33:29 +02:00
type Field struct {
2023-01-04 22:41:29 +01:00
ID int64 `json:"-"`
Name string `json:"name"`
Entries []FieldEntry `json:"entries"`
2022-05-10 16:33:29 +02:00
}
// Validate validates this field. If it is invalid, a non-empty string is returned as error message.
func (f Field) Validate() string {
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 22:41:29 +01:00
if length := len(f.Entries); length > FieldEntriesLimit {
return fmt.Sprintf("max number of entries is %d, current number is %d", FieldEntriesLimit, length)
}
2023-01-04 22:41:29 +01: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)
}
2023-01-04 22:41:29 +01:00
if entry.Status == StatusUnknown || entry.Status >= wordStatusMax {
return fmt.Sprintf("entries.%d: status is invalid, must be between 1 and %d, is %d", i, wordStatusMax-1, entry.Status)
}
}
return ""
}
2022-05-10 16:33:29 +02: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-01-04 22:41:29 +01:00
qfields, err := db.q.GetUserFields(ctx, id.String())
2022-05-10 16:33:29 +02:00
if err != nil {
2023-01-04 22:41:29 +01:00
return nil, errors.Wrap(err, "querying fields")
2022-05-10 16:33:29 +02:00
}
2023-01-04 22:41:29 +01:00
fs = make([]Field, len(qfields))
for i := range qfields {
fs[i] = Field{
ID: int64(*qfields[i].ID),
Name: *qfields[i].Name,
Entries: dbEntriesToFieldEntries(qfields[i].Entries),
}
2022-05-10 16:33:29 +02:00
}
2023-01-04 22:41:29 +01:00
2022-05-10 16:33:29 +02:00
return fs, nil
}
// SetUserFields updates the fields for the given user.
func (db *DB) SetUserFields(ctx context.Context, tx pgx.Tx, userID xid.ID, fields []Field) (err error) {
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 22:41:29 +01:00
querier := queries.NewQuerier(tx)
for _, field := range fields {
querier.InsertUserField(ctx, queries.InsertUserFieldParams{
UserID: userID.String(),
Name: field.Name,
Entries: entriesToDBEntries(field.Entries),
})
}
if err != nil {
return errors.Wrap(err, "inserting new fields")
}
return nil
}
// 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-01-04 22:41:29 +01:00
qfields, err := db.q.GetMemberFields(ctx, id.String())
if err != nil {
2023-01-04 22:41:29 +01:00
return nil, errors.Wrap(err, "querying fields")
}
2023-01-04 22:41:29 +01:00
fs = make([]Field, len(qfields))
for i := range qfields {
fs[i] = Field{
ID: int64(*qfields[i].ID),
Name: *qfields[i].Name,
Entries: dbEntriesToFieldEntries(qfields[i].Entries),
}
}
2023-01-04 22:41:29 +01: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 22:41:29 +01:00
querier := queries.NewQuerier(tx)
for _, field := range fields {
querier.InsertMemberField(ctx, queries.InsertMemberFieldParams{
MemberID: memberID.String(),
Name: field.Name,
Entries: entriesToDBEntries(field.Entries),
})
}
if err != nil {
return errors.Wrap(err, "inserting new fields")
}
return nil
}