tulip/database/db-types.go

80 lines
2.2 KiB
Go
Raw Normal View History

2023-09-06 22:20:09 +01:00
package database
import (
"database/sql"
2023-09-08 00:30:03 +01:00
"fmt"
2023-09-06 22:20:09 +01:00
"github.com/MrMelon54/pronouns"
"github.com/google/uuid"
"golang.org/x/text/language"
"net/url"
"time"
)
type User struct {
Sub uuid.UUID `json:"sub"`
Name string `json:"name,omitempty"`
Username string `json:"username"`
Password string `json:"password"`
Picture NullStringScanner `json:"picture,omitempty"`
Website NullStringScanner `json:"website,omitempty"`
Email string `json:"email"`
EmailVerified bool `json:"email_verified"`
Pronouns PronounScanner `json:"pronouns,omitempty"`
Birthdate NullDateScanner `json:"birthdate,omitempty"`
ZoneInfo LocationScanner `json:"zoneinfo,omitempty"`
Locale LocaleScanner `json:"locale,omitempty"`
UpdatedAt time.Time `json:"updated_at"`
Active bool `json:"active"`
}
type UserPatch struct {
Name string
Picture string
Website string
Pronouns pronouns.Pronoun
Birthdate sql.NullTime
ZoneInfo *time.Location
Locale language.Tag
2023-09-06 22:20:09 +01:00
}
2023-09-08 00:30:03 +01:00
func (u *UserPatch) ParseFromForm(v url.Values) (safeErrs []error) {
var err error
u.Name = v.Get("name")
u.Picture = v.Get("picture")
u.Website = v.Get("website")
if v.Has("reset_pronouns") {
u.Pronouns = pronouns.TheyThem
} else {
u.Pronouns, err = pronouns.FindPronoun(v.Get("pronouns"))
2023-09-06 22:20:09 +01:00
if err != nil {
2023-09-08 00:30:03 +01:00
safeErrs = append(safeErrs, fmt.Errorf("invalid pronoun selected"))
2023-09-06 22:20:09 +01:00
}
}
2023-09-08 00:30:03 +01:00
if v.Has("reset_birthdate") || v.Get("birthdate") == "" {
u.Birthdate = sql.NullTime{}
} else {
u.Birthdate = sql.NullTime{Valid: true}
u.Birthdate.Time, err = time.Parse(time.DateOnly, v.Get("birthdate"))
2023-09-06 22:20:09 +01:00
if err != nil {
2023-09-08 00:30:03 +01:00
safeErrs = append(safeErrs, fmt.Errorf("invalid time selected"))
2023-09-06 22:20:09 +01:00
}
}
if v.Has("reset_zoneinfo") {
u.ZoneInfo = time.UTC
} else {
u.ZoneInfo, err = time.LoadLocation(v.Get("zoneinfo"))
2023-09-06 22:20:09 +01:00
if err != nil {
2023-09-08 00:30:03 +01:00
safeErrs = append(safeErrs, fmt.Errorf("invalid timezone selected"))
2023-09-06 22:20:09 +01:00
}
}
if v.Has("reset_locale") {
u.Locale = language.AmericanEnglish
} else {
u.Locale, err = language.Parse(v.Get("locale"))
2023-09-06 22:20:09 +01:00
if err != nil {
2023-09-08 00:30:03 +01:00
safeErrs = append(safeErrs, fmt.Errorf("invalid language selected"))
2023-09-06 22:20:09 +01:00
}
}
2023-09-08 00:30:03 +01:00
return
2023-09-06 22:20:09 +01:00
}