github.com/soulteary/pocket-bookcase@v0.0.0-20240428065142-0b5a9a0fc98a/internal/model/account.go (about)

     1  package model
     2  
     3  import (
     4  	"database/sql/driver"
     5  	"encoding/json"
     6  	"fmt"
     7  )
     8  
     9  // Account is the database model for account.
    10  type Account struct {
    11  	ID       int        `db:"id"       json:"id"`
    12  	Username string     `db:"username" json:"username"`
    13  	Password string     `db:"password" json:"password,omitempty"`
    14  	Owner    bool       `db:"owner"    json:"owner"`
    15  	Config   UserConfig `db:"config"               json:"config"`
    16  }
    17  
    18  type UserConfig struct {
    19  	ShowId        bool `json:"ShowId"`
    20  	ListMode      bool `json:"ListMode"`
    21  	HideThumbnail bool `json:"HideThumbnail"`
    22  	HideExcerpt   bool `json:"HideExcerpt"`
    23  	NightMode     bool `json:"NightMode"`
    24  	KeepMetadata  bool `json:"KeepMetadata"`
    25  	UseArchive    bool `json:"UseArchive"`
    26  	CreateEbook   bool `json:"CreateEbook"`
    27  	MakePublic    bool `json:"MakePublic"`
    28  }
    29  
    30  func (c *UserConfig) Scan(value interface{}) error {
    31  	switch v := value.(type) {
    32  	case []byte:
    33  		json.Unmarshal(v, &c)
    34  		return nil
    35  	case string:
    36  		json.Unmarshal([]byte(v), &c)
    37  		return nil
    38  	default:
    39  		return fmt.Errorf("unsupported type: %T", v)
    40  	}
    41  }
    42  
    43  func (c UserConfig) Value() (driver.Value, error) {
    44  	return json.Marshal(c)
    45  }
    46  
    47  // ToDTO converts Account to AccountDTO.
    48  func (a Account) ToDTO() AccountDTO {
    49  	return AccountDTO{
    50  		ID:       a.ID,
    51  		Username: a.Username,
    52  		Owner:    a.Owner,
    53  		Config:   a.Config,
    54  	}
    55  }
    56  
    57  // AccountDTO is data transfer object for Account.
    58  type AccountDTO struct {
    59  	ID       int        `json:"id"`
    60  	Username string     `json:"username"`
    61  	Owner    bool       `json:"owner"`
    62  	Config   UserConfig `json:"config"`
    63  }