95 lines
2.3 KiB
Go
95 lines
2.3 KiB
Go
package domain
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
UnverifiedUser = 0
|
|
VerificatedUser = 1
|
|
AdminUser = 100
|
|
)
|
|
|
|
type User struct {
|
|
ID int `json:"id" db:"id"`
|
|
Role int `json:"-" db:"role"`
|
|
Username string `json:"username" db:"username"`
|
|
Email string `json:"email" db:"email"`
|
|
HashedPassword string `json:"-" db:"hashed_password"`
|
|
AvatarImage string `json:"avatar_image" db:"avatar_image"`
|
|
BlackPhoenix bool `json:"black_phoenix" db:"black_phoenix"`
|
|
DateOfBirth CustomDate `json:"date_of_birth" db:"date_of_birth"`
|
|
DateOfRegistration CustomDate `json:"date_of_registration" db:"date_of_registration"`
|
|
}
|
|
|
|
func (u *User) UnmarshalBinary(b []byte) error {
|
|
return json.Unmarshal(b, u)
|
|
}
|
|
|
|
func (u User) MarshalBinary() ([]byte, error) {
|
|
return json.Marshal(u)
|
|
}
|
|
|
|
type UserFilter struct {
|
|
Username string `json:"username" validate:"omitempty,min=2,max=30"`
|
|
Email string `json:"email" validate:"omitempty,email"`
|
|
}
|
|
|
|
type UserPassword struct {
|
|
UserPassword string `json:"user_password" validate:"min=8"`
|
|
}
|
|
|
|
type UserRegister struct {
|
|
Email string `json:"email" validate:"email"`
|
|
Username string `json:"username" validate:"min=2,max=30"`
|
|
Password string `json:"password" validate:"min=8"`
|
|
Password2 string `json:"password2" validate:"eqfield=Password"`
|
|
DateOfBirth CustomDate `json:"date_of_birth" validate:"date_of_birth"`
|
|
}
|
|
|
|
type CustomDate struct {
|
|
time.Time
|
|
}
|
|
|
|
func (ct *CustomDate) UnmarshalJSON(data []byte) error {
|
|
str := string(data[1 : len(data)-1])
|
|
|
|
t, err := time.Parse(time.DateOnly, str)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ct.Time = t
|
|
return nil
|
|
}
|
|
|
|
func (ct CustomDate) MarshalJSON() ([]byte, error) {
|
|
return []byte(fmt.Sprintf("\"%s\"", ct.Format(time.DateOnly))), nil
|
|
}
|
|
|
|
func (ct CustomDate) String() string {
|
|
return ct.Format(time.DateOnly)
|
|
}
|
|
|
|
func (ct *CustomDate) Scan(value any) error {
|
|
switch v := value.(type) {
|
|
case string:
|
|
t, err := time.Parse(time.DateOnly, v)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
*ct = CustomDate{Time: t}
|
|
case time.Time:
|
|
*ct = CustomDate{Time: v}
|
|
default:
|
|
panic("incorrect time type received")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (ct CustomDate) Value() (driver.Value, error) {
|
|
return ct.Time.Format(time.DateOnly), nil
|
|
}
|